Files
Chronos/src/Titano.Data/Archiving/ArchiveService.cs
T
Claude b76b745d7d fix(archive): the orphans fix queues the deep analysis a scan would
A desktop scan queues a background deep analysis of the sequences whose
frames it changed when Settings > Analysis > After a scan is on; photos the
Archive screen's orphans fix indexed got only the light analysis, which then
dropped the deep status. ArchiveService.ReconcileAsync takes that setting
and queues the deep analysis too, at background priority. A follow-up is
left out when a queued job of its kind already covers the sequence, whatever
else that job targets, as the scan's follow-ups check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-12 23:58:19 +00:00

490 lines
32 KiB
C#

using System.Text.Json;
using Titano.Core.Archiving;
using Titano.Core.Diagnostics;
using Titano.Core.Import;
using Titano.Core.Model;
using Titano.Core.Naming;
using Titano.Data.Archiving.Jobs;
using Titano.Data.Jobs;
using Titano.Data.Processing;
using Titano.Data.Repositories;
namespace Titano.Data.Archiving;
/// <summary>
/// What the desktop client and the server call to work with archives (BRIEF §7, §12.4, §12.5, §14.4): open, create
/// or adopt an archive, read and change its settings, plan and apply mass renames, plan imports and card erases, delete
/// sequences into the trash and restore them, query duplicates and orphans, and
/// the job handlers for the long operations (enqueue them with <see cref="ArchiveJobs"/>).
/// </summary>
public sealed class ArchiveService : IJobHandlerProvider
{
/// <summary>Lease serialising read-modify-write of <c>archive.json</c> between the processes sharing an archive.</summary>
private const string SettingsLease = "archive-settings";
/// <summary>Times a title, tags, rating or notes edit is applied at the version it read before it writes regardless.</summary>
private const int InfoEditAttempts = 3;
private readonly TitanoDatabase _db;
private readonly SequenceWriter _writer;
public ArchiveService(TitanoDatabase db, ArchiveServiceOptions? options = null)
{
_db = db;
_writer = new SequenceWriter(options ?? new ArchiveServiceOptions());
Indexer = new ArchiveIndexer(db, _writer);
Ingest = new ArchiveIngest(db, _writer, Indexer);
Verifier = new ArchiveVerifier(db, _writer, Indexer);
Maintenance = new ArchiveMaintenance(db, _writer, Indexer);
Renamer = new MassRenamer(db, _writer, Indexer);
Images = new ArchiveImages(db, _writer, Indexer);
Imports = new ImportService(db, Ingest);
Trash = new SequenceTrash(db, _writer, Indexer);
}
public ArchiveIndexer Indexer { get; }
/// <summary>Commits staged files into sequences (server uploads, client card imports).</summary>
public ArchiveIngest Ingest { get; }
public ArchiveVerifier Verifier { get; }
public ArchiveMaintenance Maintenance { get; }
public MassRenamer Renamer { get; }
public ArchiveImages Images { get; }
public ImportService Imports { get; }
/// <summary>Deletes sequences into the archive's trash and restores them.</summary>
public SequenceTrash Trash { get; }
/// <summary>Handlers for scan, rebuild, verify, import, thumbs, proxies and maintenance jobs; register them on the <see cref="JobEngine"/>.</summary>
public IReadOnlyList<IJobHandler> CreateJobHandlers() =>
[
new ScanJobHandler(_db, Indexer, JobKind.Scan, _writer.Options.ImagesAfterScan),
new ScanJobHandler(_db, Indexer, JobKind.Rebuild, _writer.Options.ImagesAfterScan),
new VerifyJobHandler(_db, Verifier),
new ImportJobHandler(_db, Imports),
new ImageJobHandler(_db, Images, JobKind.Thumbs),
new ImageJobHandler(_db, Images, JobKind.Proxies),
new MaintenanceJobHandler(_db, Maintenance),
];
/// <summary>Makes <paramref name="root"/> a new, empty archive and indexes it.</summary>
/// <exception cref="InvalidOperationException">The folder already is an archive (open it instead).</exception>
public Task<Archive> CreateAsync(string root, string name, LayoutTemplates? templates = null, ProxyPolicy? proxies = null,
CancellationToken cancellationToken = default) =>
CreateAsync(root, new ArchiveSettings(name, templates ?? new LayoutTemplates(), proxies ?? new ProxyPolicy(), new ThumbnailPolicy()), cancellationToken);
/// <summary>
/// Makes <paramref name="root"/> a new, empty archive with <paramref name="settings"/> and indexes it. A blank name
/// becomes the folder's name; a longer one than <see cref="ArchiveSettings.MaxNameLength"/> is shortened.
/// </summary>
/// <exception cref="InvalidOperationException">The folder already is an archive (open it instead).</exception>
/// <exception cref="ArgumentException">A template or policy is invalid (<see cref="ArgumentException.ParamName"/> names it).</exception>
public async Task<Archive> CreateAsync(string root, ArchiveSettings settings, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(settings);
var paths = new ArchivePaths(root);
settings = WithUsableName(settings, paths);
if (settings.Problem() is { } field) throw new ArgumentException($"The archive setting {field} is invalid.", field);
if (File.Exists(paths.ManifestPath)) throw new InvalidOperationException($"{paths.Root} already is a Titano archive.");
Directory.CreateDirectory(paths.MetaDirectory);
var manifest = new ArchiveManifest
{
Id = Guid.CreateVersion7(),
Name = settings.Name.Trim(),
Created = DateTimeOffset.UtcNow,
LayoutTemplates = settings.Templates,
ProxyPolicy = settings.Proxies,
ThumbnailPolicy = settings.Thumbnails,
};
ManifestStore.WriteArchive(paths.ManifestPath, manifest);
return await RegisterAsync(paths, manifest, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Registers an existing archive (or relinks a moved one: same id, new root) and completes renames a crash
/// interrupted. An offline archive is registered as it was last seen.
/// </summary>
public async Task<Archive> OpenAsync(string root, CancellationToken cancellationToken = default)
{
var paths = new ArchivePaths(root);
var archive = await RegisterAsync(paths, ManifestStore.ReadArchive(paths.ManifestPath), cancellationToken).ConfigureAwait(false);
if (ArchiveVolume.Probe(paths) != ArchiveAvailability.Online) return archive;
try
{
await Renamer.ResumePendingAsync(archive, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException)
{
// The journal stays; the next open tries again. The archive itself is usable meanwhile.
Log.Warning("archive", $"Resuming an interrupted rename in {paths.Root} failed", ex);
}
return archive;
}
/// <summary>
/// Opens a folder of photos as an archive in place (Welcome "adopt folder"): nothing is moved or renamed; enqueue
/// <see cref="ArchiveJobs.Scan"/> with a full pass to write sidecars and index it.
/// </summary>
public Task<Archive> AdoptAsync(string root, string? name = null, CancellationToken cancellationToken = default)
{
var paths = new ArchivePaths(root);
return File.Exists(paths.ManifestPath)
? OpenAsync(root, cancellationToken)
: CreateAsync(root, name ?? Path.GetFileName(paths.Root), null, null, cancellationToken);
}
/// <summary>
/// <see cref="AdoptAsync(string, string?, CancellationToken)"/> for a folder that may become a new archive with
/// <paramref name="defaults"/> (templates and policies; the folder's name when its name is blank). An existing
/// archive keeps its own settings.
/// </summary>
public Task<Archive> AdoptAsync(string root, ArchiveSettings defaults, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(defaults);
return File.Exists(new ArchivePaths(root).ManifestPath) ? OpenAsync(root, cancellationToken) : CreateAsync(root, defaults, cancellationToken);
}
/// <summary>
/// The settings in the archive's <c>archive.json</c>; those of the index (with the default thumbnail policy, which only
/// the file keeps) while the file cannot be read.
/// </summary>
public async Task<ArchiveSettings> GetSettingsAsync(Guid archiveId, CancellationToken cancellationToken = default)
{
var archive = await LoadArchiveAsync(archiveId, cancellationToken).ConfigureAwait(false);
return TryReadManifest(archive) is { } manifest
? ArchiveSettings.Of(manifest)
: new ArchiveSettings(archive.Name, new LayoutTemplates { Folder = archive.FolderTemplate, File = archive.FileTemplate },
new ProxyPolicy { LongEdge = archive.ProxyLongEdge }, new ThumbnailPolicy());
}
/// <summary>
/// The settings in the archive's <c>archive.json</c>, with its index row brought up to them when another process
/// sharing the archive changed the file (a server's admin panel, another PC's Settings): imports and uploads take their
/// templates and proxy size from that row. Null while the file cannot be read (offline, damaged, newer, of another
/// archive): the row stays as it is, and only <see cref="GetSettingsAsync"/>'s stand-ins are known.
/// </summary>
public async Task<ArchiveSettings?> RefreshSettingsAsync(Guid archiveId, CancellationToken cancellationToken = default)
{
var archive = await LoadArchiveAsync(archiveId, cancellationToken).ConfigureAwait(false);
if (TryReadManifest(archive) is not { } manifest) return null;
var settings = ArchiveSettings.Of(manifest);
if (!settings.IsRegisteredIn(archive))
{
// Read without the settings lease, as at open: an edit made meanwhile writes its own row, and the next refresh
// settles a file that changes again.
await RegisterAsync(new ArchivePaths(archive.RootPath), manifest, cancellationToken).ConfigureAwait(false);
Log.Info("archive", $"Settings of {archive.RootPath} changed elsewhere: {manifest.LayoutTemplates.Folder} · {manifest.LayoutTemplates.File}, proxies {manifest.ProxyPolicy.LongEdge}");
}
return settings;
}
/// <summary>
/// Changes an archive's settings in its <c>archive.json</c> (the source of truth, BRIEF §7), then in the index.
/// <paramref name="edit"/> receives the settings read from the file under the archive's settings lease, so a server and
/// a client sharing the archive never undo each other's change; the file is replaced atomically. Folders and files
/// already in the archive keep their names: templates and policies apply to the next imports, proxies and thumbnails.
/// A blank or overlong name in the file (archives made at a drive root, or before names were checked) is made usable
/// as <see cref="CreateAsync(string, ArchiveSettings, CancellationToken)"/> does, instead of failing every edit.
/// </summary>
/// <exception cref="ArgumentException">An edited setting is invalid (<see cref="ArgumentException.ParamName"/> names it).</exception>
/// <exception cref="ArchiveOfflineException">The archive is offline or read-only.</exception>
/// <exception cref="LeaseUnavailableException">Another process kept the archive's settings busy.</exception>
public async Task<Archive> UpdateSettingsAsync(Guid archiveId, Func<ArchiveSettings, ArchiveSettings> edit, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(edit);
var archive = await LoadArchiveAsync(archiveId, cancellationToken).ConfigureAwait(false);
var paths = new ArchivePaths(archive.RootPath);
ArchiveVolume.EnsureWritable(paths);
// The index row is written under the same lease as the file, so two edits cannot leave them in opposite orders.
using (await LeaseFile.AcquireAsync(paths.LocksDirectory, SettingsLease, _writer.Options.Owner, _writer.Options.LeaseWait, _writer.Options.LeaseDuration,
cancellationToken).ConfigureAwait(false))
{
var current = ManifestStore.ReadArchive(paths.ManifestPath);
if (current.Id != archive.Id) throw new InvalidDataException($"{paths.ManifestPath} belongs to archive {current.Id}, not {archive.Id}.");
// The name read is made usable before the edit sees it; a name the edit sets is checked as given.
var settings = edit(WithUsableName(ArchiveSettings.Of(current), paths));
if (settings.Problem() is { } field) throw new ArgumentException($"The archive setting {field} is invalid.", field);
var manifest = current with
{
Name = settings.Name.Trim(),
LayoutTemplates = settings.Templates,
ProxyPolicy = settings.Proxies,
ThumbnailPolicy = settings.Thumbnails,
};
if (manifest != current) ManifestStore.WriteArchive(paths.ManifestPath, manifest);
Log.Info("archive", $"Settings of {paths.Root}: {manifest.LayoutTemplates.Folder} · {manifest.LayoutTemplates.File}, proxies {manifest.ProxyPolicy.LongEdge}, thumbnails {manifest.ThumbnailPolicy.LongEdge}");
return await RegisterAsync(paths, manifest, cancellationToken).ConfigureAwait(false);
}
}
public static ArchiveAvailability GetAvailability(Archive archive) => ArchiveVolume.Probe(new ArchivePaths(archive.RootPath));
/// <summary>Changes title, tags, rating and notes in the sidecar and the index together (settings version + 1).</summary>
public Task<Sequence> SetSequenceInfoAsync(Guid sequenceId, string? title, IReadOnlyList<string> tags, int rating, string? notes,
CancellationToken cancellationToken = default) =>
EditSequenceInfoAsync(sequenceId, _ => new SequenceInfo(title, tags, rating, notes), cancellationToken);
/// <summary>
/// Changes some of title, tags, rating and notes from their current values — read under the sequence's lease from
/// the sidecar, or from the index when it is ahead (an edit made there only) — so fields <paramref name="edit"/>
/// leaves alone keep any change made since the caller read the sequence. Sidecar and index change together
/// (settings version + 1), the index row in these fields only, at the settings version read under the lease
/// (<see cref="SequenceRepository.EditAsync(Guid, SequenceEdit, int?, CancellationToken)"/>): a settings edit committed
/// since the caller read the sequence (an analysis's Auto values, the server's PATCH, which takes no lease) stays, and
/// so do frames another process listed in the sidecar, which the next scan indexes
/// (<see cref="ArchiveIndexer.InStepAsync(Archive, SequencePaths, SequenceManifest, CancellationToken)"/>).
/// <paramref name="edit"/> runs again on top of an edit the API committed meanwhile.
/// </summary>
/// <exception cref="ArgumentException">The sequence is not in the index (or left it before the edit).</exception>
public async Task<Sequence> EditSequenceInfoAsync(Guid sequenceId, Func<SequenceInfo, SequenceInfo> edit, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(edit);
var (archive, sequence) = await LoadSequenceAsync(sequenceId, cancellationToken).ConfigureAwait(false);
var paths = new ArchivePaths(archive.RootPath);
ArchiveVolume.EnsureWritable(paths);
var folder = paths.Sequence(sequence.Folder);
await using (await _writer.LockAsync(paths, sequenceId, cancellationToken).ConfigureAwait(false))
{
var manifest = SequenceWriter.ReadLocked(folder, sequenceId);
var inStep = await Indexer.InStepAsync(archive, folder, manifest, cancellationToken).ConfigureAwait(false);
for (var attempt = 1; ; attempt++)
{
var row = await _db.Sequences.GetAsync(sequenceId, cancellationToken).ConfigureAwait(false) ?? throw Unknown(sequenceId);
var current = row.SettingsVersion > manifest.Versions.Settings
? new SequenceInfo(row.Title, row.Tags, row.Rating, row.Notes)
: new SequenceInfo(manifest.Title, manifest.Tags, manifest.Rating, manifest.Notes);
var info = edit(current);
var title = string.IsNullOrWhiteSpace(info.Title) ? null : info.Title.Trim();
// The last attempt writes over whatever the index holds then rather than fail the user's edit.
var result = await _db.Sequences.EditAsync(sequenceId,
new SequenceEdit { Title = title ?? string.Empty, Tags = info.Tags, Rating = info.Rating, Notes = info.Notes ?? string.Empty },
attempt < InfoEditAttempts ? row.SettingsVersion : null, cancellationToken).ConfigureAwait(false);
// A conflict means the API changed the sequence after the read: the edit applies again on top of that.
if (result.Outcome == SequenceEditOutcome.Conflict) continue;
if (result.Sequence is not { } updated) throw Unknown(sequenceId);
// The sidecar takes the fields as the index stored them (trimmed, tags without duplicates).
ManifestStore.WriteSequence(folder.ManifestPath, manifest with
{
Title = updated.Title,
Tags = updated.Tags,
Rating = updated.Rating,
Notes = updated.Notes,
Versions = manifest.Versions with { Settings = System.Math.Max(manifest.Versions.Settings + 1, updated.SettingsVersion) },
UpdatedAt = DateTimeOffset.UtcNow,
});
if (inStep) await Indexer.RecordStampsAsync(archive, folder, sequenceId, cancellationToken).ConfigureAwait(false);
return updated;
}
}
}
public Task<IReadOnlyList<DuplicateGroup>> FindDuplicatesAsync(Guid archiveId, CancellationToken cancellationToken = default) =>
Maintenance.FindDuplicatesAsync(archiveId, cancellationToken);
public async Task<OrphanReport> FindOrphansAsync(Guid archiveId, CancellationToken cancellationToken = default) =>
await Maintenance.FindOrphansAsync(await LoadArchiveAsync(archiveId, cancellationToken).ConfigureAwait(false), cancellationToken).ConfigureAwait(false);
/// <summary>
/// Orphans fix: index the unlisted photos of a sequence and flag its missing files. Photos added, gone or back clear
/// what covered the frames, as a scan's reconcile does (<see cref="ArchiveIndexer.ReconcileAsync"/>), and what a scan's
/// follow-ups would make again is queued on <paramref name="followUps"/> (null: nothing): the light analysis, a
/// background deep analysis when <paramref name="deepAnalysis"/> asks (as <see cref="ScanParameters.DeepAnalysis"/>
/// does for a scan) and, on a server (<see cref="ArchiveServiceOptions.ImagesAfterScan"/>), the thumbnails and proxies.
/// A kind the engine does not run, or one a queued job of that kind already covers for the sequence, is left out.
/// </summary>
public async Task ReconcileAsync(Guid sequenceId, JobEngine? followUps, bool deepAnalysis, CancellationToken cancellationToken = default)
{
var (archive, sequence) = await LoadSequenceAsync(sequenceId, cancellationToken).ConfigureAwait(false);
if (!await Indexer.ReconcileAsync(archive, sequence, cancellationToken).ConfigureAwait(false) || followUps is null) return;
List<ArchiveJobRequest> requests = [ProcessingJobs.LightAnalysis([sequenceId], sequence.DisplayTitle)];
// BRIEF §10.2: background, low priority.
if (deepAnalysis) requests.Add(ProcessingJobs.DeepAnalysis([sequenceId], sequence.DisplayTitle) with { Priority = JobPriority.Background });
if (_writer.Options.ImagesAfterScan)
{
requests.Add(ArchiveJobs.Thumbnails([sequenceId], sequence.DisplayTitle));
// An archive whose policy makes no proxies would get a pass that does nothing.
if (archive.ProxyLongEdge != ProxyPolicy.Disabled) requests.Add(ArchiveJobs.Proxies([sequenceId], sequence.DisplayTitle));
}
foreach (var request in requests)
{
if (!followUps.Runs(request.Kind)) continue;
// A queued job runs after the fix, on the frames as they are then, whatever else it targets (a scan's follow-ups).
var queued = await _db.Jobs.ListQueuedAsync(request.Kind, 1000, cancellationToken).ConfigureAwait(false);
if (queued.Any(j => j.TargetIds.Contains(sequenceId))) continue;
await followUps.EnqueueAsync(request, cancellationToken: cancellationToken).ConfigureAwait(false);
}
}
/// <summary>Orphans fix: drop index rows whose sidecar is gone.</summary>
public async Task<int> RemoveMissingSequencesAsync(Guid archiveId, IReadOnlyList<Guid> sequenceIds, CancellationToken cancellationToken = default) =>
await Maintenance.RemoveMissingSequencesAsync(await LoadArchiveAsync(archiveId, cancellationToken).ConfigureAwait(false), sequenceIds, cancellationToken)
.ConfigureAwait(false);
/// <summary>Dry run of a mass rename.</summary>
public async Task<RenamePlan> PlanRenameAsync(Guid archiveId, IReadOnlyList<Guid> sequenceIds, string? folderTemplate, string? titleTemplate,
CancellationToken cancellationToken = default) =>
await Renamer.PlanAsync(await LoadArchiveAsync(archiveId, cancellationToken).ConfigureAwait(false), sequenceIds, folderTemplate, titleTemplate,
cancellationToken).ConfigureAwait(false);
public async Task ApplyRenameAsync(RenamePlan plan, IProgress<int>? progress = null, CancellationToken cancellationToken = default) =>
await Renamer.ApplyAsync(await LoadArchiveAsync(plan.ArchiveId, cancellationToken).ConfigureAwait(false), plan, progress, cancellationToken)
.ConfigureAwait(false);
/// <summary>Dry run of moving sequences to their archive's trash; <paramref name="progress"/> counts the sequences checked.</summary>
public Task<DeletePlan> PlanDeleteAsync(IReadOnlyList<Guid> sequenceIds, IProgress<int>? progress = null, CancellationToken cancellationToken = default) =>
Trash.PlanAsync(sequenceIds, progress, cancellationToken);
/// <summary>Moves exactly the planned sequences to the trash; returns the trash entries made.</summary>
public Task<IReadOnlyList<TrashEntry>> DeleteAsync(DeletePlan plan, string? deletedBy = null, IProgress<int>? progress = null,
CancellationToken cancellationToken = default) =>
Trash.DeleteAsync(plan, deletedBy, progress, cancellationToken);
/// <summary>The sequences in an archive's trash, newest first.</summary>
public async Task<IReadOnlyList<TrashEntry>> ListTrashAsync(Guid archiveId, CancellationToken cancellationToken = default) =>
await Trash.ListAsync(await LoadArchiveAsync(archiveId, cancellationToken).ConfigureAwait(false), cancellationToken).ConfigureAwait(false);
/// <summary>Moves a sequence back from the trash and indexes it again.</summary>
public async Task<Sequence> RestoreAsync(Guid archiveId, string entryName, CancellationToken cancellationToken = default) =>
await Trash.RestoreAsync(await LoadArchiveAsync(archiveId, cancellationToken).ConfigureAwait(false), entryName, cancellationToken).ConfigureAwait(false);
/// <summary>Plans a card or folder import into an archive (nothing is copied).</summary>
public async Task<ImportPlan> PlanImportAsync(Guid archiveId, string sourceRoot, ImportOptions options, Guid? cardId = null, IProgress<int>? progress = null,
CancellationToken cancellationToken = default) =>
await Imports.PlanAsync(await LoadArchiveAsync(archiveId, cancellationToken).ConfigureAwait(false), sourceRoot, options, cardId, progress,
cancellationToken).ConfigureAwait(false);
/// <summary>Dry run of "delete imported" for a card; pass the plan to <see cref="CardEraser.Execute"/> after the user confirms.</summary>
public async Task<ErasePlan> PlanCardEraseAsync(Guid archiveId, Guid cardId, string sourceRoot, IProgress<int>? progress = null,
CancellationToken cancellationToken = default) =>
await Imports.PlanEraseAsync(await LoadArchiveAsync(archiveId, cancellationToken).ConfigureAwait(false), cardId, sourceRoot, progress,
cancellationToken).ConfigureAwait(false);
/// <summary>
/// <paramref name="settings"/> with a name an archive can keep: a blank one becomes the folder's name (the root itself
/// for a drive root), one longer than <see cref="ArchiveSettings.MaxNameLength"/> is shortened.
/// </summary>
private static ArchiveSettings WithUsableName(ArchiveSettings settings, ArchivePaths paths)
{
var name = string.IsNullOrWhiteSpace(settings.Name) ? Path.GetFileName(paths.Root) is { Length: > 0 } folder ? folder : paths.Root : settings.Name.Trim();
return settings with { Name = name.Length > ArchiveSettings.MaxNameLength ? name[..ArchiveSettings.MaxNameLength] : name };
}
private async Task<Archive> RegisterAsync(ArchivePaths paths, ArchiveManifest manifest, CancellationToken cancellationToken)
{
var archive = new Archive
{
Id = manifest.Id,
Name = manifest.Name,
RootPath = paths.Root,
FolderTemplate = manifest.LayoutTemplates.Folder,
FileTemplate = manifest.LayoutTemplates.File,
SchemaVersion = manifest.SchemaVersion,
Created = manifest.Created,
ProxyLongEdge = manifest.ProxyPolicy.LongEdge,
};
await _db.Archives.UpsertAsync(archive, cancellationToken).ConfigureAwait(false);
return archive;
}
/// <summary>
/// The archive's <c>archive.json</c>; null (logged) while it cannot be read, when a newer Titano wrote it (this one
/// would drop what it does not know) or when it belongs to another archive.
/// </summary>
private static ArchiveManifest? TryReadManifest(Archive archive)
{
var paths = new ArchivePaths(archive.RootPath);
try
{
if (!File.Exists(paths.ManifestPath)) return null;
var manifest = ManifestStore.ReadArchive(paths.ManifestPath);
if (manifest.SchemaVersion > ArchiveManifest.CurrentSchemaVersion)
Log.Warning("archive", $"{paths.ManifestPath} has schema version {manifest.SchemaVersion}, newer than this Titano's; the index's settings are used");
else if (manifest.Id == archive.Id) return manifest;
else Log.Warning("archive", $"{paths.ManifestPath} belongs to archive {manifest.Id}, not {archive.Id}; the index's settings are used");
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException)
{
Log.Warning("archive", $"The settings of {paths.Root} cannot be read; the index's are used", ex);
}
return null;
}
private async Task<Archive> LoadArchiveAsync(Guid archiveId, CancellationToken cancellationToken) =>
await _db.Archives.GetAsync(archiveId, cancellationToken).ConfigureAwait(false) ?? throw new ArgumentException($"Unknown archive {archiveId}.", nameof(archiveId));
private async Task<(Archive, Sequence)> LoadSequenceAsync(Guid sequenceId, CancellationToken cancellationToken)
{
var sequence = await _db.Sequences.GetAsync(sequenceId, cancellationToken).ConfigureAwait(false) ?? throw Unknown(sequenceId);
return (await LoadArchiveAsync(sequence.ArchiveId, cancellationToken).ConfigureAwait(false), sequence);
}
private static ArgumentException Unknown(Guid sequenceId) => new($"Unknown sequence {sequenceId}.", nameof(sequenceId));
}
/// <summary>The fields of a sequence the user describes it with (sidecar and index).</summary>
public sealed record SequenceInfo(string? Title, IReadOnlyList<string> Tags, int Rating, string? Notes);
/// <summary>
/// The settings an archive keeps in <c>archive.json</c> (BRIEF §7, §8.5): its name, the naming templates of new imports
/// and the policies of the proxies and thumbnails built from now on.
/// </summary>
public sealed record ArchiveSettings(string Name, LayoutTemplates Templates, ProxyPolicy Proxies, ThumbnailPolicy Thumbnails)
{
public const int MaxNameLength = 100;
public static ArchiveSettings Of(ArchiveManifest manifest) => new(manifest.Name, manifest.LayoutTemplates, manifest.ProxyPolicy, manifest.ThumbnailPolicy);
/// <summary>Whether the index row <paramref name="archive"/> holds these settings (all it keeps: not the thumbnail policy).</summary>
public bool IsRegisteredIn(Archive archive)
{
ArgumentNullException.ThrowIfNull(archive);
return Name == archive.Name && Templates.Folder == archive.FolderTemplate && Templates.File == archive.FileTemplate
&& Proxies.LongEdge == archive.ProxyLongEdge;
}
/// <summary>The first invalid field by its API name (<c>name</c>, <c>folderTemplate</c>, <c>fileTemplate</c>,
/// <c>proxyLongEdge</c>, <c>thumbnailLongEdge</c>); null when every field is valid.</summary>
public string? Problem()
{
if (string.IsNullOrWhiteSpace(Name) || Name.Trim().Length > MaxNameLength) return "name";
if (string.IsNullOrWhiteSpace(Templates.Folder) || !PathTemplate.Parse(Templates.Folder).IsValid) return "folderTemplate";
if (string.IsNullOrWhiteSpace(Templates.File) || !PathTemplate.Parse(Templates.File).IsValid) return "fileTemplate";
if (!ProxyPolicy.IsValidLongEdge(Proxies.LongEdge) || Proxies.Quality is < 1 or > 100) return "proxyLongEdge";
if (!ThumbnailPolicy.IsValidLongEdge(Thumbnails.LongEdge) || Thumbnails.Quality is < 1 or > 100) return "thumbnailLongEdge";
return null;
}
}
/// <summary>Builds the archive jobs; enqueue with <see cref="EnqueueAsync"/>.</summary>
public static class ArchiveJobs
{
/// <param name="deepAnalysis">Also queue a background deep analysis of the sequences the scan indexes (see <see cref="ScanParameters"/>).</param>
public static ArchiveJobRequest Scan(Archive archive, bool full = false, bool deepAnalysis = false) =>
new(JobKind.Scan, [archive.Id], archive.Name, Json(new ScanParameters(full, deepAnalysis), ArchiveJobJson.Default.ScanParameters), JobPriority.User);
public static ArchiveJobRequest Rebuild(Archive archive) => new(JobKind.Rebuild, [archive.Id], archive.Name, null, JobPriority.User);
public static ArchiveJobRequest Verify(Archive archive, IReadOnlyList<Guid>? sequenceIds = null) =>
new(JobKind.Verify, [archive.Id], archive.Name, Json(new VerifyParameters(sequenceIds), ArchiveJobJson.Default.VerifyParameters), JobPriority.User);
public static ArchiveJobRequest Import(Archive archive, ImportPlan plan, Guid? cardId, bool verify = true, Guid? userId = null) =>
new(JobKind.Import, [archive.Id], archive.Name,
Json(new ImportJobParameters(archive.Id, plan, cardId, verify, userId), ArchiveJobJson.Default.ImportJobParameters), JobPriority.User);
public static ArchiveJobRequest Thumbnails(IReadOnlyList<Guid> sequenceIds, string? label = null, bool force = false) =>
new(JobKind.Thumbs, sequenceIds, label, Json(new ImageJobParameters(force), ArchiveJobJson.Default.ImageJobParameters), JobPriority.Background);
public static ArchiveJobRequest Proxies(IReadOnlyList<Guid> sequenceIds, string? label = null, bool force = false) =>
new(JobKind.Proxies, sequenceIds, label, Json(new ImageJobParameters(force), ArchiveJobJson.Default.ImageJobParameters), JobPriority.Background);
public static ArchiveJobRequest Maintenance(Archive archive, bool dryRun = false) =>
new(JobKind.Maintenance, [archive.Id], archive.Name, Json(new MaintenanceParameters(dryRun), ArchiveJobJson.Default.MaintenanceParameters),
JobPriority.Normal);
public static Task<Job> EnqueueAsync(this JobEngine engine, ArchiveJobRequest request, Guid? createdBy = null, CancellationToken cancellationToken = default) =>
engine.EnqueueAsync(request.Kind, request.Targets, request.Label, request.ParametersJson, request.Priority, createdBy, cancellationToken);
private static string Json<T>(T value, System.Text.Json.Serialization.Metadata.JsonTypeInfo<T> typeInfo) => JsonSerializer.Serialize(value, typeInfo);
}