Skip to content

Commit

Permalink
Add: Create collection backup before overwriting osu collection
Browse files Browse the repository at this point in the history
Closes #51
  • Loading branch information
Piotrekol committed Feb 16, 2022
1 parent 90ba0f0 commit 07ab517
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion App/SidePanelActionsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using Common;
using GuiComponents.Interfaces;
using System.ComponentModel;
using System.Security.Cryptography;

namespace App
{
Expand Down Expand Up @@ -523,14 +524,15 @@ 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);
}

if (_userDialogs.YesNoMessageBox("Are you sure that you want to overwrite your existing osu! collection?",
"Are you sure?", MessageBoxType.Question))
{
await BeforeCollectionSave(Initalizer.LoadedCollections);
BackupOsuCollection();
_osuFileIo.CollectionLoader.SaveOsuCollection(Initalizer.LoadedCollections, fileLocation);
_userDialogs.OkMessageBox("Collections saved.", "Info", MessageBoxType.Success);
}
Expand All @@ -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());
Expand Down

0 comments on commit 07ab517

Please sign in to comment.