2019-10-22 00:18:23 +13:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2020-02-05 20:19:48 +13:00
|
|
|
|
Copyright (c) 2007-2020 ShareX Team
|
2019-10-22 00:18:23 +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 Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using ShareX.HelpersLib;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace ShareX.HistoryLib
|
|
|
|
|
{
|
|
|
|
|
public class HistoryManagerJSON : HistoryManager
|
|
|
|
|
{
|
|
|
|
|
private static readonly object thisLock = new object();
|
|
|
|
|
|
|
|
|
|
public HistoryManagerJSON(string filePath) : base(filePath)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 01:42:17 +13:00
|
|
|
|
protected override List<HistoryItem> Load(string filePath)
|
2019-10-22 00:18:23 +13:00
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
|
|
|
|
|
{
|
|
|
|
|
lock (thisLock)
|
|
|
|
|
{
|
|
|
|
|
string json = File.ReadAllText(filePath, Encoding.UTF8);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(json))
|
|
|
|
|
{
|
|
|
|
|
json = "[" + json + "]";
|
|
|
|
|
|
|
|
|
|
return JsonConvert.DeserializeObject<List<HistoryItem>>(json);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new List<HistoryItem>();
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 01:42:17 +13:00
|
|
|
|
protected override bool Append(string filePath, IEnumerable<HistoryItem> historyItems)
|
2019-10-22 00:18:23 +13:00
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(filePath))
|
|
|
|
|
{
|
|
|
|
|
lock (thisLock)
|
|
|
|
|
{
|
|
|
|
|
Helpers.CreateDirectoryFromFilePath(filePath);
|
|
|
|
|
|
|
|
|
|
using (FileStream fs = File.Open(filePath, FileMode.Append, FileAccess.Write, FileShare.Read))
|
2019-10-22 01:05:22 +13:00
|
|
|
|
using (StreamWriter sw = new StreamWriter(fs))
|
2019-10-22 00:18:23 +13:00
|
|
|
|
{
|
2019-10-22 01:05:22 +13:00
|
|
|
|
JsonSerializer serializer = new JsonSerializer();
|
|
|
|
|
serializer.DefaultValueHandling = DefaultValueHandling.Ignore;
|
|
|
|
|
|
2019-10-22 00:18:23 +13:00
|
|
|
|
bool firstObject = fs.Length == 0;
|
|
|
|
|
|
2019-10-22 01:43:44 +13:00
|
|
|
|
foreach (HistoryItem historyItem in historyItems)
|
2019-10-22 00:18:23 +13:00
|
|
|
|
{
|
|
|
|
|
string json = "";
|
|
|
|
|
|
2019-10-22 01:43:44 +13:00
|
|
|
|
if (!firstObject)
|
2019-10-22 00:18:23 +13:00
|
|
|
|
{
|
|
|
|
|
json += ",\r\n";
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-22 01:43:44 +13:00
|
|
|
|
json += JObject.FromObject(historyItem, serializer).ToString();
|
2019-10-22 00:18:23 +13:00
|
|
|
|
|
|
|
|
|
sw.Write(json);
|
2019-10-22 01:43:44 +13:00
|
|
|
|
|
|
|
|
|
firstObject = false;
|
2019-10-22 00:18:23 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Backup(FilePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|