From d55560ea97c92528b25c71a85e8e0ba40c36bec5 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Fri, 21 Feb 2020 22:06:04 +1100 Subject: [PATCH] Add UserData operations- Copy, verify, remove --- Wox.Core/Configuration/IPortable.cs | 1 + Wox.Core/Configuration/Portable.cs | 16 ++++- Wox.Infrastructure/Wox.cs | 31 ++++----- Wox.Plugin/SharedCommands/FilesFolders.cs | 76 +++++++++++++++++++++++ Wox.Plugin/Wox.Plugin.csproj | 1 + 5 files changed, 110 insertions(+), 15 deletions(-) create mode 100644 Wox.Plugin/SharedCommands/FilesFolders.cs diff --git a/Wox.Core/Configuration/IPortable.cs b/Wox.Core/Configuration/IPortable.cs index e92db779a..74e32fecd 100644 --- a/Wox.Core/Configuration/IPortable.cs +++ b/Wox.Core/Configuration/IPortable.cs @@ -9,5 +9,6 @@ namespace Wox.Core.Configuration void RemoveUninstallerEntry(); bool IsPortableModeEnabled(); void MoveUserDataFolder(string fromLocation, string toLocation); + void VerifyUserDataAfterMove(string fromLocation, string toLocation); } } \ No newline at end of file diff --git a/Wox.Core/Configuration/Portable.cs b/Wox.Core/Configuration/Portable.cs index 92f6fac2d..5f5a60be6 100644 --- a/Wox.Core/Configuration/Portable.cs +++ b/Wox.Core/Configuration/Portable.cs @@ -1,6 +1,7 @@ using Squirrel; using System; using Wox.Infrastructure; +using Wox.Plugin.SharedCommands; namespace Wox.Core.Configuration { @@ -10,6 +11,8 @@ namespace Wox.Core.Configuration private string exeName; private string rootAppDirectory; private UpdateManager portabilityUpdater; + private string roamingDataPath; + private string portableDataPath; public Portable() { @@ -17,6 +20,9 @@ namespace Wox.Core.Configuration exeName = applicationName + ".exe"; rootAppDirectory = Constant.RootDirectory; portabilityUpdater = new UpdateManager(string.Empty, applicationName, rootAppDirectory); + + roamingDataPath = Constant.RoamingDataPath; + portableDataPath = Constant.PortableDataPath; } public void DisablePortableMode() @@ -28,6 +34,7 @@ namespace Wox.Core.Configuration { try { + MoveUserDataFolder(roamingDataPath, portableDataPath); RemoveShortcuts(); RemoveUninstallerEntry(); } @@ -62,7 +69,14 @@ namespace Wox.Core.Configuration public void MoveUserDataFolder(string fromLocation, string toLocation) { - throw new NotImplementedException(); + FilesFolders.Copy(fromLocation, toLocation); + VerifyUserDataAfterMove(fromLocation, toLocation); + FilesFolders.RemoveFolder(fromLocation); + } + + public void VerifyUserDataAfterMove(string fromLocation, string toLocation) + { + FilesFolders.VerifyBothFolderFilesEqual(fromLocation, toLocation); } } } diff --git a/Wox.Infrastructure/Wox.cs b/Wox.Infrastructure/Wox.cs index 61e2b411f..8507ee393 100644 --- a/Wox.Infrastructure/Wox.cs +++ b/Wox.Infrastructure/Wox.cs @@ -7,27 +7,30 @@ namespace Wox.Infrastructure { public static class Constant { - public static string DetermineDataDirectory() - { - string portableDataPath = Path.Combine(ProgramDirectory, "UserData"); - if (Directory.Exists(portableDataPath)) - { - return portableDataPath; - } - else - { - return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Wox); - } - } - public const string Wox = "Wox"; public const string Plugins = "Plugins"; private static readonly Assembly Assembly = Assembly.GetExecutingAssembly(); public static readonly string ProgramDirectory = Directory.GetParent(Assembly.Location.NonNull()).ToString(); + public static readonly string ExecutablePath = Path.Combine(ProgramDirectory, Wox + ".exe"); private static readonly string ApplicationDirectory = Directory.GetParent(ProgramDirectory).ToString(); public static readonly string RootDirectory = Directory.GetParent(ApplicationDirectory).ToString(); - public static readonly string ExecutablePath = Path.Combine(ProgramDirectory, Wox + ".exe"); + + public const string PortableFolderName = "UserData"; + public static string PortableDataPath = Path.Combine(ProgramDirectory, PortableFolderName); + public static string RoamingDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Wox); + public static string DetermineDataDirectory() + { + if (Directory.Exists(PortableDataPath)) + { + return PortableDataPath; + } + else + { + return RoamingDataPath; + } + } + public static readonly string DataDirectory = DetermineDataDirectory(); public static readonly string PluginsDirectory = Path.Combine(DataDirectory, Plugins); public static readonly string PreinstalledDirectory = Path.Combine(ProgramDirectory, Plugins); diff --git a/Wox.Plugin/SharedCommands/FilesFolders.cs b/Wox.Plugin/SharedCommands/FilesFolders.cs new file mode 100644 index 000000000..b3f0a3c8b --- /dev/null +++ b/Wox.Plugin/SharedCommands/FilesFolders.cs @@ -0,0 +1,76 @@ +using System.IO; + +namespace Wox.Plugin.SharedCommands +{ + public static class FilesFolders + { + public static void Copy(this string sourcePath, string targetPath) + { + // Get the subdirectories for the specified directory. + DirectoryInfo dir = new DirectoryInfo(sourcePath); + + if (!dir.Exists) + { + throw new DirectoryNotFoundException( + "Source directory does not exist or could not be found: " + + sourcePath); + } + + DirectoryInfo[] dirs = dir.GetDirectories(); + // If the destination directory doesn't exist, create it. + if (!Directory.Exists(targetPath)) + { + Directory.CreateDirectory(targetPath); + } + + // Get the files in the directory and copy them to the new location. + FileInfo[] files = dir.GetFiles(); + foreach (FileInfo file in files) + { + string temppath = Path.Combine(targetPath, file.Name); + file.CopyTo(temppath, false); + } + + // Recursively copy subdirectories by calling itself on each subdirectory until there are no more to copy + foreach (DirectoryInfo subdir in dirs) + { + string temppath = Path.Combine(targetPath, subdir.Name); + Copy(subdir.FullName, temppath); + } + + } + + public static bool VerifyBothFolderFilesEqual(this string fromPath, string toPath) + { + var fromDir = new DirectoryInfo(fromPath); + var toDir = new DirectoryInfo(toPath); + + if (fromDir.GetFiles().Length != toDir.GetFiles().Length) + return false; + + if (Directory.GetDirectories(fromPath).Length != Directory.GetDirectories(toPath).Length) + return false; + + return true; + } + + public static void RemoveFolder(this string path) + { + try + { + if (Directory.Exists(path)) + Directory.Delete(path, true); + } + catch(PathTooLongException e) + { + //log and update error message to output +#if DEBUG + throw; +#else + throw;// PRODUCTION LOGGING AND CONTINUE + +#endif + } + } + } +} diff --git a/Wox.Plugin/Wox.Plugin.csproj b/Wox.Plugin/Wox.Plugin.csproj index ceba2bbf0..b0b457e98 100644 --- a/Wox.Plugin/Wox.Plugin.csproj +++ b/Wox.Plugin/Wox.Plugin.csproj @@ -65,6 +65,7 @@ +