2020-12-07 09:00:45 +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;
|
2022-02-11 07:02:25 +00:00
|
|
|
using osu.Framework.Extensions;
|
2020-12-07 09:00:45 +00:00
|
|
|
using osu.Game.IO.Archives;
|
|
|
|
using osu.Game.Utils;
|
|
|
|
using SharpCompress.Common;
|
|
|
|
|
|
|
|
namespace osu.Game.Database
|
|
|
|
{
|
|
|
|
/// <summary>
|
2022-06-16 09:53:13 +00:00
|
|
|
/// An encapsulated import task to be imported to an <see cref="RealmArchiveModelImporter{TModel}"/>.
|
2020-12-07 09:00:45 +00:00
|
|
|
/// </summary>
|
|
|
|
public class ImportTask
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// The path to the file (or filename in the case a stream is provided).
|
|
|
|
/// </summary>
|
|
|
|
public string Path { get; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// An optional stream which provides the file content.
|
|
|
|
/// </summary>
|
2020-12-08 03:52:34 +00:00
|
|
|
public Stream? Stream { get; }
|
2020-12-07 09:00:45 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Construct a new import task from a path (on a local filesystem).
|
|
|
|
/// </summary>
|
|
|
|
public ImportTask(string path)
|
|
|
|
{
|
|
|
|
Path = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2022-06-20 06:38:44 +00:00
|
|
|
/// Construct a new import task from a stream. The provided stream will be disposed after reading.
|
2020-12-07 09:00:45 +00:00
|
|
|
/// </summary>
|
|
|
|
public ImportTask(Stream stream, string filename)
|
|
|
|
{
|
|
|
|
Path = filename;
|
|
|
|
Stream = stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Retrieve an archive reader from this task.
|
|
|
|
/// </summary>
|
|
|
|
public ArchiveReader GetReader()
|
|
|
|
{
|
2023-09-14 04:29:29 +00:00
|
|
|
if (Stream == null)
|
2023-09-14 04:38:23 +00:00
|
|
|
{
|
|
|
|
if (ZipUtils.IsZipArchive(Path))
|
|
|
|
return new ZipArchiveReader(File.Open(Path, FileMode.Open, FileAccess.Read, FileShare.Read), System.IO.Path.GetFileName(Path));
|
|
|
|
if (Directory.Exists(Path))
|
|
|
|
return new DirectoryArchiveReader(Path);
|
|
|
|
if (File.Exists(Path))
|
|
|
|
return new SingleFileArchiveReader(Path);
|
|
|
|
|
|
|
|
throw new InvalidFormatException($"{Path} is not a valid archive");
|
|
|
|
}
|
2023-09-14 04:29:29 +00:00
|
|
|
|
2023-09-14 13:19:18 +00:00
|
|
|
if (Stream is not MemoryStream memoryStream)
|
2023-09-14 17:36:33 +00:00
|
|
|
{
|
2023-09-14 13:19:18 +00:00
|
|
|
// Path used primarily in tests (converting `ManifestResourceStream`s to `MemoryStream`s).
|
|
|
|
memoryStream = new MemoryStream(Stream.ReadAllBytesToArray());
|
2023-09-14 17:36:33 +00:00
|
|
|
Stream.Dispose();
|
|
|
|
}
|
2023-09-14 04:29:29 +00:00
|
|
|
|
2023-09-14 13:19:18 +00:00
|
|
|
if (ZipUtils.IsZipArchive(memoryStream))
|
|
|
|
return new ZipArchiveReader(memoryStream, Path);
|
2023-09-14 04:29:29 +00:00
|
|
|
|
2023-09-14 13:19:18 +00:00
|
|
|
return new MemoryStreamArchiveReader(memoryStream, Path);
|
2021-11-12 07:39:17 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 21:19:04 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Deletes the file that is encapsulated by this <see cref="ImportTask"/>.
|
|
|
|
/// </summary>
|
|
|
|
public virtual void DeleteFile()
|
|
|
|
{
|
|
|
|
if (File.Exists(Path))
|
|
|
|
File.Delete(Path);
|
|
|
|
}
|
|
|
|
|
2020-12-07 09:00:45 +00:00
|
|
|
public override string ToString() => System.IO.Path.GetFileName(Path);
|
|
|
|
}
|
|
|
|
}
|