1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 03:27:24 +08:00
osu-lazer/osu.Game/Database/LegacyModelExporter.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

128 lines
4.3 KiB
C#
Raw Normal View History

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;
using osu.Framework.Platform;
using osu.Game.Extensions;
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>
public abstract class LegacyModelExporter<TModel>
where TModel : RealmObject
{
/// <summary>
/// The file extension for exports (including the leading '.').
/// </summary>
protected abstract string FileExtension { get; }
protected readonly Storage UserFileStorage;
2022-11-19 00:02:35 +08:00
protected readonly Storage ExportStorage;
2022-11-17 22:38:24 +08:00
2022-11-19 00:02:35 +08:00
protected readonly RealmAccess RealmAccess;
2022-11-17 22:38:24 +08:00
2022-11-19 00:02:35 +08:00
protected 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-19 00:02:35 +08:00
protected Stream? OutputStream;
2022-11-17 22:38:24 +08:00
2022-11-19 00:02:35 +08:00
protected bool ShouldDisposeStream;
protected LegacyModelExporter(Storage storage, RealmAccess realm, ProgressNotification notification, Stream? stream = 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
Notification = notification;
RealmAccess = realm;
OutputStream = stream;
ShouldDisposeStream = false;
2022-11-17 22:38:24 +08:00
}
2022-11-19 11:34:35 +08:00
/// <summary>
/// Export model to <see cref="OutputStream"/>
/// if <see cref="OutputStream"/> is null, model will export to default folder.
/// </summary>
/// <param name="uuid">The model which have Guid.</param>
/// <returns></returns>
2022-11-17 22:38:24 +08:00
{
bool canCancel = true;
Notification.CancelRequested += () => canCancel;
2022-11-21 16:42:11 +08:00
public virtual async Task ExportAsync(IHasGuidPrimaryKey uuid)
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
{
if (r.Find<TModel>(id) is IHasNamedFiles model)
{
2022-11-19 00:02:35 +08:00
Filename = $"{model.GetDisplayString().GetValidFilename()}{FileExtension}";
2022-11-17 22:38:24 +08:00
}
else
{
return;
}
2022-11-19 00:02:35 +08:00
if (OutputStream == null)
2022-11-17 22:38:24 +08:00
{
2022-11-19 00:02:35 +08:00
OutputStream = ExportStorage.CreateFileSafely(Filename);
ShouldDisposeStream = true;
}
using (var archive = ZipArchive.Create())
{
float i = 0;
foreach (var file in model.Files)
2022-11-17 22:38:24 +08:00
{
2022-11-19 00:02:35 +08:00
if (Notification.CancellationToken.IsCancellationRequested) return;
2022-11-17 22:38:24 +08:00
2022-11-19 00:02:35 +08:00
archive.AddEntry(file.Filename, UserFileStorage.GetStream(file.File.GetStoragePath()));
i++;
Notification.Progress = i / model.Files.Count();
Notification.Text = $"Exporting... ({i}/{model.Files.Count()})";
2022-11-17 22:38:24 +08:00
}
2022-11-19 00:02:35 +08:00
Notification.Text = "Saving Zip Archive...";
canCancel = false;
2022-11-19 00:02:35 +08:00
archive.SaveTo(OutputStream);
2022-11-17 22:38:24 +08:00
}
});
2022-11-19 00:02:35 +08:00
}).ContinueWith(OnComplete);
}
protected void OnComplete(Task t)
{
if (ShouldDisposeStream)
2022-11-17 22:38:24 +08:00
{
2022-11-19 00:02:35 +08:00
OutputStream?.Dispose();
}
2022-11-17 22:38:24 +08:00
2022-11-19 00:02:35 +08:00
if (t.IsFaulted)
{
Notification.State = ProgressNotificationState.Cancelled;
return;
}
if (Notification.CancellationToken.IsCancellationRequested)
{
return;
}
2022-11-17 22:38:24 +08:00
2022-11-19 00:02:35 +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
}
}
}