Add UserData operations- Copy, verify, remove

This commit is contained in:
Jeremy Wu 2020-02-21 22:06:04 +11:00
parent 7385243c1d
commit d55560ea97
5 changed files with 110 additions and 15 deletions

View file

@ -9,5 +9,6 @@ namespace Wox.Core.Configuration
void RemoveUninstallerEntry();
bool IsPortableModeEnabled();
void MoveUserDataFolder(string fromLocation, string toLocation);
void VerifyUserDataAfterMove(string fromLocation, string toLocation);
}
}

View file

@ -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);
}
}
}

View file

@ -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);

View file

@ -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
}
}
}
}

View file

@ -65,6 +65,7 @@
<Compile Include="Query.cs" />
<Compile Include="Result.cs" />
<Compile Include="ActionContext.cs" />
<Compile Include="SharedCommands\FilesFolders.cs" />
<Compile Include="SharedCommands\SearchWeb.cs" />
<Compile Include="SharedCommands\ShellCommand.cs" />
</ItemGroup>