2022-11-17 22:38:24 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
using System;
|
2022-11-19 00:02:35 +08:00
|
|
|
using System.IO;
|
2022-11-17 22:38:24 +08:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
2022-11-21 17:58:01 +08:00
|
|
|
using osu.Framework.Graphics;
|
2022-11-17 22:38:24 +08:00
|
|
|
using osu.Framework.Platform;
|
|
|
|
using osu.Game.Extensions;
|
2022-11-21 17:58:01 +08:00
|
|
|
using osu.Game.Overlays;
|
2022-11-17 22:38:24 +08:00
|
|
|
using osu.Game.Overlays.Notifications;
|
|
|
|
using Realms;
|
|
|
|
using SharpCompress.Archives.Zip;
|
|
|
|
|
|
|
|
namespace osu.Game.Database
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// A class which handles exporting legacy user data of a single type from osu-stable.
|
|
|
|
/// </summary>
|
2022-11-21 17:58:01 +08:00
|
|
|
public abstract class LegacyModelExporter<TModel> : Component
|
|
|
|
where TModel : RealmObject, IHasNamedFiles, IHasGuidPrimaryKey
|
2022-11-17 22:38:24 +08:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// The file extension for exports (including the leading '.').
|
|
|
|
/// </summary>
|
|
|
|
protected abstract string FileExtension { get; }
|
|
|
|
|
2022-11-21 17:58:01 +08:00
|
|
|
protected Storage UserFileStorage;
|
|
|
|
protected Storage ExportStorage;
|
2022-11-17 22:38:24 +08:00
|
|
|
|
2022-11-21 17:58:01 +08:00
|
|
|
protected RealmAccess RealmAccess;
|
2022-11-17 22:38:24 +08:00
|
|
|
|
2022-11-21 17:58:01 +08:00
|
|
|
private readonly ProgressNotification notification;
|
2022-11-17 22:38:24 +08:00
|
|
|
|
2022-11-19 00:02:35 +08:00
|
|
|
protected string Filename = null!;
|
2022-11-17 22:38:24 +08:00
|
|
|
|
2022-11-21 17:58:01 +08:00
|
|
|
private bool canCancel = true;
|
2022-11-19 00:02:35 +08:00
|
|
|
|
2022-11-21 17:58:01 +08:00
|
|
|
protected LegacyModelExporter(Storage storage, RealmAccess realm, INotificationOverlay? notifications = null)
|
2022-11-17 22:38:24 +08:00
|
|
|
{
|
2022-11-19 00:02:35 +08:00
|
|
|
ExportStorage = storage.GetStorageForDirectory(@"exports");
|
2022-11-17 22:38:24 +08:00
|
|
|
UserFileStorage = storage.GetStorageForDirectory(@"files");
|
2022-11-19 00:02:35 +08:00
|
|
|
RealmAccess = realm;
|
2022-11-21 17:58:01 +08:00
|
|
|
|
|
|
|
notification = new ProgressNotification
|
|
|
|
{
|
|
|
|
State = ProgressNotificationState.Active,
|
|
|
|
Text = "Exporting...",
|
|
|
|
CompletionText = "Export completed"
|
|
|
|
};
|
|
|
|
notification.CancelRequested += () => canCancel;
|
|
|
|
|
|
|
|
notifications?.Post(notification);
|
2022-11-17 22:38:24 +08:00
|
|
|
}
|
|
|
|
|
2022-11-21 17:58:01 +08:00
|
|
|
public async Task ExportAsync(RealmObject item)
|
2022-11-17 22:38:24 +08:00
|
|
|
{
|
2022-11-21 17:58:01 +08:00
|
|
|
if (item is TModel model)
|
|
|
|
{
|
|
|
|
Filename = $"{model.GetDisplayString().GetValidFilename()}{FileExtension}";
|
2022-11-19 11:57:56 +08:00
|
|
|
|
2022-11-21 17:58:01 +08:00
|
|
|
using (var stream = ExportStorage.CreateFileSafely(Filename))
|
|
|
|
{
|
|
|
|
await ExportToStreamAsync(model, stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual async Task ExportToStreamAsync(TModel uuid, Stream stream)
|
|
|
|
{
|
2022-11-17 22:38:24 +08:00
|
|
|
Guid id = uuid.ID;
|
|
|
|
await Task.Run(() =>
|
|
|
|
{
|
2022-11-19 00:02:35 +08:00
|
|
|
RealmAccess.Run(r =>
|
2022-11-17 22:38:24 +08:00
|
|
|
{
|
2022-11-21 17:58:01 +08:00
|
|
|
TModel model = r.Find<TModel>(id);
|
|
|
|
createZipArchive(model, stream);
|
2022-11-17 22:38:24 +08:00
|
|
|
});
|
2022-11-19 00:02:35 +08:00
|
|
|
}).ContinueWith(OnComplete);
|
|
|
|
}
|
|
|
|
|
2022-11-21 17:58:01 +08:00
|
|
|
private void createZipArchive(TModel model, Stream outputStream)
|
2022-11-19 00:02:35 +08:00
|
|
|
{
|
2022-11-21 17:58:01 +08:00
|
|
|
using (var archive = ZipArchive.Create())
|
2022-11-17 22:38:24 +08:00
|
|
|
{
|
2022-11-21 17:58:01 +08:00
|
|
|
float i = 0;
|
|
|
|
|
|
|
|
foreach (var file in model.Files)
|
|
|
|
{
|
|
|
|
if (notification.CancellationToken.IsCancellationRequested) return;
|
|
|
|
|
|
|
|
archive.AddEntry(file.Filename, UserFileStorage.GetStream(file.File.GetStoragePath()));
|
|
|
|
i++;
|
|
|
|
notification.Progress = i / model.Files.Count();
|
|
|
|
notification.Text = $"Exporting... ({i}/{model.Files.Count()})";
|
|
|
|
}
|
|
|
|
|
|
|
|
notification.Text = "Saving Zip Archive...";
|
|
|
|
canCancel = false;
|
|
|
|
archive.SaveTo(outputStream);
|
2022-11-19 00:02:35 +08:00
|
|
|
}
|
2022-11-21 17:58:01 +08:00
|
|
|
}
|
2022-11-17 22:38:24 +08:00
|
|
|
|
2022-11-21 17:58:01 +08:00
|
|
|
protected void OnComplete(Task t)
|
|
|
|
{
|
2022-11-19 00:02:35 +08:00
|
|
|
if (t.IsFaulted)
|
|
|
|
{
|
2022-11-21 17:58:01 +08:00
|
|
|
notification.State = ProgressNotificationState.Cancelled;
|
2022-11-19 00:02:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-21 17:58:01 +08:00
|
|
|
if (notification.CancellationToken.IsCancellationRequested)
|
2022-11-19 00:02:35 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-11-17 22:38:24 +08:00
|
|
|
|
2022-11-21 17:58:01 +08:00
|
|
|
notification.CompletionText = "Export Complete, Click to open the folder";
|
|
|
|
notification.CompletionClickAction += () => ExportStorage.PresentFileExternally(Filename);
|
|
|
|
notification.State = ProgressNotificationState.Completed;
|
2022-11-17 22:38:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|