Using JSON for history file now, XML history file gonna be automatically migrated to JSON history file on startup

This commit is contained in:
Jaex 2019-10-25 15:42:17 +03:00
parent c039479d79
commit 82ad2deb94
9 changed files with 93 additions and 34 deletions

View file

@ -177,26 +177,6 @@ public static string ChangeFilenameExtension(string filePath, string extension)
return filePath; return filePath;
} }
public static string RenameFile(string filePath, string newFileName)
{
try
{
if (File.Exists(filePath))
{
string directory = Path.GetDirectoryName(filePath);
string newPath = Path.Combine(directory, newFileName);
File.Move(filePath, newPath);
return newPath;
}
}
catch (Exception e)
{
MessageBox.Show("Rename error:\r\n" + e.ToString(), "ShareX - " + Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return filePath;
}
public static string AppendExtension(string filePath, string extension) public static string AppendExtension(string filePath, string extension)
{ {
return filePath.TrimEnd('.') + '.' + extension.TrimStart('.'); return filePath.TrimEnd('.') + '.' + extension.TrimStart('.');
@ -966,15 +946,58 @@ public static bool IsValidFilePath(string path)
return fi != null; return fi != null;
} }
public static void CopyFile(string filePath, string destinationFolder, bool overwrite = true) public static string CopyFile(string filePath, string destinationFolder, bool overwrite = true)
{ {
if (!string.IsNullOrEmpty(filePath) && !string.IsNullOrEmpty(destinationFolder)) if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath) && !string.IsNullOrEmpty(destinationFolder))
{ {
string fileName = Path.GetFileName(filePath); string fileName = Path.GetFileName(filePath);
string destinationFilePath = Path.Combine(destinationFolder, fileName); string destinationFilePath = Path.Combine(destinationFolder, fileName);
CreateDirectoryFromDirectoryPath(destinationFolder); CreateDirectoryFromDirectoryPath(destinationFolder);
File.Copy(filePath, destinationFilePath, overwrite); File.Copy(filePath, destinationFilePath, overwrite);
return destinationFilePath;
} }
return null;
}
public static string MoveFile(string filePath, string destinationFolder, bool overwrite = true)
{
if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath) && !string.IsNullOrEmpty(destinationFolder))
{
string fileName = Path.GetFileName(filePath);
string destinationFilePath = Path.Combine(destinationFolder, fileName);
CreateDirectoryFromDirectoryPath(destinationFolder);
if (overwrite && File.Exists(destinationFilePath))
{
File.Delete(destinationFilePath);
}
File.Move(filePath, destinationFilePath);
return destinationFilePath;
}
return null;
}
public static string RenameFile(string filePath, string newFileName)
{
try
{
if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
{
string directory = Path.GetDirectoryName(filePath);
string newFilePath = Path.Combine(directory, newFileName);
File.Move(filePath, newFilePath);
return newFilePath;
}
}
catch (Exception e)
{
MessageBox.Show("Rename file error:\r\n" + e.ToString(), "ShareX - " + Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return filePath;
} }
public static string BackupFileWeekly(string filePath, string destinationFolder) public static string BackupFileWeekly(string filePath, string destinationFolder)

View file

@ -154,7 +154,7 @@ private HistoryItem[] GetHistoryItems()
{ {
if (history == null) if (history == null)
{ {
history = new HistoryManagerXML(HistoryPath); history = new HistoryManagerJSON(HistoryPath);
} }
IEnumerable<HistoryItem> tempHistoryItems = history.GetHistoryItems(); IEnumerable<HistoryItem> tempHistoryItems = history.GetHistoryItems();

View file

@ -113,7 +113,7 @@ private IEnumerable<HistoryItem> GetHistoryItems()
{ {
if (history == null) if (history == null)
{ {
history = new HistoryManagerXML(HistoryPath); history = new HistoryManagerJSON(HistoryPath);
} }
List<HistoryItem> historyItems = history.GetHistoryItems(); List<HistoryItem> historyItems = history.GetHistoryItems();

View file

@ -87,19 +87,19 @@ private bool IsValidHistoryItem(HistoryItem historyItem)
(!string.IsNullOrEmpty(historyItem.URL) || !string.IsNullOrEmpty(historyItem.FilePath)); (!string.IsNullOrEmpty(historyItem.URL) || !string.IsNullOrEmpty(historyItem.FilePath));
} }
public List<HistoryItem> Load() protected List<HistoryItem> Load()
{ {
return Load(FilePath); return Load(FilePath);
} }
public abstract List<HistoryItem> Load(string filePath); protected abstract List<HistoryItem> Load(string filePath);
public bool Append(IEnumerable<HistoryItem> historyItems) protected bool Append(IEnumerable<HistoryItem> historyItems)
{ {
return Append(FilePath, historyItems); return Append(FilePath, historyItems);
} }
public abstract bool Append(string filePath, IEnumerable<HistoryItem> historyItems); protected abstract bool Append(string filePath, IEnumerable<HistoryItem> historyItems);
protected void Backup(string filePath) protected void Backup(string filePath)
{ {

View file

@ -40,7 +40,7 @@ public HistoryManagerJSON(string filePath) : base(filePath)
{ {
} }
public override List<HistoryItem> Load(string filePath) protected override List<HistoryItem> Load(string filePath)
{ {
if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath)) if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
{ {
@ -60,7 +60,7 @@ public override List<HistoryItem> Load(string filePath)
return new List<HistoryItem>(); return new List<HistoryItem>();
} }
public override bool Append(string filePath, IEnumerable<HistoryItem> historyItems) protected override bool Append(string filePath, IEnumerable<HistoryItem> historyItems)
{ {
if (!string.IsNullOrEmpty(filePath)) if (!string.IsNullOrEmpty(filePath))
{ {

View file

@ -41,7 +41,7 @@ public HistoryManagerXML(string filePath) : base(filePath)
{ {
} }
public override List<HistoryItem> Load(string filePath) protected override List<HistoryItem> Load(string filePath)
{ {
List<HistoryItem> historyItemList = new List<HistoryItem>(); List<HistoryItem> historyItemList = new List<HistoryItem>();
@ -131,7 +131,7 @@ private HistoryItem ParseHistoryItem(XElement element)
return hi; return hi;
} }
public override bool Append(string filePath, IEnumerable<HistoryItem> historyItems) protected override bool Append(string filePath, IEnumerable<HistoryItem> historyItems)
{ {
if (!string.IsNullOrEmpty(filePath)) if (!string.IsNullOrEmpty(filePath))
{ {

View file

@ -148,7 +148,7 @@ public static string PersonalFolder
} }
} }
public const string HistoryFilename = "History.xml"; public const string HistoryFilename = "History.json";
public static string HistoryFilePath public static string HistoryFilePath
{ {
@ -160,6 +160,18 @@ public static string HistoryFilePath
} }
} }
public const string HistoryFilenameOld = "History.xml";
public static string HistoryFilePathOld
{
get
{
if (Sandbox) return null;
return Path.Combine(PersonalFolder, HistoryFilenameOld);
}
}
public const string LogsFoldername = "Logs"; public const string LogsFoldername = "Logs";
public static string LogsFolder => Path.Combine(PersonalFolder, LogsFoldername); public static string LogsFolder => Path.Combine(PersonalFolder, LogsFoldername);

View file

@ -24,6 +24,7 @@ You should have received a copy of the GNU General Public License
#endregion License Information (GPL v3) #endregion License Information (GPL v3)
using ShareX.HelpersLib; using ShareX.HelpersLib;
using ShareX.HistoryLib;
using ShareX.ScreenCaptureLib; using ShareX.ScreenCaptureLib;
using ShareX.UploadersLib; using ShareX.UploadersLib;
using ShareX.UploadersLib.FileUploaders; using ShareX.UploadersLib.FileUploaders;
@ -145,6 +146,7 @@ public static void LoadApplicationConfig()
Settings.SettingsSaveFailed += Settings_SettingsSaveFailed; Settings.SettingsSaveFailed += Settings_SettingsSaveFailed;
DefaultTaskSettings = Settings.DefaultTaskSettings; DefaultTaskSettings = Settings.DefaultTaskSettings;
ApplicationConfigBackwardCompatibilityTasks(); ApplicationConfigBackwardCompatibilityTasks();
MigrateHistoryFile();
} }
private static void Settings_SettingsSaveFailed(Exception e) private static void Settings_SettingsSaveFailed(Exception e)
@ -209,6 +211,28 @@ private static void ApplicationConfigBackwardCompatibilityTasks()
} }
} }
private static void MigrateHistoryFile()
{
if (File.Exists(Program.HistoryFilePathOld))
{
if (!File.Exists(Program.HistoryFilePath))
{
DebugHelper.WriteLine($"Migrating XML history file \"{Program.HistoryFilePathOld}\" to JSON history file \"{Program.HistoryFilePath}\"");
HistoryManagerXML historyManagerXML = new HistoryManagerXML(Program.HistoryFilePathOld);
List<HistoryItem> historyItems = historyManagerXML.GetHistoryItems();
if (historyItems.Count > 0)
{
HistoryManagerJSON historyManagerJSON = new HistoryManagerJSON(Program.HistoryFilePath);
historyManagerJSON.AppendHistoryItems(historyItems);
}
}
Helpers.MoveFile(Program.HistoryFilePathOld, BackupFolder);
}
}
private static void UploadersConfigBackwardCompatibilityTasks() private static void UploadersConfigBackwardCompatibilityTasks()
{ {
if (UploadersConfig.IsUpgradeFrom("11.6.0")) if (UploadersConfig.IsUpgradeFrom("11.6.0"))

View file

@ -552,7 +552,7 @@ private static void AppendHistoryItemAsync(HistoryItem historyItem)
{ {
Task.Run(() => Task.Run(() =>
{ {
HistoryManager history = new HistoryManagerXML(Program.HistoryFilePath) HistoryManager history = new HistoryManagerJSON(Program.HistoryFilePath)
{ {
BackupFolder = SettingManager.BackupFolder, BackupFolder = SettingManager.BackupFolder,
CreateBackup = false, CreateBackup = false,