using Febe.Core.Abstractions; using Febe.Core.Domain; using Febe.Core.Engine; using Febe.Core.Library; using Febe.Web.Library; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Routing; namespace Febe.Web.Api; /// /// The review of the probable duplicates found after a download (spec §9.5, §10.3): the same comparison as mockup 09 /// for pairs with origin «download». Nothing is decided in advance; a file leaves the library only on Alberto's choice /// (rule 12). /// public static class DuplicatesApi { public static void Map(IEndpointRouteBuilder endpoints) { endpoints.MapGet("/api/duplicates", async (long? after, FebeLibrary library, IClock clock, CancellationToken ct) => { var review = library.DownloadDuplicates; var cases = await review.ListAsync(after ?? 0, 50, ct).ConfigureAwait(false); var pending = await review.CountPendingAsync(ct).ConfigureAwait(false); return ApiHttp.Json(new DuplicatePageDto([.. cases.Select(c => Case(c, clock))], pending), FebeJsonContext.Default.DuplicatePageDto); }); endpoints.MapPost("/api/duplicates/{pairId:long}", async (long pairId, HttpContext context, FebeLibrary library, LibraryChanges changes, IClock clock, CancellationToken ct) => { var body = await ApiHttp.BodyAsync(context, FebeJsonContext.Default.DecideRequest).ConfigureAwait(false); var decision = ParseDecision(body.Decision); if (decision == DuplicateDecision.Pending) { throw new BadRequestException("Scegli che cosa tenere: il più grande, quello già in libreria oppure entrambi."); } var review = library.DownloadDuplicates; DownloadDecisionResult result; try { result = await review.DecideAsync(pairId, decision, body.ApplyToAll, ct).ConfigureAwait(false); } catch (KeyNotFoundException ex) { throw new NotFoundException(ex.Message, ex); } if (result.FilesRemoved > 0) { changes.Publish(new LibraryChange(clock.Now, null, "deleted")); } var pending = await review.CountPendingAsync(ct).ConfigureAwait(false); var message = result.FilesRemoved switch { 0 => result.Decided == 1 ? "Fatto: restano entrambi i file." : $"Fatto per {ItalianText.Count(result.Decided, "doppione", "doppioni")}.", _ => $"Fatto: {ItalianText.Count(result.FilesRemoved, "file tolto", "file tolti")} dalla libreria (Febe ricorda la scelta e non li riscarica).", }; if (result.Problems.Count > 0) { message += " Alcuni file non si sono potuti cancellare e restano sul disco."; } return ApiHttp.Json(new DecideDto(result.Decided, result.FilesRemoved, result.Problems, message, pending), FebeJsonContext.Default.DecideDto); }); } public static DuplicateCaseDto Case(DownloadDuplicateCase item, IClock clock) { ArgumentNullException.ThrowIfNull(item); ArgumentNullException.ThrowIfNull(clock); DuplicateSideDto Side(DownloadDuplicateSide s) => new( true, s.MediaId, null, s.Username, s.RelativePath, ApiMapping.Media(s.Kind), s.Width, s.Height, s.Bytes, ApiMapping.Local(s.PublishedAt, clock.Now, clock.LocalZone), s.HasThumbnail && s.Sha256 is { } sha ? "/api/thumbnails/" + sha : null); return new DuplicateCaseDto(item.Pair.Id, Reason(item.Pair.Reason), Decision(item.Pair.Decision), Side(item.Existing), Side(item.Candidate), item.CandidateIsLarger, false); } public static string Reason(DuplicateReason reason) => reason == DuplicateReason.SameInstagramId ? "sameInstagramId" : "visuallySimilar"; public static string Decision(DuplicateDecision decision) => decision switch { DuplicateDecision.KeepLarger => "keepLarger", DuplicateDecision.KeepExisting => "keepExisting", DuplicateDecision.KeepBoth => "keepBoth", _ => "pending", }; public static DuplicateDecision ParseDecision(string? text) => text?.ToLowerInvariant() switch { "keeplarger" => DuplicateDecision.KeepLarger, "keepexisting" => DuplicateDecision.KeepExisting, "keepboth" => DuplicateDecision.KeepBoth, "pending" => DuplicateDecision.Pending, _ => throw new BadRequestException("Scelta sconosciuta: tieni il più grande, quello in libreria oppure entrambi."), }; }