osu/osu.Game/Database/LegacyArchiveExporter.cs

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

92 lines
3.6 KiB
C#
Raw Normal View History

2023-02-18 16:18:27 +00: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-18 17:06:07 +00:00
using System;
2023-02-18 16:18:27 +00:00
using System.IO;
using System.Linq;
using System.Threading;
2023-02-23 11:20:54 +00:00
using osu.Framework.Logging;
2023-02-18 16:18:27 +00: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 11:20:54 +00:00
using Logger = osu.Framework.Logging.Logger;
2023-02-18 16:18:27 +00:00
namespace osu.Game.Database
{
/// <summary>
/// An exporter which handles the common scenario of exporting a model to a zip-based archive, usually with a custom file extension.
/// </summary>
public abstract class LegacyArchiveExporter<TModel> : LegacyExporter<TModel>
2023-02-18 16:18:27 +00:00
where TModel : RealmObject, IHasNamedFiles, IHasGuidPrimaryKey
{
protected LegacyArchiveExporter(Storage storage)
: base(storage)
2023-02-18 16:18:27 +00:00
{
}
2023-04-09 13:11:52 +00:00
protected override void ExportToStream(TModel model, Stream outputStream, ProgressNotification? notification, CancellationToken cancellationToken = default)
2023-02-18 16:18:27 +00:00
=> exportZipArchive(model, outputStream, notification, cancellationToken);
/// <summary>
/// Exports an item to Stream as a legacy (.zip based) package.
/// </summary>
/// <param name="model">The model to be exported.</param>
2023-02-18 16:18:27 +00:00
/// <param name="outputStream">The output stream to export to.</param>
/// <param name="notification">An optional target notification to update with ongoing export progress.</param>
/// <param name="cancellationToken">The cancellation token.</param>
2023-04-09 13:11:52 +00:00
private void exportZipArchive(TModel model, Stream outputStream, ProgressNotification? notification, CancellationToken cancellationToken = default)
2023-02-18 16:18:27 +00:00
{
2023-02-18 17:06:07 +00:00
try
2023-02-18 16:18:27 +00:00
{
2023-02-18 17:41:08 +00:00
using var writer = new ZipWriter(outputStream, new ZipWriterOptions(CompressionType.Deflate));
int i = 0;
int fileCount = model.Files.Count();
bool fileMissing = false;
2023-02-18 17:06:07 +00:00
foreach (var file in model.Files)
2023-02-18 16:18:27 +00:00
{
2023-02-18 17:06:07 +00:00
cancellationToken.ThrowIfCancellationRequested();
using (var stream = UserFileStorage.GetStream(file.File.GetStoragePath()))
2023-02-18 16:18:27 +00:00
{
2023-02-18 17:06:07 +00: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-18 16:18:27 +00:00
{
2023-02-18 17:06:07 +00:00
// Only pop up once to prevent spam.
if (!fileMissing)
2023-02-18 16:18:27 +00:00
{
2023-02-23 11:20:54 +00:00
Logger.Log("Some of model files are missing, they will not be included in the archive", LoggingTarget.Database, LogLevel.Error);
2023-02-18 17:06:07 +00:00
fileMissing = true;
}
}
else
{
writer.Write(file.Filename, stream);
2023-02-18 16:18:27 +00:00
}
}
2023-02-18 17:06:07 +00:00
2023-04-09 13:11:52 +00:00
if (notification != null)
{
notification.Progress = (float)(i + 1) / fileCount;
2023-04-09 13:11:52 +00:00
}
i++;
2023-02-18 16:18:27 +00:00
}
2023-02-18 17:06:07 +00:00
}
catch (ObjectDisposedException)
{
2023-02-18 17:17:24 +00:00
// outputStream may close before writing when request cancel.
2023-02-18 17:06:07 +00:00
if (cancellationToken.IsCancellationRequested)
return;
2023-02-18 16:18:27 +00:00
2023-02-18 17:06:07 +00:00
throw;
2023-02-18 16:18:27 +00:00
}
}
}
}