ShareX/ShareX.IndexerLib/Indexer.cs

109 lines
3.7 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2023-01-10 09:31:02 +13:00
Copyright (c) 2007-2023 ShareX Team
2013-11-03 23:53:49 +13:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using System;
using System.IO;
2014-12-11 09:25:20 +13:00
namespace ShareX.IndexerLib
2013-11-03 23:53:49 +13:00
{
public abstract class Indexer
{
2016-02-03 23:58:54 +13:00
protected IndexerSettings settings = null;
2013-11-03 23:53:49 +13:00
protected Indexer(IndexerSettings indexerSettings)
{
2016-02-03 23:58:54 +13:00
settings = indexerSettings;
2013-11-03 23:53:49 +13:00
}
2016-02-03 23:58:54 +13:00
public static string Index(string folderPath, IndexerSettings settings)
2013-11-03 23:53:49 +13:00
{
Indexer indexer = null;
2016-02-03 23:58:54 +13:00
switch (settings.Output)
2013-11-03 23:53:49 +13:00
{
case IndexerOutput.Html:
2016-02-03 23:58:54 +13:00
indexer = new IndexerHtml(settings);
2013-11-03 23:53:49 +13:00
break;
case IndexerOutput.Txt:
2016-02-03 23:58:54 +13:00
indexer = new IndexerText(settings);
2013-11-03 23:53:49 +13:00
break;
case IndexerOutput.Xml:
2016-02-03 23:58:54 +13:00
indexer = new IndexerXml(settings);
2013-11-03 23:53:49 +13:00
break;
2016-02-03 09:06:22 +13:00
case IndexerOutput.Json:
2016-02-03 23:58:54 +13:00
indexer = new IndexerJson(settings);
2016-02-03 09:06:22 +13:00
break;
2013-11-03 23:53:49 +13:00
}
return indexer.Index(folderPath);
}
2016-02-03 23:58:54 +13:00
public abstract string Index(string folderPath);
2013-11-03 23:53:49 +13:00
2016-02-03 23:58:54 +13:00
protected abstract void IndexFolder(FolderInfo dir, int level = 0);
2013-11-03 23:53:49 +13:00
protected FolderInfo GetFolderInfo(string folderPath, int level = 0)
{
FolderInfo folderInfo = new FolderInfo(folderPath);
2016-02-03 23:58:54 +13:00
if (settings.MaxDepthLevel == 0 || level < settings.MaxDepthLevel)
2013-11-03 23:53:49 +13:00
{
try
2013-11-03 23:53:49 +13:00
{
DirectoryInfo currentDirectoryInfo = new DirectoryInfo(folderPath);
foreach (DirectoryInfo directoryInfo in currentDirectoryInfo.EnumerateDirectories())
2013-11-03 23:53:49 +13:00
{
2016-02-03 23:58:54 +13:00
if (settings.SkipHiddenFolders && directoryInfo.Attributes.HasFlag(FileAttributes.Hidden))
{
continue;
}
FolderInfo subFolderInfo = GetFolderInfo(directoryInfo.FullName, level + 1);
folderInfo.Folders.Add(subFolderInfo);
subFolderInfo.Parent = folderInfo;
2013-11-03 23:53:49 +13:00
}
foreach (FileInfo fileInfo in currentDirectoryInfo.EnumerateFiles())
2013-11-03 23:53:49 +13:00
{
2016-02-03 23:58:54 +13:00
if (settings.SkipHiddenFiles && fileInfo.Attributes.HasFlag(FileAttributes.Hidden))
{
continue;
}
folderInfo.Files.Add(fileInfo);
2013-11-03 23:53:49 +13:00
}
folderInfo.Files.Sort((x, y) => x.Name.CompareTo(y.Name));
}
catch (UnauthorizedAccessException)
{
2013-11-03 23:53:49 +13:00
}
}
return folderInfo;
}
}
}