2016-02-03 09:06:22 +13:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2022-01-12 05:32:17 +13:00
|
|
|
|
Copyright (c) 2007-2022 ShareX Team
|
2016-02-03 09:06:22 +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)
|
|
|
|
|
|
2016-02-04 00:33:45 +13:00
|
|
|
|
using Newtonsoft.Json;
|
2016-02-04 01:12:15 +13:00
|
|
|
|
using ShareX.HelpersLib;
|
2016-02-04 00:33:45 +13:00
|
|
|
|
using System.IO;
|
2016-02-03 09:06:22 +13:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace ShareX.IndexerLib
|
|
|
|
|
{
|
|
|
|
|
public class IndexerJson : Indexer
|
|
|
|
|
{
|
2016-02-04 00:33:45 +13:00
|
|
|
|
private JsonWriter jsonWriter;
|
|
|
|
|
|
2016-02-03 09:06:22 +13:00
|
|
|
|
public IndexerJson(IndexerSettings indexerSettings) : base(indexerSettings)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Index(string folderPath)
|
|
|
|
|
{
|
|
|
|
|
FolderInfo folderInfo = GetFolderInfo(folderPath);
|
|
|
|
|
folderInfo.Update();
|
|
|
|
|
|
2016-02-04 00:33:45 +13:00
|
|
|
|
StringBuilder sbContent = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
using (StringWriter sw = new StringWriter(sbContent))
|
|
|
|
|
using (jsonWriter = new JsonTextWriter(sw))
|
|
|
|
|
{
|
|
|
|
|
jsonWriter.Formatting = Formatting.Indented;
|
|
|
|
|
|
|
|
|
|
jsonWriter.WriteStartObject();
|
|
|
|
|
IndexFolder(folderInfo);
|
|
|
|
|
jsonWriter.WriteEndObject();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sbContent.ToString();
|
2016-02-03 09:06:22 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void IndexFolder(FolderInfo dir, int level = 0)
|
2016-02-04 01:12:15 +13:00
|
|
|
|
{
|
|
|
|
|
if (settings.CreateParseableJson)
|
|
|
|
|
{
|
2021-06-11 06:44:40 +12:00
|
|
|
|
IndexFolderParseable(dir);
|
2016-02-04 01:12:15 +13:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-06-11 06:44:40 +12:00
|
|
|
|
IndexFolderSimple(dir);
|
2016-02-04 01:12:15 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-11 06:44:40 +12:00
|
|
|
|
private void IndexFolderSimple(FolderInfo dir)
|
2016-02-03 09:06:22 +13:00
|
|
|
|
{
|
2016-02-04 00:33:45 +13:00
|
|
|
|
jsonWriter.WritePropertyName(dir.FolderName);
|
|
|
|
|
jsonWriter.WriteStartArray();
|
|
|
|
|
|
|
|
|
|
foreach (FolderInfo subdir in dir.Folders)
|
|
|
|
|
{
|
|
|
|
|
jsonWriter.WriteStartObject();
|
|
|
|
|
IndexFolder(subdir);
|
|
|
|
|
jsonWriter.WriteEndObject();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (FileInfo fi in dir.Files)
|
|
|
|
|
{
|
|
|
|
|
jsonWriter.WriteValue(fi.Name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jsonWriter.WriteEnd();
|
2016-02-03 09:06:22 +13:00
|
|
|
|
}
|
2016-02-04 01:12:15 +13:00
|
|
|
|
|
2021-06-11 06:44:40 +12:00
|
|
|
|
private void IndexFolderParseable(FolderInfo dir)
|
2016-02-04 01:12:15 +13:00
|
|
|
|
{
|
|
|
|
|
jsonWriter.WritePropertyName("Name");
|
|
|
|
|
jsonWriter.WriteValue(dir.FolderName);
|
|
|
|
|
|
|
|
|
|
if (settings.ShowSizeInfo)
|
|
|
|
|
{
|
|
|
|
|
jsonWriter.WritePropertyName("Size");
|
|
|
|
|
jsonWriter.WriteValue(dir.Size.ToSizeString(settings.BinaryUnits));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dir.Folders.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
jsonWriter.WritePropertyName("Folders");
|
|
|
|
|
jsonWriter.WriteStartArray();
|
|
|
|
|
|
|
|
|
|
foreach (FolderInfo subdir in dir.Folders)
|
|
|
|
|
{
|
|
|
|
|
jsonWriter.WriteStartObject();
|
|
|
|
|
IndexFolder(subdir);
|
|
|
|
|
jsonWriter.WriteEndObject();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jsonWriter.WriteEnd();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dir.Files.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
jsonWriter.WritePropertyName("Files");
|
|
|
|
|
jsonWriter.WriteStartArray();
|
|
|
|
|
|
|
|
|
|
foreach (FileInfo fi in dir.Files)
|
|
|
|
|
{
|
|
|
|
|
jsonWriter.WriteStartObject();
|
|
|
|
|
|
|
|
|
|
jsonWriter.WritePropertyName("Name");
|
|
|
|
|
jsonWriter.WriteValue(fi.Name);
|
|
|
|
|
|
|
|
|
|
if (settings.ShowSizeInfo)
|
|
|
|
|
{
|
|
|
|
|
jsonWriter.WritePropertyName("Size");
|
|
|
|
|
jsonWriter.WriteValue(fi.Length.ToSizeString(settings.BinaryUnits));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jsonWriter.WriteEndObject();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jsonWriter.WriteEnd();
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-03 09:06:22 +13:00
|
|
|
|
}
|
|
|
|
|
}
|