1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 14:17:27 +08:00
osu-lazer/osu.Game/Database/LegacyArchiveExporter.cs

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

72 lines
2.6 KiB
C#
Raw Normal View History

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.
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
{
/// <summary>
2023-05-06 22:53:35 +08:00
/// 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-19 00:18:27 +08:00
where TModel : RealmObject, IHasNamedFiles, IHasGuidPrimaryKey
{
protected LegacyArchiveExporter(Storage storage)
: base(storage)
2023-02-19 00:18:27 +08:00
{
}
2023-05-05 20:28:43 +08:00
public override void ExportToStream(TModel model, Stream outputStream, ProgressNotification? notification, CancellationToken cancellationToken = default)
2023-02-19 00:18:27 +08:00
{
2023-05-05 20:05:57 +08:00
using (var writer = new ZipWriter(outputStream, new ZipWriterOptions(CompressionType.Deflate)))
2023-02-19 00:18:27 +08:00
{
int i = 0;
int fileCount = model.Files.Count();
2023-05-07 01:38:41 +08:00
bool anyFileMissing = 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 = GetFileContents(model, file))
2023-02-19 00:18:27 +08:00
{
2023-02-19 01:06:07 +08:00
if (stream == null)
2023-02-19 00:18:27 +08:00
{
Logger.Log($"File {file.Filename} is missing in local storage and will not be included in the export", LoggingTarget.Database);
2023-05-07 01:38:41 +08:00
anyFileMissing = true;
continue;
2023-02-19 00:18:27 +08:00
}
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 = (float)i / fileCount;
2023-04-09 21:11:52 +08:00
}
2023-02-19 00:18:27 +08:00
}
2023-05-07 01:38:41 +08:00
if (anyFileMissing)
{
2023-05-07 01:38:05 +08:00
Logger.Log("Some files are missing in local storage and will not be included in the export", LoggingTarget.Database, LogLevel.Error);
}
2023-02-19 01:06:07 +08:00
}
2023-02-19 00:18:27 +08:00
}
protected virtual Stream? GetFileContents(TModel model, INamedFileUsage file) => UserFileStorage.GetStream(file.File.GetStoragePath());
2023-02-19 00:18:27 +08:00
}
}