M1 Dati e libreria: modello, archivi SQLite con scrittore unico, nomi dei file, scrittura sicura, miniature, impronte, _febe.jsonl, copie, eliminazione e ricostruzione
Modello del dominio e interfacce degli archivi in Febe.Core; implementazione SQLite in Febe.Data (WAL, chiavi esterne, migrazioni numerate in transazione, coda di scrittura su un solo thread); logica della libreria in Febe.Core/Library; 164 prove MSTest, compresa la ricostruzione del database da una libreria di prova e la scala a 200.000 elementi. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
69 files changed
+12472
No files matched your search
@@ -0,0 +1,146 @@
|
||||
using Febe.Core.Abstractions;
|
||||
using Febe.Core.Domain;
|
||||
using Febe.Core.Storage;
|
||||
|
||||
namespace Febe.Core.Library;
|
||||
|
||||
/// <summary>The two choices of the deletion dialog (spec §4.6).</summary>
|
||||
public enum DeletionMode
|
||||
{
|
||||
/// <summary>
|
||||
/// Default, «ricorda la scelta»: the entry stays in the database without files, in the state «eliminato», so it is
|
||||
/// never downloaded again.
|
||||
/// </summary>
|
||||
Remember,
|
||||
|
||||
/// <summary>«Eliminalo e lascia che torni»: the record is removed too; a later check may download it again.</summary>
|
||||
AllowReturn,
|
||||
}
|
||||
|
||||
/// <summary>What a deletion did.</summary>
|
||||
/// <param name="EntryId">The entry.</param>
|
||||
/// <param name="Found">False when the entry did not exist (nothing done).</param>
|
||||
/// <param name="DeletedFiles">Files removed from disk (relative paths).</param>
|
||||
/// <param name="MissingFiles">Files that were already missing.</param>
|
||||
/// <param name="FailedFiles">Files that could not be removed (e.g. open in another program), with the reason.</param>
|
||||
public sealed record DeletionResult(
|
||||
long EntryId,
|
||||
bool Found,
|
||||
IReadOnlyList<string> DeletedFiles,
|
||||
IReadOnlyList<string> MissingFiles,
|
||||
IReadOnlyList<string> FailedFiles)
|
||||
{
|
||||
public bool Complete => Found && FailedFiles.Count == 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// «Elimina dalla libreria» (spec §4.6). The confirmation is the interface's job; this class runs only on an explicit
|
||||
/// choice of Alberto (rule 12: the library never deletes or replaces files on its own).
|
||||
/// <para>
|
||||
/// Order: the deletion line goes to <c>_febe.jsonl</c> first (so a rebuild honours it even if Febe stops halfway),
|
||||
/// then the database, then the files and their thumbnails.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public sealed class LibraryDeletion(
|
||||
IFebeStorage storage,
|
||||
LibraryLayout layout,
|
||||
AccountJournal journal,
|
||||
IClock clock,
|
||||
ThumbnailStore? thumbnails = null,
|
||||
PerceptualHashIndex? hashIndex = null)
|
||||
{
|
||||
public async Task<DeletionResult> DeleteAsync(long entryId, DeletionMode mode = DeletionMode.Remember, CancellationToken ct = default)
|
||||
{
|
||||
var stored = await storage.Entries.GetWithMediaAsync(entryId, ct).ConfigureAwait(false);
|
||||
if (stored is null)
|
||||
{
|
||||
return new DeletionResult(entryId, false, [], [], []);
|
||||
}
|
||||
|
||||
var account = await storage.Accounts.GetAsync(stored.Entry.AccountId, ct).ConfigureAwait(false)
|
||||
?? throw new InvalidOperationException($"L'account dell'elemento {entryId} non esiste.");
|
||||
var folder = layout.AccountFolder(account);
|
||||
var present = stored.Media.Where(m => m.RelativePath is not null && EntryStateRules.IsPresent(m.State)).ToList();
|
||||
|
||||
ct.ThrowIfCancellationRequested();
|
||||
|
||||
// From here the deletion goes to the end: a half-done deletion is worse than a late one.
|
||||
var none = CancellationToken.None;
|
||||
await journal.AppendDeletedAsync(folder, account, stored.Entry, present.Select(m => m.RelativePath!).ToList(), mode == DeletionMode.Remember, none)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (mode == DeletionMode.Remember)
|
||||
{
|
||||
await storage.Entries.MarkDeletedAsync(entryId, clock.Now, none).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
await storage.Entries.RemoveAsync(entryId, none).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
foreach (var media in stored.Media)
|
||||
{
|
||||
hashIndex?.Remove(media.Id);
|
||||
}
|
||||
|
||||
var deleted = new List<string>();
|
||||
var missing = new List<string>();
|
||||
var failed = new List<string>();
|
||||
foreach (var media in present)
|
||||
{
|
||||
var relative = media.RelativePath!;
|
||||
try
|
||||
{
|
||||
var path = LibraryLayout.FullPath(folder, relative);
|
||||
if (File.Exists(path))
|
||||
{
|
||||
File.Delete(path);
|
||||
deleted.Add(relative);
|
||||
}
|
||||
else
|
||||
{
|
||||
missing.Add(relative);
|
||||
}
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or ArgumentException)
|
||||
{
|
||||
failed.Add($"{relative}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
if (thumbnails is not null)
|
||||
{
|
||||
foreach (var sha in stored.Media.Select(m => m.Sha256).OfType<string>().Distinct(StringComparer.Ordinal))
|
||||
{
|
||||
var users = await storage.Media.FindBySha256Async(sha, none).ConfigureAwait(false);
|
||||
if (!users.Any(u => EntryStateRules.IsPresent(u.Media.State)))
|
||||
{
|
||||
try
|
||||
{
|
||||
thumbnails.Delete(sha);
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
// A thumbnail left behind costs a few kilobytes; it is regenerated or ignored.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new DeletionResult(entryId, true, deleted, missing, failed);
|
||||
}
|
||||
|
||||
/// <summary>Multiple selection: deletes each entry in turn.</summary>
|
||||
public async Task<IReadOnlyList<DeletionResult>> DeleteManyAsync(IEnumerable<long> entryIds, DeletionMode mode = DeletionMode.Remember, CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(entryIds);
|
||||
var results = new List<DeletionResult>();
|
||||
foreach (var id in entryIds.Distinct())
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
results.Add(await DeleteAsync(id, mode, ct).ConfigureAwait(false));
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user