Il servizio delle copie impara le partenze automatiche: TryStartAutomaticAsync e TryResumeAutomaticAsync non lanciano mai eccezioni perché Febe è occupata (occupato, niente da fare senza prendere il cancello, ferme), registrano solo i file da scrivere, un lavoro per cartella, e prendono il cancello con il permesso che cede: «Copia in…», «Riprendi», «Controlla le copie» e «Riprova» lo spostano con EnterAsync e aspettano che abbia salvato i suoi stati. I cambi di cartella passano da ChangeDestinationAsync, serializzati con le partenze automatiche: spegnere «Automatica» ferma subito la copia automatica di quella cartella e annulla i suoi lavori (risposta 52); un lavoro automatico non finito non blocca più il cambio di cartella. La copia annulla da sé un lavoro automatico la cui cartella non è più automatica. «Interrompi» di una copia automatica la ferma per un'ora, in memoria, con «Riprendi ora» (risposta 53). La prova di «Riprova» guarda anche lo spazio per il prossimo file prima di togliere una pausa. «Copia automatica in … finita» solo se il lavoro ha fatto qualcosa (risposta 54). I «Non riuscito» restano candidati di ogni giro (risposta 50); il percorso troppo lungo ha la sua nota. La chiusura aspetta il lavoro al più 10 secondi. Il segnale parte dal nucleo quando una cartella si accende o prende più file. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
250 lines
12 KiB
C#
250 lines
12 KiB
C#
using Febe.Core.Abstractions;
|
|
using Febe.Core.Domain;
|
|
using Febe.Core.Storage;
|
|
|
|
namespace Febe.Core.Export;
|
|
|
|
/// <summary>What a «Copia in…» is asked for (spec §4.10 «Manuale sempre»).</summary>
|
|
public abstract record ExportSelection
|
|
{
|
|
private ExportSelection()
|
|
{
|
|
}
|
|
|
|
/// <summary>Files by id: a selection of «Tutte le foto», a file of the viewer.</summary>
|
|
public sealed record Media(IReadOnlyList<long> MediaIds) : ExportSelection;
|
|
|
|
/// <summary>Entries by id: a grouped selection, an entry of the viewer.</summary>
|
|
public sealed record Entries(IReadOnlyList<long> EntryIds) : ExportSelection;
|
|
|
|
/// <summary>The current filter («Copia i file in…» of the CSV menu).</summary>
|
|
public sealed record Filter(LibraryFilter LibraryFilter, MosaicMode Mode) : ExportSelection;
|
|
|
|
/// <summary>Every file of an account (the account card).</summary>
|
|
public sealed record Account(long AccountId) : ExportSelection;
|
|
|
|
/// <summary>
|
|
/// The automatic export (tappa E5, D4 and D5): per automatic destination, the files it is the effective destination
|
|
/// of, not copied («Tolto» and «Superato» never), new since its «Solo i nuovi» instant, and settled (no file of the
|
|
/// entry still to download, the entry not in <paramref name="HeldEntryIds"/>).
|
|
/// </summary>
|
|
/// <param name="Destinations">The automatic destinations of the pass; one not in use (retired, unknown) gives nothing.</param>
|
|
/// <param name="HeldEntryIds">Entries whose line of <c>_febe.jsonl</c> is still to be written (<c>libreria.giornale-da-scrivere</c>).</param>
|
|
/// <param name="Round">
|
|
/// A recovery round (start-up and every hour): files whose last copy in the destination is «Non riuscito» are
|
|
/// candidates too, every round, whatever the error (answer 50 of 4 December 2026). Outside a round they wait for it,
|
|
/// so a file that keeps failing does not make a job at every signal.
|
|
/// </param>
|
|
public sealed record Automatic(IReadOnlyList<AutomaticScope> Destinations, IReadOnlyList<long> HeldEntryIds, bool Round) : ExportSelection;
|
|
|
|
/// <summary>One automatic destination and its «Solo i nuovi» instant (null: everything due, saved before or after).</summary>
|
|
/// <param name="Since">Files saved in the library at or after it (<c>media.downloaded_at</c>); a file without that time is not new.</param>
|
|
public sealed record AutomaticScope(long DestinationId, DateTimeOffset? Since);
|
|
}
|
|
|
|
/// <summary>How the instants of «Fotogrammi» are chosen (spec §4.10 «Fotogrammi»).</summary>
|
|
public enum FrameMode
|
|
{
|
|
/// <summary>«Questo istante»: one frame at <see cref="ExportFramesRequest.At"/>.</summary>
|
|
At,
|
|
|
|
/// <summary>«Uno ogni k secondi»: at 0, k, 2k… before the end.</summary>
|
|
Every,
|
|
|
|
/// <summary>«N in tutto»: N frames spread over the video, each in the middle of its share.</summary>
|
|
Count,
|
|
|
|
/// <summary>
|
|
/// «Salva il fotogramma» of a still video (tappa E4, answer 45): the one frame the analysis chose (the sharpest
|
|
/// looked at, never a dark one), at the instant <c>video_analysis</c> keeps; named <c>…_fotogramma</c>. One video
|
|
/// only (answer 46).
|
|
/// </summary>
|
|
Still,
|
|
}
|
|
|
|
/// <summary>A request of frames of videos, in a photo format (Jpg, Png or WebpLossless).</summary>
|
|
public sealed record ExportFramesRequest(FrameMode Mode, TimeSpan? At, TimeSpan? Every, int? Count, ExportFormat Format);
|
|
|
|
/// <summary>
|
|
/// The formats of «Copia in…» for this request only (tappa E3, D1): null keeps the destination's (or the general
|
|
/// settings'). A photo format or Original for photos; Mp4 or Original for videos.
|
|
/// </summary>
|
|
public sealed record ExportFormatChoice(ExportFormat? Photo, ExportFormat? Video);
|
|
|
|
/// <summary>«Copia di nuovo» of one copy row (tappa E3, D11): the form of that row, looked up only by its exact identity.</summary>
|
|
/// <param name="FrameAt">The instant of a frame row; null otherwise.</param>
|
|
/// <param name="Still">The frame row is one of «Salva il fotogramma»: the new file is named <c>…_fotogramma</c> again.</param>
|
|
public sealed record ExportCopyRequest(ExportVariant Variant, ExportFormat Format, TimeSpan? FrameAt, bool Still = false);
|
|
|
|
/// <summary>A «Copia in…»: what to copy, whether «Tolti» and «Superati» are copied again (D9), who asked.</summary>
|
|
/// <param name="Formats">The formats of this request («Copia in…» → «Cambia»); null: those of the destination.</param>
|
|
/// <param name="Frames">Not null: a frames job («Fotogrammi…» → «Nella cartella delle copie»), videos only.</param>
|
|
/// <param name="Exact">«Copia di nuovo» of one copy row, only with a selection of one file.</param>
|
|
public sealed record ExportJobRequest(
|
|
ExportSelection Selection,
|
|
bool CopyAgain = false,
|
|
ExportRunOrigin Origin = ExportRunOrigin.Manual,
|
|
ExportFormatChoice? Formats = null,
|
|
ExportFramesRequest? Frames = null,
|
|
ExportCopyRequest? Exact = null);
|
|
|
|
/// <summary>
|
|
/// What a «Copia in…» would do (database only, decisions D3 and D9 of tappa E2). Each file goes to the effective
|
|
/// destination of its account (D5).
|
|
/// </summary>
|
|
/// <param name="Files">Files to write.</param>
|
|
/// <param name="Bytes">Their bytes.</param>
|
|
/// <param name="AlreadyCopied">Already in their destination (or, with «Una copia sola», copied for another account).</param>
|
|
/// <param name="RemovedOrSuperseded">
|
|
/// «Tolti o superati»: never copied again by themselves. With <see cref="ExportJobRequest.CopyAgain"/> they are also
|
|
/// counted in <paramref name="Files"/> and <paramref name="Bytes"/>, since they will be written.
|
|
/// </param>
|
|
/// <param name="NotExported">«Esporta questo account» off, no destination, or a content type the destination does not take.</param>
|
|
/// <param name="Missing">«File non presente in libreria».</param>
|
|
/// <param name="Destinations">Each destination that gets files, with whether it answers now.</param>
|
|
/// <param name="HasDestination">
|
|
/// A folder of the copies exists (the general one or one of an account, in use): when nothing is to copy, the dialog
|
|
/// says «no folder» only when this is false, not because no destination gets files.
|
|
/// </param>
|
|
public sealed record ExportPlan(
|
|
int Files,
|
|
long Bytes,
|
|
int AlreadyCopied,
|
|
int RemovedOrSuperseded,
|
|
int NotExported,
|
|
int Missing,
|
|
IReadOnlyList<ExportPlanDestination> Destinations,
|
|
bool HasDestination = true)
|
|
{
|
|
public static ExportPlan Empty { get; } = new(0, 0, 0, 0, 0, 0, []);
|
|
|
|
/// <summary>
|
|
/// Files predicted to be copied as they are, by kind («3 HEIC copiati come sono»): a prediction from the names
|
|
/// («circa»), the copy decides on the bytes (tappa E3).
|
|
/// </summary>
|
|
public IReadOnlyList<ExportPlanAsIs> AsIs { get; init; } = [];
|
|
|
|
/// <summary>Files predicted to be converted.</summary>
|
|
public int Converted { get; init; }
|
|
|
|
/// <summary>«Con perdita»: at least one file predicted converted to JPG, or JPG frames (D7 of tappa E3).</summary>
|
|
public bool Lossy { get; init; }
|
|
|
|
/// <summary>Frames job: the due videos.</summary>
|
|
public int Videos { get; init; }
|
|
|
|
/// <summary>Frames job: the photos of the selection, skipped («Le 4 foto si saltano»).</summary>
|
|
public int PhotosSkipped { get; init; }
|
|
|
|
/// <summary>Frames job: the frames to write (also in <see cref="Files"/>); «circa» for a video of unknown length.</summary>
|
|
public int Frames { get; init; }
|
|
|
|
/// <summary>Frames job: videos that would give more than <see cref="ExportFrames.MaxPerVideo"/> frames (the job is refused).</summary>
|
|
public int OverCap { get; init; }
|
|
|
|
/// <summary>
|
|
/// «Salva il fotogramma» (tappa E4): due videos whose analysis is not «Fermo», or that were never analysed (skipped:
|
|
/// «Non è un video fermo, o non è stato analizzato»).
|
|
/// </summary>
|
|
public int NotStill { get; init; }
|
|
}
|
|
|
|
/// <summary>Files of a plan copied as they are, of one kind («HEIC», «AVIF», «raw», «GIF», «WebM»).</summary>
|
|
public sealed record ExportPlanAsIs(string Label, int Files);
|
|
|
|
/// <summary>A destination of a plan.</summary>
|
|
/// <param name="Username">The account of an own folder; null for the general folder.</param>
|
|
/// <param name="Files">Files to write there.</param>
|
|
/// <param name="Paused">The destination is paused (its last check failed).</param>
|
|
/// <param name="Problem">Why it cannot be written now (not reachable, not mounted, paused), in Italian; null when fine.</param>
|
|
/// <param name="FreeBytes">Free space, when it can be measured.</param>
|
|
public sealed record ExportPlanDestination(
|
|
long DestinationId,
|
|
string Label,
|
|
string RootPath,
|
|
ExportScope Scope,
|
|
string? Username,
|
|
int Files,
|
|
long Bytes,
|
|
bool Paused,
|
|
string? Problem,
|
|
long? FreeBytes);
|
|
|
|
/// <summary>What the export job is doing.</summary>
|
|
public enum ExportPhase
|
|
{
|
|
Preparing,
|
|
|
|
/// <summary>«Controlla le copie».</summary>
|
|
Checking,
|
|
|
|
/// <summary>An engine session is running: copies wait for it to end (decision D11).</summary>
|
|
WaitingForSession,
|
|
|
|
Copying,
|
|
|
|
Finished,
|
|
}
|
|
|
|
/// <summary>Progress of the export job, for the progress card (polled, D20).</summary>
|
|
/// <param name="RunId">The job's run being worked on; null for «Controlla le copie» and when nothing ever ran.</param>
|
|
/// <param name="BytesTotal">Bytes of the files still pending when the run (re)started, plus those done since; 0 when unknown.</param>
|
|
public sealed record ExportProgress(
|
|
long? RunId,
|
|
ExportPhase Phase,
|
|
int FilesDone,
|
|
int FilesTotal,
|
|
long BytesDone,
|
|
long BytesTotal,
|
|
string? CurrentFile,
|
|
string? DestinationLabel,
|
|
IReadOnlyDictionary<ExportItemState, int> ByState,
|
|
DateTimeOffset UpdatedAt)
|
|
{
|
|
/// <summary>0..1: by bytes when known, otherwise by files.</summary>
|
|
public double Fraction => BytesTotal > 0
|
|
? Math.Clamp((double)BytesDone / BytesTotal, 0, 1)
|
|
: FilesTotal > 0 ? Math.Clamp((double)FilesDone / FilesTotal, 0, 1) : 0;
|
|
|
|
/// <summary>
|
|
/// The kind of <see cref="RunId"/>'s run (tappa E5, D7.8): taken from the run being worked on, not from the job; null
|
|
/// for «Controlla le copie» and before a run is reached.
|
|
/// </summary>
|
|
public ExportRunKind? Kind { get; init; }
|
|
|
|
/// <summary>Who started <see cref="RunId"/>'s run: an automatic run resumed with «Riprendi» stays automatic here.</summary>
|
|
public ExportRunOrigin? Origin { get; init; }
|
|
}
|
|
|
|
/// <summary>What an automatic start did (tappa E5, D7): never an exception because Febe is busy.</summary>
|
|
public enum AutoExportStartOutcome
|
|
{
|
|
/// <summary>A job started: <see cref="AutoExportStart.Runs"/> are its runs.</summary>
|
|
Started,
|
|
|
|
/// <summary>Nothing to copy (or to resume): no lease was taken, no run recorded.</summary>
|
|
NothingToDo,
|
|
|
|
/// <summary>
|
|
/// Something else comes first: a job, an engine session, a change of destination, other long work
|
|
/// (<see cref="AutoExportStart.BusyWith"/>), or Alberto's work displaced the start while it planned.
|
|
/// </summary>
|
|
Busy,
|
|
|
|
/// <summary>«Interrompi» stopped the automatic copies until <c>ExportService.AutomaticHeldUntil</c> (answer 53).</summary>
|
|
Held,
|
|
}
|
|
|
|
/// <summary>The answer of <c>ExportService.TryStartAutomaticAsync</c> and <c>TryResumeAutomaticAsync</c>.</summary>
|
|
/// <param name="BusyWith">The long work that holds the gate, when that is why the start is <see cref="AutoExportStartOutcome.Busy"/>.</param>
|
|
public sealed record AutoExportStart(AutoExportStartOutcome Outcome, IReadOnlyList<ExportRun> Runs, LongWorkKind? BusyWith = null);
|
|
|
|
/// <summary>What <c>ExportService.StartAsync</c> did: one run per destination (D6), and the plan it followed.</summary>
|
|
public sealed record ExportStart(IReadOnlyList<ExportRun> Runs, ExportPlan Plan);
|
|
|
|
/// <summary>What «Riprova» of a paused destination did: the reason it is still paused (null: it answers), and the jobs resumed.</summary>
|
|
public sealed record ExportRetry(string? Problem, IReadOnlyList<ExportRun> Resumed);
|
|
|
|
/// <summary>The export job ended (however it ended): the runs it worked on, and whether copies may have changed.</summary>
|
|
public sealed record ExportJobFinished(IReadOnlyList<long> RunIds, bool CopiesChanged);
|