osu/osu.Game/Database/LegacyArchiveExporter.cs

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

86 lines
3.4 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;
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;
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)
{
}
protected override void ExportToStream(TModel model, Stream outputStream, ProgressNotification notification, CancellationToken cancellationToken = default)
=> 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>
private void exportZipArchive(TModel model, Stream outputStream, ProgressNotification notification, CancellationToken cancellationToken = default)
{
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));
float i = 0;
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-18 17:06:07 +00:00
PostNotification?.Invoke(new SimpleErrorNotification
{
Text = "Some of your files are missing, they will not be included in the archive"
});
fileMissing = true;
}
}
else
{
writer.Write(file.Filename, stream);
2023-02-18 16:18:27 +00:00
}
}
2023-02-18 17:06:07 +00:00
i++;
notification.Progress = i / model.Files.Count();
notification.Text = $"Exporting... ({i}/{model.Files.Count()})";
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
}
}
}
}