2021-11-25 07:36:30 +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.
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2021-11-25 07:36:30 +00:00
|
|
|
using System.IO;
|
2022-11-30 10:06:44 +00:00
|
|
|
using System.Linq;
|
2021-11-25 07:36:30 +00:00
|
|
|
using osu.Framework.Platform;
|
|
|
|
using osu.Game.Extensions;
|
2022-11-30 10:06:44 +00:00
|
|
|
using osu.Game.Utils;
|
2021-11-25 07:36:30 +00:00
|
|
|
using SharpCompress.Archives.Zip;
|
|
|
|
|
|
|
|
namespace osu.Game.Database
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// A class which handles exporting legacy user data of a single type from osu-stable.
|
|
|
|
/// </summary>
|
|
|
|
public abstract class LegacyExporter<TModel>
|
|
|
|
where TModel : class, IHasNamedFiles
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// The file extension for exports (including the leading '.').
|
|
|
|
/// </summary>
|
|
|
|
protected abstract string FileExtension { get; }
|
|
|
|
|
|
|
|
protected readonly Storage UserFileStorage;
|
|
|
|
|
2022-11-30 10:06:44 +00:00
|
|
|
private readonly Storage exportStorage;
|
2021-11-25 07:36:30 +00:00
|
|
|
|
2021-11-25 09:36:01 +00:00
|
|
|
protected LegacyExporter(Storage storage)
|
2021-11-25 07:36:30 +00:00
|
|
|
{
|
2022-11-30 10:06:44 +00:00
|
|
|
exportStorage = storage.GetStorageForDirectory(@"exports");
|
2021-11-25 07:36:30 +00:00
|
|
|
UserFileStorage = storage.GetStorageForDirectory(@"files");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Exports an item to a legacy (.zip based) package.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The item to export.</param>
|
2022-11-30 10:06:44 +00:00
|
|
|
public void Export(TModel item)
|
2021-11-25 07:36:30 +00:00
|
|
|
{
|
2022-11-30 10:06:44 +00:00
|
|
|
var itemFilename = item.GetDisplayString().GetValidFilename();
|
2021-11-25 07:36:30 +00:00
|
|
|
|
2022-11-30 10:06:44 +00:00
|
|
|
var existingExports = exportStorage.GetFiles("", $"{itemFilename}*{FileExtension}").ToArray();
|
|
|
|
|
|
|
|
// trim the file extension
|
|
|
|
for (int i = 0; i < existingExports.Length; i++)
|
|
|
|
existingExports[i] = existingExports[i].TrimEnd(FileExtension.ToCharArray());
|
|
|
|
|
|
|
|
string filename = $"{NamingUtils.GetNextBestName(existingExports, itemFilename)}{FileExtension}";
|
|
|
|
using (var stream = exportStorage.CreateFileSafely(filename))
|
2021-11-25 07:36:30 +00:00
|
|
|
ExportModelTo(item, stream);
|
|
|
|
|
2022-11-30 10:06:44 +00:00
|
|
|
exportStorage.PresentFileExternally(filename);
|
2021-11-25 07:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Exports an item to the given output stream.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="model">The item to export.</param>
|
|
|
|
/// <param name="outputStream">The output stream to export to.</param>
|
|
|
|
public virtual void ExportModelTo(TModel model, Stream outputStream)
|
|
|
|
{
|
|
|
|
using (var archive = ZipArchive.Create())
|
|
|
|
{
|
|
|
|
foreach (var file in model.Files)
|
|
|
|
archive.AddEntry(file.Filename, UserFileStorage.GetStream(file.File.GetStoragePath()));
|
|
|
|
|
|
|
|
archive.SaveTo(outputStream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|