2023-02-19 00:18:27 +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.
|
|
|
|
|
|
2023-02-19 01:06:07 +08:00
|
|
|
|
using System;
|
2023-02-19 00:18:27 +08:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
2023-02-23 19:20:54 +08:00
|
|
|
|
using osu.Framework.Logging;
|
2023-02-19 00:18:27 +08:00
|
|
|
|
using osu.Framework.Platform;
|
|
|
|
|
using osu.Game.Extensions;
|
|
|
|
|
using osu.Game.Overlays.Notifications;
|
|
|
|
|
using Realms;
|
|
|
|
|
using SharpCompress.Common;
|
|
|
|
|
using SharpCompress.Writers;
|
|
|
|
|
using SharpCompress.Writers.Zip;
|
2023-02-23 19:20:54 +08:00
|
|
|
|
using Logger = osu.Framework.Logging.Logger;
|
2023-02-19 00:18:27 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Database
|
|
|
|
|
{
|
|
|
|
|
public abstract class LegacyArchiveExporter<TModel> : LegacyModelExporter<TModel>
|
|
|
|
|
where TModel : RealmObject, IHasNamedFiles, IHasGuidPrimaryKey
|
|
|
|
|
{
|
|
|
|
|
protected LegacyArchiveExporter(Storage storage, RealmAccess realm)
|
|
|
|
|
: base(storage, realm)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 21:11:52 +08:00
|
|
|
|
protected override void ExportToStream(TModel model, Stream outputStream, ProgressNotification? notification, CancellationToken cancellationToken = default)
|
2023-02-19 00:18:27 +08:00
|
|
|
|
=> exportZipArchive(model, outputStream, notification, cancellationToken);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Exports an item to Stream as a legacy (.zip based) package.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model">The model will be exported.</param>
|
|
|
|
|
/// <param name="outputStream">The output stream to export to.</param>
|
|
|
|
|
/// <param name="notification">The notification will displayed to the user</param>
|
|
|
|
|
/// <param name="cancellationToken">The Cancellation token that can cancel the exporting.</param>
|
2023-04-09 21:11:52 +08:00
|
|
|
|
private void exportZipArchive(TModel model, Stream outputStream, ProgressNotification? notification, CancellationToken cancellationToken = default)
|
2023-02-19 00:18:27 +08:00
|
|
|
|
{
|
2023-02-19 01:06:07 +08:00
|
|
|
|
try
|
2023-02-19 00:18:27 +08:00
|
|
|
|
{
|
2023-02-19 01:41:08 +08:00
|
|
|
|
using var writer = new ZipWriter(outputStream, new ZipWriterOptions(CompressionType.Deflate));
|
2023-02-19 01:24:07 +08:00
|
|
|
|
|
|
|
|
|
float i = 0;
|
|
|
|
|
bool fileMissing = false;
|
|
|
|
|
|
2023-02-19 01:06:07 +08:00
|
|
|
|
foreach (var file in model.Files)
|
2023-02-19 00:18:27 +08:00
|
|
|
|
{
|
2023-02-19 01:06:07 +08:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
using (var stream = UserFileStorage.GetStream(file.File.GetStoragePath()))
|
2023-02-19 00:18:27 +08:00
|
|
|
|
{
|
2023-02-19 01:06:07 +08:00
|
|
|
|
// Sometimes we cannot find the file(probably deleted by the user), so we handle this and post a error.
|
|
|
|
|
if (stream == null)
|
2023-02-19 00:18:27 +08:00
|
|
|
|
{
|
2023-02-19 01:06:07 +08:00
|
|
|
|
// Only pop up once to prevent spam.
|
|
|
|
|
if (!fileMissing)
|
2023-02-19 00:18:27 +08:00
|
|
|
|
{
|
2023-02-23 19:20:54 +08:00
|
|
|
|
Logger.Log("Some of model files are missing, they will not be included in the archive", LoggingTarget.Database, LogLevel.Error);
|
2023-02-19 01:06:07 +08:00
|
|
|
|
fileMissing = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
writer.Write(file.Filename, stream);
|
2023-02-19 00:18:27 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-19 01:06:07 +08:00
|
|
|
|
|
|
|
|
|
i++;
|
2023-04-09 21:11:52 +08:00
|
|
|
|
|
|
|
|
|
if (notification != null)
|
|
|
|
|
{
|
|
|
|
|
notification.Progress = i / model.Files.Count();
|
|
|
|
|
notification.Text = $"Exporting... ({i}/{model.Files.Count()})";
|
|
|
|
|
}
|
2023-02-19 00:18:27 +08:00
|
|
|
|
}
|
2023-02-19 01:06:07 +08:00
|
|
|
|
}
|
|
|
|
|
catch (ObjectDisposedException)
|
|
|
|
|
{
|
2023-02-19 01:17:24 +08:00
|
|
|
|
// outputStream may close before writing when request cancel.
|
2023-02-19 01:06:07 +08:00
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
|
|
|
return;
|
2023-02-19 00:18:27 +08:00
|
|
|
|
|
2023-02-19 01:06:07 +08:00
|
|
|
|
throw;
|
2023-02-19 00:18:27 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|