osu/osu.Game/Database/LegacyArchiveExporter.cs

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

70 lines
2.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.
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>
2023-05-06 14:53:35 +00: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-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-05-05 12:28:43 +00:00
public override void ExportToStream(TModel model, Stream outputStream, ProgressNotification? notification, CancellationToken cancellationToken = default)
2023-02-18 16:18:27 +00:00
{
2023-05-05 12:05:57 +00:00
using (var writer = new ZipWriter(outputStream, new ZipWriterOptions(CompressionType.Deflate)))
2023-02-18 16:18:27 +00:00
{
int i = 0;
int fileCount = model.Files.Count();
2023-05-06 17:38:41 +00:00
bool anyFileMissing = 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
if (stream == null)
2023-02-18 16:18:27 +00:00
{
Logger.Log($"File {file.Filename} is missing in local storage and will not be included in the export", LoggingTarget.Database);
2023-05-06 17:38:41 +00:00
anyFileMissing = true;
continue;
2023-02-18 16:18:27 +00:00
}
writer.Write(file.Filename, stream);
2023-02-18 16:18:27 +00:00
}
2023-02-18 17:06:07 +00:00
i++;
2023-04-09 13:11:52 +00:00
if (notification != null)
{
notification.Progress = (float)i / fileCount;
2023-04-09 13:11:52 +00:00
}
2023-02-18 16:18:27 +00:00
}
2023-05-06 17:38:41 +00:00
if (anyFileMissing)
{
2023-05-06 17:38:05 +00:00
Logger.Log("Some files are missing in local storage and will not be included in the export", LoggingTarget.Database, LogLevel.Error);
}
2023-02-18 17:06:07 +00:00
}
2023-02-18 16:18:27 +00:00
}
}
}