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
@@ -1,6 +1,7 @@
|
||||
using Febe.Core.Domain;
|
||||
using Febe.Core.Storage;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using static Febe.Data.Stores.EntryMapping;
|
||||
|
||||
namespace Febe.Data.Stores;
|
||||
|
||||
@@ -13,6 +14,8 @@ internal sealed class ImportStore(FebeDatabase db) : IImportStore
|
||||
+ "taken_at, date_source, instagram_id, code, probable_account, probable_kind, highlight_title, outcome, matched_media_id, "
|
||||
+ "target_account_id, decision, state, entry_id, destination_path, last_error";
|
||||
|
||||
private const int FileColumnCount = 28;
|
||||
|
||||
private static ImportRun ReadRun(SqliteDataReader r) => new()
|
||||
{
|
||||
Id = r.GetInt64(0),
|
||||
@@ -308,4 +311,186 @@ internal sealed class ImportStore(FebeDatabase db) : IImportStore
|
||||
.With("@a", Sql.Int(accountId)).With("@r", runId).With("@f", folder).ExecuteNonQuery(),
|
||||
ct);
|
||||
}
|
||||
|
||||
public Task<IReadOnlyList<ImportFolderCounts>> ListFolderCountsAsync(long runId, CancellationToken ct = default) =>
|
||||
db.ReadAsync<IReadOnlyList<ImportFolderCounts>>(
|
||||
c =>
|
||||
{
|
||||
var folders = new Dictionary<string, FolderAccumulator>(StringComparer.Ordinal);
|
||||
using (var r = c.Command("""
|
||||
SELECT folder, outcome, COUNT(*), COALESCE(SUM(bytes), 0), MAX(probable_account), MAX(target_account_id),
|
||||
SUM(CASE WHEN instagram_id IS NOT NULL OR code IS NOT NULL THEN 1 ELSE 0 END)
|
||||
FROM import_file WHERE run_id = @r GROUP BY folder, outcome
|
||||
""").With("@r", runId).ExecuteReader())
|
||||
{
|
||||
while (r.Read())
|
||||
{
|
||||
var folder = r.GetString(0);
|
||||
if (!folders.TryGetValue(folder, out var acc))
|
||||
{
|
||||
acc = new FolderAccumulator();
|
||||
folders[folder] = acc;
|
||||
}
|
||||
|
||||
var count = r.GetInt32(2);
|
||||
acc.ByOutcome[r.GetEnum<ImportOutcome>(1)] += count;
|
||||
acc.Files += count;
|
||||
acc.Bytes += r.GetInt64(3);
|
||||
acc.ProbableAccount ??= r.GetTextOrNull(4);
|
||||
acc.TargetAccountId ??= r.GetInt64OrNull(5);
|
||||
acc.Recognized += r.GetInt32(6);
|
||||
}
|
||||
}
|
||||
|
||||
return folders
|
||||
.OrderBy(f => f.Key, StringComparer.Ordinal)
|
||||
.Select(f => new ImportFolderCounts(f.Key, f.Value.Files, f.Value.Bytes, f.Value.ProbableAccount, f.Value.TargetAccountId, f.Value.ByOutcome, f.Value.Recognized))
|
||||
.ToList();
|
||||
},
|
||||
ct);
|
||||
|
||||
public Task<IReadOnlyList<DuplicatePair>> ListPairsAsync(long runId, bool pendingOnly, long afterId = 0, int limit = 500, CancellationToken ct = default) =>
|
||||
db.ReadAsync<IReadOnlyList<DuplicatePair>>(
|
||||
c =>
|
||||
{
|
||||
using var r = c.Command("""
|
||||
SELECT p.id, p.origin, p.reason, p.existing_media_id, p.existing_import_file_id, p.candidate_media_id,
|
||||
p.candidate_import_file_id, p.distance, p.decision, p.created_at, p.decided_at
|
||||
FROM duplicate_pair p JOIN import_file f ON f.id = p.candidate_import_file_id
|
||||
WHERE f.run_id = @r AND p.id > @after AND (@pending = 0 OR p.decision = 'Pending')
|
||||
ORDER BY p.id LIMIT @limit
|
||||
""")
|
||||
.With("@r", runId).With("@after", afterId).With("@pending", Sql.Bool(pendingOnly))
|
||||
.With("@limit", (long)Math.Clamp(limit, 1, 10_000)).ExecuteReader();
|
||||
var list = new List<DuplicatePair>();
|
||||
while (r.Read())
|
||||
{
|
||||
list.Add(new DuplicatePair
|
||||
{
|
||||
Id = r.GetInt64(0),
|
||||
Origin = r.GetEnum<DuplicateOrigin>(1),
|
||||
Reason = r.GetEnum<DuplicateReason>(2),
|
||||
ExistingMediaId = r.GetInt64OrNull(3),
|
||||
ExistingImportFileId = r.GetInt64OrNull(4),
|
||||
CandidateMediaId = r.GetInt64OrNull(5),
|
||||
CandidateImportFileId = r.GetInt64OrNull(6),
|
||||
Distance = r.GetInt32OrNull(7),
|
||||
Decision = r.GetEnum<DuplicateDecision>(8),
|
||||
CreatedAt = r.GetTime(9),
|
||||
DecidedAt = r.GetTimeOrNull(10),
|
||||
});
|
||||
}
|
||||
|
||||
return list;
|
||||
},
|
||||
ct);
|
||||
|
||||
public Task<int> CountPairsAsync(long runId, bool pendingOnly, CancellationToken ct = default) =>
|
||||
db.ReadAsync(
|
||||
c => (int)c.Command("""
|
||||
SELECT COUNT(*) FROM duplicate_pair p JOIN import_file f ON f.id = p.candidate_import_file_id
|
||||
WHERE f.run_id = @r AND (@pending = 0 OR p.decision = 'Pending')
|
||||
""").With("@r", runId).With("@pending", Sql.Bool(pendingOnly)).ScalarInt64(),
|
||||
ct);
|
||||
|
||||
public Task<IReadOnlyList<ImportReportRow>> ListReportRowsAsync(long runId, long afterId = 0, int limit = 1000, CancellationToken ct = default) =>
|
||||
db.ReadAsync<IReadOnlyList<ImportReportRow>>(
|
||||
c =>
|
||||
{
|
||||
var columns = string.Join(", ", FileColumns.Split(", ").Select(column => "f." + column));
|
||||
using var r = c.Command($"""
|
||||
SELECT {columns}, ma.username, mm.relative_path, ta.username
|
||||
FROM import_file f
|
||||
LEFT JOIN media mm ON mm.id = f.matched_media_id
|
||||
LEFT JOIN entry me ON me.id = mm.entry_id
|
||||
LEFT JOIN account ma ON ma.id = me.account_id
|
||||
LEFT JOIN account ta ON ta.id = f.target_account_id
|
||||
WHERE f.run_id = @r AND f.id > @after
|
||||
ORDER BY f.id LIMIT @limit
|
||||
""")
|
||||
.With("@r", runId).With("@after", afterId).With("@limit", (long)Math.Clamp(limit, 1, 10_000)).ExecuteReader();
|
||||
var list = new List<ImportReportRow>();
|
||||
while (r.Read())
|
||||
{
|
||||
list.Add(new ImportReportRow(ReadFile(r), r.GetTextOrNull(FileColumnCount), r.GetTextOrNull(FileColumnCount + 1), r.GetTextOrNull(FileColumnCount + 2)));
|
||||
}
|
||||
|
||||
return list;
|
||||
},
|
||||
ct);
|
||||
|
||||
public Task<int> RemovePendingPairsAsync(long runId, CancellationToken ct = default) =>
|
||||
db.WriteAsync(
|
||||
w => w.Command("""
|
||||
DELETE FROM duplicate_pair WHERE decision = 'Pending'
|
||||
AND candidate_import_file_id IN (SELECT id FROM import_file WHERE run_id = @r)
|
||||
""").With("@r", runId).ExecuteNonQuery(),
|
||||
ct);
|
||||
|
||||
public Task DeleteRunAsync(long runId, CancellationToken ct = default) =>
|
||||
db.WriteAsync(
|
||||
w =>
|
||||
{
|
||||
// Pairs go by cascade with their import files; files on disk are never touched here.
|
||||
w.Command("DELETE FROM import_run WHERE id = @r").With("@r", runId).ExecuteNonQuery();
|
||||
},
|
||||
ct);
|
||||
|
||||
public Task<LibraryIdentifiers> LoadLibraryIdentifiersAsync(CancellationToken ct = default) =>
|
||||
db.ReadAsync(
|
||||
c =>
|
||||
{
|
||||
var entries = new List<EntryIdentity>();
|
||||
using (var r = c.Command("""
|
||||
SELECT id, account_id, kind, state, instagram_id, code FROM entry
|
||||
WHERE instagram_id IS NOT NULL OR code IS NOT NULL
|
||||
""").ExecuteReader())
|
||||
{
|
||||
while (r.Read())
|
||||
{
|
||||
entries.Add(new EntryIdentity(r.GetInt64(0), r.GetInt64(1), r.GetEnum<EntryKind>(2), r.GetEnum<EntryState>(3), r.GetTextOrNull(4), r.GetTextOrNull(5)));
|
||||
}
|
||||
}
|
||||
|
||||
var media = new List<MediaIdentity>();
|
||||
using (var r = c.Command("SELECT id, entry_id, instagram_id FROM media WHERE instagram_id IS NOT NULL").ExecuteReader())
|
||||
{
|
||||
while (r.Read())
|
||||
{
|
||||
media.Add(new MediaIdentity(r.GetInt64(0), r.GetInt64(1), r.GetString(2)));
|
||||
}
|
||||
}
|
||||
|
||||
return new LibraryIdentifiers(entries, media);
|
||||
},
|
||||
ct);
|
||||
|
||||
public Task<Entry?> FindEntryByCodeAsync(long accountId, EntryKind kind, string code, CancellationToken ct = default)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(code);
|
||||
return db.ReadAsync(
|
||||
c =>
|
||||
{
|
||||
using var r = c.Command($"""
|
||||
SELECT {EntryColumns} FROM entry e WHERE e.account_id = @a AND e.kind = @k AND e.code = @c ORDER BY e.id LIMIT 1
|
||||
""").With("@a", accountId).With("@k", Sql.Enum(kind)).With("@c", code.Trim()).ExecuteReader();
|
||||
return r.Read() ? ReadEntry(r) : null;
|
||||
},
|
||||
ct);
|
||||
}
|
||||
|
||||
private sealed class FolderAccumulator
|
||||
{
|
||||
public Dictionary<ImportOutcome, int> ByOutcome { get; } = Enum.GetValues<ImportOutcome>().ToDictionary(o => o, _ => 0);
|
||||
|
||||
public int Files { get; set; }
|
||||
|
||||
public long Bytes { get; set; }
|
||||
|
||||
public string? ProbableAccount { get; set; }
|
||||
|
||||
public long? TargetAccountId { get; set; }
|
||||
|
||||
public int Recognized { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user