Skip to content

Commit

Permalink
Restore backup file check
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Sep 16, 2024
1 parent 8505f2d commit ff6716b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 3 deletions.
18 changes: 18 additions & 0 deletions v2rayN/ServiceLib/Common/FileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ public static bool ZipExtractToFile(string fileName, string toPath, string ignor
return true;
}

public static List<string>? GetFilesFromZip(string fileName)
{
if (!File.Exists(fileName))
{
return null;
}
try
{
using ZipArchive archive = ZipFile.OpenRead(fileName);
return archive.Entries.Select(entry => entry.FullName).ToList();
}
catch (Exception ex)
{
Logging.SaveLog(ex.Message, ex);
return null;
}
}

public static bool CreateFromDirectory(string sourceDirectoryName, string destinationArchiveFileName)
{
try
Expand Down
9 changes: 9 additions & 0 deletions v2rayN/ServiceLib/Resx/ResUI.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions v2rayN/ServiceLib/Resx/ResUI.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1315,4 +1315,7 @@
<data name="LvWebDavDirName" xml:space="preserve">
<value>Remote folder name (optional)</value>
</data>
<data name="LocalRestoreInvalidZipTips" xml:space="preserve">
<value>Invalid backup file</value>
</data>
</root>
3 changes: 3 additions & 0 deletions v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1312,4 +1312,7 @@
<data name="LvWebDavDirName" xml:space="preserve">
<value>远程文件夹名称(可选)</value>
</data>
<data name="LocalRestoreInvalidZipTips" xml:space="preserve">
<value>无效备份文件</value>
</data>
</root>
3 changes: 3 additions & 0 deletions v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1192,4 +1192,7 @@
<data name="LvWebDavDirName" xml:space="preserve">
<value>遠端資料夾名稱(可選)</value>
</data>
<data name="LocalRestoreInvalidZipTips" xml:space="preserve">
<value>無效備份文件</value>
</data>
</root>
16 changes: 13 additions & 3 deletions v2rayN/ServiceLib/ViewModels/BackupAndRestoreViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ namespace ServiceLib.ViewModels
{
public class BackupAndRestoreViewModel : MyReactiveObject
{
private readonly string _guiConfigs = "guiConfigs";
private static string BackupFileName => $"backup_{DateTime.Now:yyyyMMddHHmmss}.zip";

public ReactiveCommand<Unit, Unit> RemoteBackupCmd { get; }
public ReactiveCommand<Unit, Unit> RemoteRestoreCmd { get; }
public ReactiveCommand<Unit, Unit> WebDavCheckCmd { get; }
Expand Down Expand Up @@ -65,7 +68,7 @@ private async Task WebDavCheck()
private async Task RemoteBackup()
{
DisplayOperationMsg();
var fileName = Utils.GetBackupPath($"backup_{DateTime.Now:yyyyMMddHHmmss}.zip");
var fileName = Utils.GetBackupPath(BackupFileName);
var result = await CreateZipFileFromDirectory(fileName);
if (result)
{
Expand Down Expand Up @@ -122,9 +125,16 @@ public async Task LocalRestore(string fileName)
{
return;
}
//check
var lstFiles = FileManager.GetFilesFromZip(fileName);
if (lstFiles is null || !lstFiles.Where(t => t.Contains(_guiConfigs)).Any())
{
DisplayOperationMsg(ResUI.LocalRestoreInvalidZipTips);
return;
}

//backup first
var fileBackup = Utils.GetBackupPath($"backup_{DateTime.Now:yyyyMMddHHmmss}.zip");
var fileBackup = Utils.GetBackupPath(BackupFileName);
var result = await CreateZipFileFromDirectory(fileBackup);
if (result)
{
Expand All @@ -145,7 +155,7 @@ private async Task<bool> CreateZipFileFromDirectory(string fileName)

var configDir = Utils.GetConfigPath();
var configDirZipTemp = Utils.GetTempPath($"v2rayN_{DateTime.Now:yyyyMMddHHmmss}");
var configDirTemp = Path.Combine(configDirZipTemp, "guiConfigs");
var configDirTemp = Path.Combine(configDirZipTemp, _guiConfigs);

await Task.Run(() => FileManager.CopyDirectory(configDir, configDirTemp, false, "cache.db"));
var ret = await Task.Run(() => FileManager.CreateFromDirectory(configDirZipTemp, fileName));
Expand Down

0 comments on commit ff6716b

Please sign in to comment.