E3: formati e fotogrammi nella copia verso le cartelle esterne

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>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-27 23:08:38 +01:00
1 parent 704c1ed9cf
commit b746aff45d
19 files changed
+1941 -257

No files matched your search

+118 -14
View File
@@ -15,6 +15,9 @@ namespace Febe.Core.Export;
public sealed class ExportService : IAsyncDisposable
{
private const string BusyText = "C'è già una copia al lavoro: aspetta che finisca o interrompila.";
/// <summary>A frames request over <see cref="ExportFrames.MaxPerRequest"/>.</summary>
public const string TooManyFramesText = "Più di 20.000 fotogrammi in una richiesta: scegli meno video o un intervallo più lungo";
private const int ItemBatch = 2000;
private static readonly TimeSpan ProgressInterval = TimeSpan.FromMilliseconds(250);
@@ -114,9 +117,13 @@ public sealed class ExportService : IAsyncDisposable
/// recorded and nothing starts.
/// </summary>
/// <exception cref="InvalidOperationException">A copy, an import or a rebuild is running.</exception>
/// <exception cref="ArgumentException">
/// The request is not valid, or a frames request asks more than 1.000 frames of a video or 20.000 in all (Italian message).
/// </exception>
public async Task<ExportStart> StartAsync(ExportJobRequest request, CancellationToken ct = default)
{
ArgumentNullException.ThrowIfNull(request);
ExportPlanner.Validate(request);
var held = new LeaseHolder();
lock (_gate)
{
@@ -160,12 +167,32 @@ public sealed class ExportService : IAsyncDisposable
var runs = new Dictionary<long, ExportRun>();
var pending = new List<ExportItem>();
var exports = _env.Storage.Exports;
if (request.Frames is not null)
{
// Refused before anything is recorded (D12 of tappa E3): a video over 1.000 frames, more than 20.000 in all.
var count = new PlanTally();
await foreach (var file in Planner.EnumerateAsync(request, effective, settings, ct).ConfigureAwait(false))
{
count.Add(file);
}
if (count.OverCap > 0)
{
throw new ArgumentException(ExportFrames.OverCapMessage, nameof(request));
}
if (count.Frames > ExportFrames.MaxPerRequest)
{
throw new ArgumentException(TooManyFramesText, nameof(request));
}
}
try
{
await foreach (var file in Planner.EnumerateAsync(request, effective, settings, ct).ConfigureAwait(false))
{
tally.Add(file);
if (file.DestinationId is not { } destination || file.Action is PlannedCopy.Missing or PlannedCopy.NotExported)
if (file.DestinationId is not { } destination || file.Action is PlannedCopy.Missing or PlannedCopy.NotExported or PlannedCopy.PhotoSkipped or PlannedCopy.OverCap)
{
continue;
}
@@ -176,22 +203,28 @@ public sealed class ExportService : IAsyncDisposable
new ExportRun { DestinationId = destination, Kind = ExportRunKind.Copy, Origin = request.Origin, State = ExportRunState.Running, StartedAt = _env.Clock.Now },
CancellationToken.None).ConfigureAwait(false);
runs[destination] = run;
if (request.CopyAgain)
{
await ExportRunSettings.SetCopyAgainAsync(_env.Storage.Settings, run.Id, true, CancellationToken.None).ConfigureAwait(false);
}
await RecordRunSettingsAsync(run.Id, request).ConfigureAwait(false);
}
pending.Add(new ExportItem
// Frame items of one video are added together, in instant order (the copy takes them as one batch).
IReadOnlyList<TimeSpan?> instants = file.UnknownDuration && request.Frames is { } frames
? await FrameInstantsAsync(file.Candidate, frames, ct).ConfigureAwait(false)
: [file.FrameAt];
foreach (var at in instants)
{
RunId = run.Id,
MediaId = file.Candidate.MediaId,
AccountId = file.Candidate.AccountId,
SourceSha256 = file.Candidate.Sha256,
Variant = ExportVariant.Whole,
Format = ExportFormat.Original,
State = ExportItemState.Pending,
});
pending.Add(new ExportItem
{
RunId = run.Id,
MediaId = file.Candidate.MediaId,
AccountId = file.Candidate.AccountId,
SourceSha256 = file.Candidate.Sha256,
Variant = file.Variant,
Format = file.Format,
FrameAt = at,
State = ExportItemState.Pending,
});
}
if (pending.Count >= ItemBatch)
{
await exports.AddItemsAsync(pending, CancellationToken.None).ConfigureAwait(false);
@@ -235,6 +268,77 @@ public sealed class ExportService : IAsyncDisposable
return new ExportStart(ordered, plan);
}
/// <summary>
/// The keys of a new job: cleared first (a run id can be used again after <c>DeleteRunAsync</c>), then what a resume
/// needs to know (WP1.2 of tappa E3): «Copia di nuovo», the frames request, the exact identity of «Copia di nuovo»
/// of one copy.
/// </summary>
private async Task RecordRunSettingsAsync(long runId, ExportJobRequest request)
{
var store = _env.Storage.Settings;
var none = CancellationToken.None;
await ExportRunSettings.ClearAsync(store, runId, none).ConfigureAwait(false);
if (request.CopyAgain)
{
await ExportRunSettings.SetCopyAgainAsync(store, runId, true, none).ConfigureAwait(false);
}
if (request.Frames is { } frames)
{
await ExportRunSettings.SetFramesAsync(store, runId, frames.Mode, frames.Count ?? 0, none).ConfigureAwait(false);
}
else if (request.Exact is { Variant: ExportVariant.Frame })
{
await ExportRunSettings.SetFramesAsync(store, runId, FrameMode.At, 1, none).ConfigureAwait(false);
}
if (request.Exact is not null)
{
await ExportRunSettings.SetExactAsync(store, runId, none).ConfigureAwait(false);
}
}
/// <summary>
/// The instants of a video whose length the library does not know: its headers are read now (only the library
/// file, never the destination). A video that cannot be read gets one instant, and the copy says «Fotogramma non
/// disponibile per questo video».
/// </summary>
private async Task<IReadOnlyList<TimeSpan?>> FrameInstantsAsync(ExportCandidate candidate, ExportFramesRequest frames, CancellationToken ct)
{
TimeSpan? duration = null;
var media = await _env.Storage.Media.GetAsync(candidate.MediaId, ct).ConfigureAwait(false);
var account = await _env.Storage.Accounts.GetAsync(candidate.AccountId, ct).ConfigureAwait(false);
if (media is not null && account is not null && _env.ResolveSourcePath(account, media) is { } path)
{
try
{
duration = await Task.Run(
() =>
{
using var stream = _env.FileSystem.OpenRandomAccess(path);
return VideoFrameReader.ProbeDuration(stream);
},
ct).ConfigureAwait(false);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
// Not readable now: one instant, the copy tells.
}
}
if (duration is not { } d || d <= TimeSpan.Zero)
{
return [TimeSpan.Zero];
}
if (ExportFrames.FrameCount(d, frames) > ExportFrames.MaxPerVideo)
{
throw new ArgumentException(ExportFrames.OverCapMessage, nameof(frames));
}
return [.. ExportFrames.Instants(d, frames).Select(at => (TimeSpan?)at)];
}
/// <summary>
/// «Riprendi»: continues unfinished jobs in the background, oldest first. Null: every interrupted or paused one (a
/// paused one checks its destination first and pauses again if it still does not answer).