La richiesta di copia porta i formati («Copia in…» per foto e video, poi la destinazione, poi le impostazioni), i fotogrammi dei video e la «Copia di nuovo» di una sola copia. Il piano prevede convertiti, copiati com'è e fotogrammi; il lavoro si ricorda di fotogrammi ed identità esatta per la ripresa. La copia converte prima dei controlli di spazio e di FAT32, adotta un file già presente solo se lunghezza e SHA-256 della conversione coincidono, e prende i fotogrammi di un video in un solo passaggio, ognuno con il suo istante esatto come identità. Un file copiato in qualunque forma che non sia un fotogramma è «già copiato»; un fotogramma non rende mai copiato il file (nuovo segno FrameOnly). Le note sono un elenco di codici, con i testi italiani nuovi e il rapporto che mostra i file copiati con una nota. Le prove dell'archivio e dei piani delle query seguono le nuove ricerche; due prove nuove coprono una foto convertita e un fotogramma. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
163 lines
6.7 KiB
C#
163 lines
6.7 KiB
C#
using System.Security.Cryptography;
|
|
using Febe.Core.Abstractions;
|
|
using Febe.Core.Domain;
|
|
using Febe.Core.Library;
|
|
using Febe.Core.Library.Conversion;
|
|
using Febe.Core.Storage;
|
|
|
|
namespace Febe.Core.Export;
|
|
|
|
/// <summary>
|
|
/// What the export needs from the rest of Febe (spec §4.10). Built once by the composition (the web layer's export
|
|
/// hub); the layout is a function because on Windows the library folder can change while Febe runs.
|
|
/// </summary>
|
|
public sealed record ExportEnvironment
|
|
{
|
|
public required IFebeStorage Storage { get; init; }
|
|
|
|
/// <summary>The library folder and account folders now.</summary>
|
|
public required Func<LibraryLayout> Layout { get; init; }
|
|
|
|
/// <summary>The Data folder: a destination may not overlap it.</summary>
|
|
public required string DataFolder { get; init; }
|
|
|
|
public required IClock Clock { get; init; }
|
|
|
|
/// <summary>Docker <c>/importa</c>: a destination may not overlap it. Null on Windows.</summary>
|
|
public string? ImportRoot { get; init; }
|
|
|
|
/// <summary>Where destinations may be chosen (Docker: <c>/esporta</c>).</summary>
|
|
public ExportRootOptions Root { get; init; } = ExportRootOptions.AnyFolder;
|
|
|
|
/// <summary>
|
|
/// The full path of a library file present on disk, or null. Null: the library layout (as the web layer's
|
|
/// <c>LibraryFileResolver</c>). Use <see cref="ResolveSourcePath"/>.
|
|
/// </summary>
|
|
public Func<Account, Media, string?>? ResolveSource { get; init; }
|
|
|
|
/// <summary>Reading and hashing files (library files and files found in a destination).</summary>
|
|
public IExportFileSystem FileSystem { get; init; } = ExportFileSystem.Instance;
|
|
|
|
/// <summary>Free space and the FAT32 limit of a destination.</summary>
|
|
public IExportDisk Disk { get; init; } = new SystemExportDisk(new ProcMountInfo());
|
|
|
|
/// <summary>Copies never run during an engine session.</summary>
|
|
public IEngineSessionState Session { get; init; } = NoEngineSession.Instance;
|
|
|
|
/// <summary>Shared with import and rebuild: never together (spec §4.10). Null: nothing else to keep out (tests).</summary>
|
|
public LongWorkGate? Gate { get; init; }
|
|
|
|
/// <summary>Time allowed to a check of a destination folder (a disconnected share must not block).</summary>
|
|
public TimeSpan FolderCheckTimeout { get; init; } = TimeSpan.FromSeconds(10);
|
|
|
|
/// <summary>Writes the probe file of a folder check (a seam: tests make it fail or hang); null: the real file.</summary>
|
|
public Action<string>? WriteProbe { get; init; }
|
|
|
|
/// <summary>How often a waiting job looks whether the engine session ended.</summary>
|
|
public TimeSpan SessionPoll { get; init; } = TimeSpan.FromSeconds(5);
|
|
|
|
/// <summary>A group of copies, checked with a probe before it starts: at most this many files…</summary>
|
|
public int GroupFiles { get; init; } = 100;
|
|
|
|
/// <summary>…or this many planned bytes, whichever comes first.</summary>
|
|
public long GroupBytes { get; init; } = 1L << 30;
|
|
|
|
/// <summary>One conversion at a time in the process (tappa E3, D16): shared with the library conversion.</summary>
|
|
public ConversionThrottle Throttle { get; init; } = ConversionThrottle.Shared;
|
|
|
|
/// <summary>The checker of destination folders of this environment.</summary>
|
|
public ExportFolderCheck CreateFolderCheck() => new(Root.Mounts, FolderCheckTimeout, WriteProbe);
|
|
|
|
/// <summary>The protected folders now (Data, library, account folders, <c>/importa</c>) and the extra ones.</summary>
|
|
public Task<ExportFolderContext> FolderContextAsync(IReadOnlyList<ProtectedFolder>? extra, CancellationToken ct) =>
|
|
ExportFolderCheck.ContextAsync(Storage, Layout(), DataFolder, ImportRoot, Root, extra, ct);
|
|
|
|
/// <summary>The full path of a library file present on disk, or null (<see cref="ResolveSource"/> or the layout).</summary>
|
|
public string? ResolveSourcePath(Account account, Media media)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(account);
|
|
ArgumentNullException.ThrowIfNull(media);
|
|
if (ResolveSource is { } resolve)
|
|
{
|
|
return resolve(account, media);
|
|
}
|
|
|
|
if (media.RelativePath is null || !EntryStateRules.IsPresent(media.State))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
try
|
|
{
|
|
var path = Layout().FullPath(account, media.RelativePath);
|
|
return FileSystem.FileExists(path) ? path : null;
|
|
}
|
|
catch (ArgumentException)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>File operations of the export (a seam, like <c>IImportFileSystem</c>).</summary>
|
|
public interface IExportFileSystem
|
|
{
|
|
/// <summary>Opens a file for reading only.</summary>
|
|
Stream OpenRead(string path);
|
|
|
|
/// <summary>
|
|
/// Opens a file for reading at any position (a video to remux or to take frames from, tappa E3). By default the
|
|
/// stream of <see cref="OpenRead"/>, which can seek.
|
|
/// </summary>
|
|
Stream OpenRandomAccess(string path) => OpenRead(path);
|
|
|
|
/// <summary>SHA-256 (lowercase hex) of a file as it is on disk now.</summary>
|
|
Task<string> ComputeSha256Async(string path, CancellationToken ct);
|
|
|
|
bool FileExists(string path);
|
|
|
|
/// <summary>Size of a file, or null when it is not there.</summary>
|
|
long? FileLength(string path);
|
|
}
|
|
|
|
/// <summary>The real file system.</summary>
|
|
public sealed class ExportFileSystem : IExportFileSystem
|
|
{
|
|
private const int BufferSize = 128 * 1024;
|
|
|
|
private ExportFileSystem()
|
|
{
|
|
}
|
|
|
|
public static ExportFileSystem Instance { get; } = new();
|
|
|
|
// FileShare.Delete: a long read never blocks «Elimina dalla libreria» on Windows (tappa E3).
|
|
public Stream OpenRead(string path) =>
|
|
new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete, BufferSize, FileOptions.Asynchronous | FileOptions.SequentialScan);
|
|
|
|
/// <summary>As <c>ContainerReader.OpenRead</c>: shared for reading, writing and deleting, read at random positions.</summary>
|
|
public Stream OpenRandomAccess(string path) =>
|
|
new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, BufferSize, FileOptions.RandomAccess);
|
|
|
|
public async Task<string> ComputeSha256Async(string path, CancellationToken ct)
|
|
{
|
|
await using var stream = OpenRead(path);
|
|
return Convert.ToHexStringLower(await SHA256.HashDataAsync(stream, ct).ConfigureAwait(false));
|
|
}
|
|
|
|
public bool FileExists(string path) => File.Exists(path);
|
|
|
|
public long? FileLength(string path)
|
|
{
|
|
try
|
|
{
|
|
var info = new FileInfo(path);
|
|
return info.Exists ? info.Length : null;
|
|
}
|
|
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or ArgumentException or NotSupportedException)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|