From 07ab5177738b1cac46e11d7fc80f9177d1e90cb2 Mon Sep 17 00:00:00 2001 From: Piotrekol <4990365+Piotrekol@users.noreply.github.com> Date: Wed, 16 Feb 2022 20:47:30 +0100 Subject: [PATCH] Add: Create collection backup before overwriting osu collection Closes #51 --- App/SidePanelActionsHandler.cs | 51 +++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/App/SidePanelActionsHandler.cs b/App/SidePanelActionsHandler.cs index 12a966e..7ca3c51 100644 --- a/App/SidePanelActionsHandler.cs +++ b/App/SidePanelActionsHandler.cs @@ -26,6 +26,7 @@ using Common; using GuiComponents.Interfaces; using System.ComponentModel; +using System.Security.Cryptography; namespace App { @@ -523,7 +524,7 @@ private async void SaveDefaultCollection(object sender, object data = null) // access denied if (ex.NativeErrorCode != 5) throw; - + _userDialogs.OkMessageBox("Could not determine if osu! is running due to a permissions error.", "Warning", MessageBoxType.Warning); } @@ -531,6 +532,7 @@ private async void SaveDefaultCollection(object sender, object data = null) "Are you sure?", MessageBoxType.Question)) { await BeforeCollectionSave(Initalizer.LoadedCollections); + BackupOsuCollection(); _osuFileIo.CollectionLoader.SaveOsuCollection(Initalizer.LoadedCollections, fileLocation); _userDialogs.OkMessageBox("Collections saved.", "Info", MessageBoxType.Success); } @@ -540,6 +542,53 @@ private async void SaveDefaultCollection(object sender, object data = null) } } + private void BackupOsuCollection() + { + var backupFolder = Path.Combine(Initalizer.OsuDirectory, "collectionBackups"); + if (!Directory.Exists(backupFolder)) + Directory.CreateDirectory(backupFolder); + + var sourceCollectionFile = Path.Combine(Initalizer.OsuDirectory, "collection.db"); + if (!File.Exists(sourceCollectionFile)) + return; + + var destinationCollectionFile = Path.Combine(Initalizer.OsuDirectory, "collectionBackups", $"collection_{CalculateMD5(sourceCollectionFile)}.db"); + if (File.Exists(destinationCollectionFile)) + { + //Just update file save date to indicate latest collection version + File.SetLastWriteTime(destinationCollectionFile, DateTime.Now); + return; + } + + CleanupBackups(); + File.Copy(sourceCollectionFile, destinationCollectionFile); + + string CalculateMD5(string filename) + { + using (var md5 = MD5.Create()) + { + using (var stream = File.OpenRead(filename)) + { + var hash = md5.ComputeHash(stream); + return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); + } + } + } + + void CleanupBackups() + { + var deleteDateThreshold = DateTime.UtcNow.AddDays(-30); + var collectionFilePaths = Directory.GetFiles(backupFolder, "*.db", SearchOption.TopDirectoryOnly); + var collectionFiles = collectionFilePaths.Select(f => new FileInfo(f)) + .Where(f => f.LastWriteTimeUtc < deleteDateThreshold); + + foreach (var collectionFile in collectionFiles) + { + collectionFile.Delete(); + } + } + } + private void ClearCollections(object sender, object data = null) { _collectionEditor.EditCollection(CollectionEditArgs.ClearCollections());