1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-19 15:32:55 +08:00
osu-lazer/osu.Game/Database/LegacyModelExporter.cs

138 lines
4.6 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-21 18:05:26 +08:00
private string filename = "";
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
}
2022-11-21 18:04:05 +08:00
/// <summary>
/// Export the model to default folder.
/// </summary>
/// <param name="item">The model should export.</param>
/// <returns></returns>
public async Task ExportAsync(RealmObject item)
2022-11-17 22:38:24 +08:00
{
if (item is TModel model)
{
2022-11-21 18:05:26 +08:00
filename = $"{model.GetDisplayString().GetValidFilename()}{FileExtension}";
2022-11-21 18:05:26 +08:00
using (var stream = ExportStorage.CreateFileSafely(filename))
{
await ExportToStreamAsync(model, stream);
}
}
}
2022-11-21 18:04:05 +08:00
/// <summary>
/// Export te model corresponding to uuid to given stream.
/// </summary>
/// <param name="uuid">The medel which have <see cref="IHasGuidPrimaryKey"/>.</param>
/// <param name="stream">The stream to export.</param>
/// <returns></returns>
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";
2022-11-21 18:05:26 +08:00
notification.CompletionClickAction += () => ExportStorage.PresentFileExternally(filename);
notification.State = ProgressNotificationState.Completed;
2022-11-17 22:38:24 +08:00
}
}
}