M7 Importazione: sorgenti con sola lettura riconosciuta, analisi in sola lettura interrompibile e ripristinabile (tipo dal contenuto, SHA-256, impronta, date da nome, EXIF scritto a mano, mvhd e ffprobe facoltativo), riconoscitori Febe, generico e 4K Stogram come ipotesi spenta, confronto con la libreria e dentro la sorgente, associazione cartelle-account, decisioni sui doppioni, copia o spostamento verificati con sostituzione sicura, resoconto e CSV; prove
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
32 files changed
+7707
-3
No files matched your search
@@ -0,0 +1,169 @@
|
||||
using System.Globalization;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text.Json.Nodes;
|
||||
using Febe.Core.Abstractions;
|
||||
using Febe.Core.Library;
|
||||
using Febe.Core.Storage;
|
||||
|
||||
namespace Febe.Core.Import;
|
||||
|
||||
/// <summary>
|
||||
/// What the import needs from the rest of Febe. Built once by the composition root (Server, Desktop) with the same
|
||||
/// library objects the collection uses, so thumbnails, hashes and <c>_febe.jsonl</c> lines are made the same way.
|
||||
/// </summary>
|
||||
public sealed record ImportEnvironment
|
||||
{
|
||||
public required IFebeStorage Storage { get; init; }
|
||||
|
||||
/// <summary>Library folder and account folders.</summary>
|
||||
public required LibraryLayout Layout { get; init; }
|
||||
|
||||
/// <summary>The two naming schemes of the library settings.</summary>
|
||||
public LibraryNaming Naming { get; init; } = LibraryNaming.Default;
|
||||
|
||||
public required AccountJournal Journal { get; init; }
|
||||
|
||||
public required IClock Clock { get; init; }
|
||||
|
||||
public ThumbnailStore? Thumbnails { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// The in-memory index of the library's perceptual hashes, shared with the collection. When null the analysis loads
|
||||
/// its own copy from the database (and imported photos are not added to any shared index).
|
||||
/// </summary>
|
||||
public PerceptualHashIndex? HashIndex { get; init; }
|
||||
|
||||
/// <summary>ffmpeg, for thumbnails of imported videos (spec §8.4). Null: the neutral tile.</summary>
|
||||
public VideoFrameExtractor? Frames { get; init; }
|
||||
|
||||
/// <summary>ffprobe, an optional extra for videos the hand-written reader does not cover.</summary>
|
||||
public IVideoMetadataProbe? VideoProbe { get; init; }
|
||||
|
||||
/// <summary>«Imposta la data del file a quella di pubblicazione» (library setting).</summary>
|
||||
public bool SetFileTimeToPublication { get; init; } = true;
|
||||
|
||||
/// <summary>Where sources may be chosen (Docker: <c>/importa</c>).</summary>
|
||||
public ImportSourceOptions Sources { get; init; } = ImportSourceOptions.AnyFolder;
|
||||
|
||||
/// <summary>The Data folder: a source may not overlap it.</summary>
|
||||
public string? DataFolder { get; init; }
|
||||
|
||||
/// <summary>Read-only detection of sources.</summary>
|
||||
public ISourceAccessProbe? AccessProbe { get; init; }
|
||||
|
||||
/// <summary>Reading, hashing and deleting source files.</summary>
|
||||
public IImportFileSystem FileSystem { get; init; } = ImportFileSystem.Instance;
|
||||
|
||||
/// <summary>Files analysed at the same time (hashing and decoding).</summary>
|
||||
public int Parallelism { get; init; } = Math.Clamp(Environment.ProcessorCount, 1, 4);
|
||||
|
||||
/// <summary>The checker of sources for this environment.</summary>
|
||||
public ImportSources CreateSources() => new(Sources, Layout.Root, DataFolder, AccessProbe);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// File operations of the import on the source and on the copies. A separate seam so that the safety rule of «Sposta»
|
||||
/// (the original goes only after the copy has been verified with SHA-256) can be tested with a corrupted copy.
|
||||
/// </summary>
|
||||
public interface IImportFileSystem
|
||||
{
|
||||
/// <summary>Opens a source file for reading only.</summary>
|
||||
Stream OpenRead(string fullPath);
|
||||
|
||||
/// <summary>SHA-256 (lowercase hex) of a file as it is on disk now.</summary>
|
||||
Task<string> ComputeSha256Async(string fullPath, CancellationToken ct);
|
||||
|
||||
/// <summary>Deletes an original of «Sposta», after its copy was verified.</summary>
|
||||
void DeleteOriginal(string fullPath);
|
||||
}
|
||||
|
||||
/// <summary>The real file system.</summary>
|
||||
public sealed class ImportFileSystem : IImportFileSystem
|
||||
{
|
||||
private const int BufferSize = 128 * 1024;
|
||||
|
||||
private ImportFileSystem()
|
||||
{
|
||||
}
|
||||
|
||||
public static ImportFileSystem Instance { get; } = new();
|
||||
|
||||
public Stream OpenRead(string fullPath) =>
|
||||
new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read, BufferSize, FileOptions.Asynchronous | FileOptions.SequentialScan);
|
||||
|
||||
public async Task<string> ComputeSha256Async(string fullPath, CancellationToken ct)
|
||||
{
|
||||
await using var stream = OpenRead(fullPath);
|
||||
return Convert.ToHexStringLower(await SHA256.HashDataAsync(stream, ct).ConfigureAwait(false));
|
||||
}
|
||||
|
||||
public void DeleteOriginal(string fullPath) => File.Delete(fullPath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Choices of an import that have no column in <c>import_run</c>, kept in the <c>setting</c> table under
|
||||
/// <c>importazione.{id}.*</c>: whether the 4K Stogram hypothesis is on, and the folders that go to a new «Solo archivio»
|
||||
/// account (created only when the copy starts, so an abandoned import leaves no empty account behind).
|
||||
/// </summary>
|
||||
internal static class ImportRunSettings
|
||||
{
|
||||
public static string StogramKey(long runId) => string.Create(CultureInfo.InvariantCulture, $"importazione.{runId}.ipotesi-stogram");
|
||||
|
||||
public static string NewAccountsKey(long runId) => string.Create(CultureInfo.InvariantCulture, $"importazione.{runId}.nuovi-account");
|
||||
|
||||
public static Task<bool> GetStogramAsync(ISettingsStore store, long runId, CancellationToken ct) =>
|
||||
store.GetBoolAsync(StogramKey(runId), false, ct);
|
||||
|
||||
public static Task SetStogramAsync(ISettingsStore store, long runId, bool value, CancellationToken ct) =>
|
||||
store.SetAsync(StogramKey(runId), value ? "true" : null, ct);
|
||||
|
||||
/// <summary>Folder → user name of the new «Solo archivio» account it goes to.</summary>
|
||||
public static async Task<IReadOnlyDictionary<string, string>> GetNewAccountsAsync(ISettingsStore store, long runId, CancellationToken ct)
|
||||
{
|
||||
var text = await store.GetAsync(NewAccountsKey(runId), ct).ConfigureAwait(false);
|
||||
var result = new Dictionary<string, string>(StringComparer.Ordinal);
|
||||
if (string.IsNullOrWhiteSpace(text))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (JsonNode.Parse(text) is JsonObject json)
|
||||
{
|
||||
foreach (var (folder, value) in json)
|
||||
{
|
||||
if (value?.GetValue<string>() is { Length: > 0 } username)
|
||||
{
|
||||
result[folder] = username;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex) when (ex is System.Text.Json.JsonException or InvalidOperationException or FormatException)
|
||||
{
|
||||
// An unreadable value: as if no new account had been chosen.
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Task SetNewAccountsAsync(ISettingsStore store, long runId, IReadOnlyDictionary<string, string> folders, CancellationToken ct)
|
||||
{
|
||||
if (folders.Count == 0)
|
||||
{
|
||||
return store.SetAsync(NewAccountsKey(runId), null, ct);
|
||||
}
|
||||
|
||||
var json = new JsonObject();
|
||||
foreach (var (folder, username) in folders.OrderBy(f => f.Key, StringComparer.Ordinal))
|
||||
{
|
||||
json[folder] = username;
|
||||
}
|
||||
|
||||
return store.SetAsync(NewAccountsKey(runId), json.ToJsonString(), ct);
|
||||
}
|
||||
|
||||
public static Task ClearAsync(ISettingsStore store, long runId, CancellationToken ct) =>
|
||||
store.SetManyAsync(new Dictionary<string, string?> { [StogramKey(runId)] = null, [NewAccountsKey(runId)] = null }, ct);
|
||||
}
|
||||
Reference in new issue
Block a user