From 3d56ae2f622749ba67a713a056c93f72bdcf876a Mon Sep 17 00:00:00 2001 From: Jaex Date: Fri, 3 Aug 2018 13:40:00 +0300 Subject: [PATCH] Improvements to backup system which allows creating weekly backups right after saving --- ShareX.HelpersLib/Helpers/Helpers.cs | 56 +++++++++++++++--------- ShareX.HelpersLib/SettingsBase.cs | 49 ++++++++++++++------- ShareX.HistoryLib/Forms/HistoryForm.resx | 16 +++---- ShareX/Program.cs | 1 - ShareX/SettingManager.cs | 26 +++++------ 5 files changed, 88 insertions(+), 60 deletions(-) diff --git a/ShareX.HelpersLib/Helpers/Helpers.cs b/ShareX.HelpersLib/Helpers/Helpers.cs index fef770b43..f3d85ebdc 100644 --- a/ShareX.HelpersLib/Helpers/Helpers.cs +++ b/ShareX.HelpersLib/Helpers/Helpers.cs @@ -885,37 +885,51 @@ public static void CreateDirectoryFromFilePath(string path) } } - public static void BackupFileMonthly(string filepath, string destinationFolder) + public static void CopyFile(string filePath, string destinationFolder, bool overwrite = true) { - if (!string.IsNullOrEmpty(filepath) && File.Exists(filepath)) + if (!string.IsNullOrEmpty(filePath) && !string.IsNullOrEmpty(destinationFolder)) { - string filename = Path.GetFileNameWithoutExtension(filepath); - string extension = Path.GetExtension(filepath); - string newFilename = string.Format("{0}-{1:yyyy-MM}{2}", filename, DateTime.Now, extension); - string newFilepath = Path.Combine(destinationFolder, newFilename); - - if (!File.Exists(newFilepath)) - { - CreateDirectoryFromFilePath(newFilepath); - File.Copy(filepath, newFilepath, false); - } + string fileName = Path.GetFileName(filePath); + string destinationFilePath = Path.Combine(destinationFolder, fileName); + CreateDirectoryFromDirectoryPath(destinationFolder); + File.Copy(filePath, destinationFilePath, overwrite); } } - public static void BackupFileWeekly(string filepath, string destinationFolder) + public static string BackupFileWeekly(string filePath, string destinationFolder) { - if (!string.IsNullOrEmpty(filepath) && File.Exists(filepath)) + if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath)) { - string filename = Path.GetFileNameWithoutExtension(filepath); + string fileName = Path.GetFileNameWithoutExtension(filePath); DateTime dateTime = DateTime.Now; - string extension = Path.GetExtension(filepath); - string newFilename = string.Format("{0}-{1:yyyy-MM}-W{2:00}{3}", filename, dateTime, dateTime.WeekOfYear(), extension); - string newFilepath = Path.Combine(destinationFolder, newFilename); + string extension = Path.GetExtension(filePath); + string newFileName = string.Format("{0}-{1:yyyy-MM}-W{2:00}{3}", fileName, dateTime, dateTime.WeekOfYear(), extension); + string newFilePath = Path.Combine(destinationFolder, newFileName); - if (!File.Exists(newFilepath)) + if (!File.Exists(newFilePath)) { - CreateDirectoryFromFilePath(newFilepath); - File.Copy(filepath, newFilepath, false); + CreateDirectoryFromDirectoryPath(destinationFolder); + File.Copy(filePath, newFilePath, false); + return newFilePath; + } + } + + return null; + } + + public static void BackupFileMonthly(string filePath, string destinationFolder) + { + if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath)) + { + string fileName = Path.GetFileNameWithoutExtension(filePath); + string extension = Path.GetExtension(filePath); + string newFileName = string.Format("{0}-{1:yyyy-MM}{2}", fileName, DateTime.Now, extension); + string newFilePath = Path.Combine(destinationFolder, newFileName); + + if (!File.Exists(newFilePath)) + { + CreateDirectoryFromDirectoryPath(destinationFolder); + File.Copy(filePath, newFilePath, false); } } } diff --git a/ShareX.HelpersLib/SettingsBase.cs b/ShareX.HelpersLib/SettingsBase.cs index f13342919..624f0b479 100644 --- a/ShareX.HelpersLib/SettingsBase.cs +++ b/ShareX.HelpersLib/SettingsBase.cs @@ -37,18 +37,27 @@ public abstract class SettingsBase where T : SettingsBase, new() public delegate void SettingsSavedEventHandler(T settings, string filePath, bool result); public event SettingsSavedEventHandler SettingsSaved; - [Browsable(false)] + [Browsable(false), JsonIgnore] public string FilePath { get; private set; } [Browsable(false)] public string ApplicationVersion { get; set; } - [Browsable(false)] + [Browsable(false), JsonIgnore] public bool IsFirstTimeRun { get; private set; } - [Browsable(false)] + [Browsable(false), JsonIgnore] public bool IsUpgrade { get; private set; } + [Browsable(false), JsonIgnore] + public string BackupFolder { get; set; } + + [Browsable(false), JsonIgnore] + public bool CreateBackup { get; set; } + + [Browsable(false), JsonIgnore] + public bool CreateWeeklyBackup { get; set; } + public bool IsUpgradeFrom(string version) { return IsUpgrade && Helpers.CompareVersion(ApplicationVersion, version) <= 0; @@ -67,7 +76,7 @@ public bool Save(string filePath) FilePath = filePath; ApplicationVersion = Application.ProductVersion; - bool result = SaveInternal(this, FilePath, true); + bool result = SaveInternal(FilePath); OnSettingsSaved(FilePath, result); @@ -89,23 +98,26 @@ public void SaveAsync() SaveAsync(FilePath); } - public static T Load(string filePath) + public static T Load(string filePath, string backupFolder = null, bool createBackup = false, bool createWeeklyBackup = false) { - T setting = LoadInternal(filePath, true); + T setting = LoadInternal(filePath, backupFolder); if (setting != null) { setting.FilePath = filePath; setting.IsFirstTimeRun = string.IsNullOrEmpty(setting.ApplicationVersion); setting.IsUpgrade = !setting.IsFirstTimeRun && Helpers.CompareApplicationVersion(setting.ApplicationVersion) < 0; + setting.BackupFolder = backupFolder; + setting.CreateBackup = createBackup; + setting.CreateWeeklyBackup = createWeeklyBackup; } return setting; } - private static bool SaveInternal(object obj, string filePath, bool createBackup) + private bool SaveInternal(string filePath) { - string typeName = obj.GetType().Name; + string typeName = GetType().Name; DebugHelper.WriteLine("{0} save started: {1}", typeName, filePath); bool isSuccess = false; @@ -114,7 +126,7 @@ private static bool SaveInternal(object obj, string filePath, bool createBackup) { if (!string.IsNullOrEmpty(filePath)) { - lock (obj) + lock (this) { Helpers.CreateDirectoryFromFilePath(filePath); @@ -130,15 +142,15 @@ private static bool SaveInternal(object obj, string filePath, bool createBackup) JsonSerializer serializer = new JsonSerializer(); serializer.ContractResolver = new WritablePropertiesOnlyResolver(); serializer.Converters.Add(new StringEnumConverter()); - serializer.Serialize(jsonWriter, obj); + serializer.Serialize(jsonWriter, this); jsonWriter.Flush(); } if (File.Exists(filePath)) { - if (createBackup) + if (CreateBackup) { - File.Copy(filePath, filePath + ".bak", true); + Helpers.CopyFile(filePath, BackupFolder); } File.Delete(filePath); @@ -146,6 +158,11 @@ private static bool SaveInternal(object obj, string filePath, bool createBackup) File.Move(tempFilePath, filePath); + if (CreateWeeklyBackup && !string.IsNullOrEmpty(BackupFolder)) + { + Helpers.BackupFileWeekly(filePath, BackupFolder); + } + isSuccess = true; } } @@ -162,7 +179,7 @@ private static bool SaveInternal(object obj, string filePath, bool createBackup) return isSuccess; } - private static T LoadInternal(string filePath, bool checkBackup) + private static T LoadInternal(string filePath, string backupFolder = null) { string typeName = typeof(T).Name; @@ -209,9 +226,11 @@ private static T LoadInternal(string filePath, bool checkBackup) DebugHelper.WriteException(e, typeName + " load failed: " + filePath); } - if (checkBackup) + if (!string.IsNullOrEmpty(backupFolder)) { - return LoadInternal(filePath + ".bak", false); + string fileName = Path.GetFileName(filePath); + string backupFilePath = Path.Combine(backupFolder, fileName); + return LoadInternal(backupFilePath); } } diff --git a/ShareX.HistoryLib/Forms/HistoryForm.resx b/ShareX.HistoryLib/Forms/HistoryForm.resx index ca83c2d83..195963335 100644 --- a/ShareX.HistoryLib/Forms/HistoryForm.resx +++ b/ShareX.HistoryLib/Forms/HistoryForm.resx @@ -223,13 +223,13 @@ 13, 24 - 74, 13 + 52, 13 17 - Filename filter: + Filename: lblFilenameFilter @@ -349,13 +349,13 @@ 16, 179 - 73, 17 + 51, 17 12 - Host filter: + Host: cbHostFilter @@ -379,13 +379,13 @@ 16, 152 - 75, 17 + 53, 17 10 - Type filter: + Type: cbTypeFilter @@ -490,13 +490,13 @@ 16, 72 - 74, 17 + 52, 17 0 - Date filter: + Date: cbDateFilter diff --git a/ShareX/Program.cs b/ShareX/Program.cs index e5cf0968d..040038dd2 100644 --- a/ShareX/Program.cs +++ b/ShareX/Program.cs @@ -300,7 +300,6 @@ private static void Run() if (WatchFolderManager != null) WatchFolderManager.Dispose(); SettingManager.SaveAllSettings(); - SettingManager.BackupSettings(); DebugHelper.Logger.AsyncWrite = false; DebugHelper.WriteLine("ShareX closing."); diff --git a/ShareX/SettingManager.cs b/ShareX/SettingManager.cs index ab1e2fff5..afeb1c9eb 100644 --- a/ShareX/SettingManager.cs +++ b/ShareX/SettingManager.cs @@ -141,18 +141,18 @@ public static void WaitHotkeysConfig() public static void LoadApplicationConfig() { - Settings = ApplicationConfig.Load(ApplicationConfigFilePath); + Settings = ApplicationConfig.Load(ApplicationConfigFilePath, BackupFolder, true, true); DefaultTaskSettings = Settings.DefaultTaskSettings; } public static void LoadUploadersConfig() { - UploadersConfig = UploadersConfig.Load(UploadersConfigFilePath); + UploadersConfig = UploadersConfig.Load(UploadersConfigFilePath, BackupFolder, true, true); } public static void LoadHotkeysConfig() { - HotkeysConfig = HotkeysConfig.Load(HotkeysConfigFilePath); + HotkeysConfig = HotkeysConfig.Load(HotkeysConfigFilePath, BackupFolder, true, true); } public static void LoadAllSettings() @@ -242,20 +242,16 @@ public static void SaveAllSettingsAsync() SaveHotkeysConfigAsync(); } - public static void BackupSettings() - { - Helpers.BackupFileWeekly(ApplicationConfigFilePath, BackupFolder); - Helpers.BackupFileWeekly(UploadersConfigFilePath, BackupFolder); - Helpers.BackupFileWeekly(HotkeysConfigFilePath, BackupFolder); - Helpers.BackupFileWeekly(Program.HistoryFilePath, BackupFolder); - } - public static void ResetSettings() { - Settings = new ApplicationConfig(); - DefaultTaskSettings = Settings.DefaultTaskSettings; - UploadersConfig = new UploadersConfig(); - HotkeysConfig = new HotkeysConfig(); + if (File.Exists(ApplicationConfigFilePath)) File.Delete(ApplicationConfigFilePath); + LoadApplicationConfig(); + + if (File.Exists(UploadersConfigFilePath)) File.Delete(UploadersConfigFilePath); + LoadUploadersConfig(); + + if (File.Exists(HotkeysConfigFilePath)) File.Delete(HotkeysConfigFilePath); + LoadHotkeysConfig(); } public static bool Export(string archivePath)