1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 04:07:25 +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.

127 lines
4.1 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.Graphics;
2022-11-21 18:00:10 +08:00
using osu.Framework.Logging;
2022-11-17 22:38:24 +08:00
using osu.Framework.Platform;
using osu.Game.Extensions;
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>
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; }
protected Storage UserFileStorage;
protected Storage ExportStorage;
2022-11-17 22:38:24 +08:00
protected RealmAccess RealmAccess;
2022-11-17 22:38:24 +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
private bool canCancel = true;
2022-11-19 00:02:35 +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;
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
}
public async Task ExportAsync(RealmObject item)
2022-11-17 22:38:24 +08:00
{
if (item is TModel model)
{
Filename = $"{model.GetDisplayString().GetValidFilename()}{FileExtension}";
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
{
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);
}
private void createZipArchive(TModel model, Stream outputStream)
2022-11-19 00:02:35 +08:00
{
using (var archive = ZipArchive.Create())
2022-11-17 22:38:24 +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-17 22:38:24 +08:00
protected void OnComplete(Task t)
{
2022-11-19 00:02:35 +08:00
if (t.IsFaulted)
{
notification.State = ProgressNotificationState.Cancelled;
2022-11-21 18:00:10 +08:00
Logger.Error(t.Exception, "An error occurred while exporting");
2022-11-19 00:02:35 +08:00
return;
}
if (notification.CancellationToken.IsCancellationRequested)
2022-11-19 00:02:35 +08:00
{
return;
}
2022-11-17 22:38:24 +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
}
}
}