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-12-09 22:41:07 +08:00
|
|
|
using System.Collections.Generic;
|
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 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.Notifications;
|
2022-12-09 22:41:07 +08:00
|
|
|
using osu.Game.Utils;
|
2022-11-17 22:38:24 +08:00
|
|
|
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-12-09 22:57:03 +08:00
|
|
|
public abstract class LegacyModelExporter<TModel>
|
2022-11-21 17:58:01 +08:00
|
|
|
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;
|
2022-12-09 22:57:03 +08:00
|
|
|
private readonly 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-12-11 17:30:24 +08:00
|
|
|
private string filename = string.Empty;
|
|
|
|
public Action<Notification>? PostNotification { get; set; }
|
2022-12-09 22:57:03 +08:00
|
|
|
|
2022-12-11 17:30:24 +08:00
|
|
|
protected LegacyModelExporter(Storage storage, RealmAccess realm)
|
2022-11-17 22:38:24 +08:00
|
|
|
{
|
2022-12-09 22:57:03 +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-17 22:38:24 +08:00
|
|
|
}
|
|
|
|
|
2022-11-21 18:04:05 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Export the model to default folder.
|
|
|
|
/// </summary>
|
2022-12-09 23:43:03 +08:00
|
|
|
/// <param name="model">The model should export.</param>
|
2022-11-21 18:04:05 +08:00
|
|
|
/// <returns></returns>
|
2022-12-09 23:43:03 +08:00
|
|
|
public async Task ExportAsync(TModel model)
|
2022-11-17 22:38:24 +08:00
|
|
|
{
|
2022-12-09 23:43:03 +08:00
|
|
|
string itemFilename = model.GetDisplayString().GetValidFilename();
|
|
|
|
IEnumerable<string> existingExports = exportStorage.GetFiles("", $"{itemFilename}*{FileExtension}");
|
2022-12-11 17:30:24 +08:00
|
|
|
filename = NamingUtils.GetNextBestFilename(existingExports, $"{itemFilename}{FileExtension}");
|
2022-12-15 21:45:36 +08:00
|
|
|
bool success;
|
2022-12-09 22:57:03 +08:00
|
|
|
|
2022-12-09 23:43:03 +08:00
|
|
|
using (var stream = exportStorage.CreateFileSafely(filename))
|
|
|
|
{
|
2022-12-15 21:45:36 +08:00
|
|
|
success = await ExportToStreamAsync(model, stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
exportStorage.Delete(filename);
|
2022-11-21 17:58:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-21 18:04:05 +08:00
|
|
|
/// <summary>
|
2022-12-11 17:30:24 +08:00
|
|
|
/// Export model to stream.
|
2022-11-21 18:04:05 +08:00
|
|
|
/// </summary>
|
2022-11-27 08:58:54 +08:00
|
|
|
/// <param name="model">The medel which have <see cref="IHasGuidPrimaryKey"/>.</param>
|
2022-11-21 18:04:05 +08:00
|
|
|
/// <param name="stream">The stream to export.</param>
|
2022-12-15 21:45:36 +08:00
|
|
|
/// <returns>Whether the export was successful</returns>
|
|
|
|
public async Task<bool> ExportToStreamAsync(TModel model, Stream stream)
|
2022-11-21 17:58:01 +08:00
|
|
|
{
|
2022-12-11 17:30:24 +08:00
|
|
|
ProgressNotification notification = new ProgressNotification
|
|
|
|
{
|
|
|
|
State = ProgressNotificationState.Active,
|
|
|
|
Text = "Exporting...",
|
|
|
|
CompletionText = "Export completed"
|
|
|
|
};
|
|
|
|
notification.CompletionClickAction += () => exportStorage.PresentFileExternally(filename);
|
|
|
|
PostNotification?.Invoke(notification);
|
|
|
|
|
2022-11-27 08:58:54 +08:00
|
|
|
Guid id = model.ID;
|
2022-12-15 21:45:36 +08:00
|
|
|
return await Task.Run(() =>
|
2022-11-17 22:38:24 +08:00
|
|
|
{
|
2022-11-19 00:02:35 +08:00
|
|
|
RealmAccess.Run(r =>
|
2022-11-17 22:38:24 +08:00
|
|
|
{
|
2022-11-27 08:58:54 +08:00
|
|
|
TModel refetchModel = r.Find<TModel>(id);
|
2022-12-11 17:30:24 +08:00
|
|
|
ExportToStream(refetchModel, stream, notification);
|
2022-11-17 22:38:24 +08:00
|
|
|
});
|
2022-12-15 21:45:36 +08:00
|
|
|
}, notification.CancellationToken).ContinueWith(t =>
|
2022-12-11 17:30:24 +08:00
|
|
|
{
|
2022-12-15 21:45:36 +08:00
|
|
|
if (t.IsCanceled)
|
2022-12-11 17:30:24 +08:00
|
|
|
{
|
2022-12-15 21:45:36 +08:00
|
|
|
return false;
|
2022-12-11 17:30:24 +08:00
|
|
|
}
|
|
|
|
|
2022-12-15 21:45:36 +08:00
|
|
|
if (t.IsFaulted)
|
2022-12-11 17:30:24 +08:00
|
|
|
{
|
2022-12-15 21:45:36 +08:00
|
|
|
notification.State = ProgressNotificationState.Cancelled;
|
|
|
|
Logger.Error(t.Exception, "An error occurred while exporting");
|
|
|
|
return false;
|
2022-12-11 17:30:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
notification.CompletionText = "Export Complete, Click to open the folder";
|
|
|
|
notification.State = ProgressNotificationState.Completed;
|
2022-12-15 21:45:36 +08:00
|
|
|
return true;
|
2022-12-11 17:30:24 +08:00
|
|
|
});
|
2022-11-19 00:02:35 +08:00
|
|
|
}
|
|
|
|
|
2022-12-11 15:55:44 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Exports an item to Stream.
|
|
|
|
/// Override if custom export method is required.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="model">The item to export.</param>
|
|
|
|
/// <param name="outputStream">The output stream to export to.</param>
|
2022-12-11 17:30:24 +08:00
|
|
|
/// <param name="notification">The notification will displayed to the user</param>
|
2022-12-15 20:39:48 +08:00
|
|
|
protected abstract void ExportToStream(TModel model, Stream outputStream, ProgressNotification notification);
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract class LegacyArchiveExporter<TModel> : LegacyModelExporter<TModel>
|
|
|
|
where TModel : RealmObject, IHasNamedFiles, IHasGuidPrimaryKey
|
|
|
|
{
|
2022-12-15 21:45:36 +08:00
|
|
|
private bool canCancel = true;
|
|
|
|
|
2022-12-15 20:39:48 +08:00
|
|
|
protected LegacyArchiveExporter(Storage storage, RealmAccess realm)
|
|
|
|
: base(storage, realm)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void ExportToStream(TModel model, Stream outputStream, ProgressNotification notification) => exportZipArchive(model, outputStream, notification);
|
2022-11-27 08:58:54 +08:00
|
|
|
|
2022-12-11 15:54:20 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Exports an item to Stream as a legacy (.zip based) package.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="model">The item to export.</param>
|
|
|
|
/// <param name="outputStream">The output stream to export to.</param>
|
2022-12-11 17:30:24 +08:00
|
|
|
/// <param name="notification">The notification will displayed to the user</param>
|
|
|
|
private void exportZipArchive(TModel model, Stream outputStream, ProgressNotification notification)
|
2022-11-19 00:02:35 +08:00
|
|
|
{
|
2022-12-15 21:45:36 +08:00
|
|
|
try
|
2022-11-17 22:38:24 +08:00
|
|
|
{
|
2022-12-15 21:45:36 +08:00
|
|
|
notification.CancelRequested += () => canCancel;
|
2022-11-21 17:58:01 +08:00
|
|
|
|
2022-12-15 21:45:36 +08:00
|
|
|
using (var archive = ZipArchive.Create())
|
2022-11-21 17:58:01 +08:00
|
|
|
{
|
2022-12-15 21:45:36 +08:00
|
|
|
float i = 0;
|
2022-11-21 17:58:01 +08:00
|
|
|
|
2022-12-15 21:45:36 +08:00
|
|
|
foreach (var file in model.Files)
|
|
|
|
{
|
|
|
|
notification.CancellationToken.ThrowIfCancellationRequested();
|
2022-11-21 17:58:01 +08:00
|
|
|
|
2022-12-15 21:45:36 +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()})";
|
|
|
|
}
|
|
|
|
|
|
|
|
notification.Text = "Saving Zip Archive...";
|
|
|
|
canCancel = false;
|
|
|
|
archive.SaveTo(outputStream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
{
|
|
|
|
Logger.Log("Export operat canceled");
|
|
|
|
throw;
|
2022-11-19 00:02:35 +08:00
|
|
|
}
|
2022-11-21 17:58:01 +08:00
|
|
|
}
|
2022-11-17 22:38:24 +08:00
|
|
|
}
|
|
|
|
}
|