using Febe.Core.Abstractions; using Febe.Core.Domain; using Febe.Core.Import; using Febe.Core.Library; using Febe.Core.Storage; using Febe.Web.Import; using Febe.Web.Native; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Routing; namespace Febe.Web.Api; /// /// Importa (spec §9, mockup 09) over : the four steps Sorgente, Analisi, Revisione, Copia. /// The analysis and the copy run in the background; the page follows them with /progress. No file of the source /// is touched before the copy, and «Sposta» deletes an original only after its copy was verified (spec §9.4). /// public static class ImportApi { public static void Map(IEndpointRouteBuilder endpoints) { endpoints.MapGet("/api/import", async (HttpContext context, ImportHub hub, IClock clock, IServiceProvider services, CancellationToken ct) => { var service = await hub.GetAsync(ct).ConfigureAwait(false); var runs = await service.ListRunsAsync(ct).ConfigureAwait(false); var d = SettingsApi.Deps.From(services); var caps = SystemApi.Capabilities(context, d.Options, d.Shell, d.Browser, clock); var active = service.ActiveRunId; return ApiHttp.Json( new ImportOverviewDto([.. runs.Take(20).Select(r => Run(service, r, clock, active))], active, service.Sources.Root, hub.SourceRoot is null && caps.PickFolder), FebeJsonContext.Default.ImportOverviewDto); }); endpoints.MapGet("/api/import/sources", async (string? path, ImportHub hub, CancellationToken ct) => { var service = await hub.GetAsync(ct).ConfigureAwait(false); var root = service.Sources.Root ?? throw new BadRequestException("Qui la cartella si sceglie con la finestra del sistema."); var folders = Guard(() => service.BrowseSources(path)); var relative = string.IsNullOrWhiteSpace(path) ? null : path.Trim('/', '\\'); var parent = relative is null ? null : relative.Contains('/', StringComparison.Ordinal) ? relative[..relative.LastIndexOf('/')] : string.Empty; return ApiHttp.Json( new SourceListDto(relative, parent, relative is null ? root : LibraryLayout.ToStored(Path.Combine(root, relative)), [.. folders.Select(f => new SourceFolderDto(f.Name, f.Path, f.HasSubfolders))]), FebeJsonContext.Default.SourceListDto); }); endpoints.MapPost("/api/import/pick-folder", async (ImportHub hub, INativeShell shell, CancellationToken ct) => { if (hub.SourceRoot is not null || !shell.CanPickFolder) { throw new BadRequestException("Qui la cartella da importare si sceglie dall'elenco delle cartelle di /importa."); } var picked = await shell.PickFolderAsync("Cartella da importare", null, ct).ConfigureAwait(false); return ApiHttp.Json(new PathDto(picked), FebeJsonContext.Default.PathDto); }); endpoints.MapPost("/api/import/inspect", async (HttpContext context, ImportHub hub, CancellationToken ct) => { var body = await ApiHttp.BodyAsync(context, FebeJsonContext.Default.PathRequest).ConfigureAwait(false); var service = await hub.GetAsync(ct).ConfigureAwait(false); var info = Guard(() => service.InspectSource(body.Path)); return ApiHttp.Json( new SourceInfoDto(body.Path, info.DisplayPath, info.Name, info.ReadOnly, info.CanMove, info.LooksLikeFebeLibrary, info.LooksLikeStogram, info.Folders), FebeJsonContext.Default.SourceInfoDto); }); endpoints.MapPost("/api/import/start", async (HttpContext context, ImportHub hub, IClock clock, CancellationToken ct) => { var body = await ApiHttp.BodyAsync(context, FebeJsonContext.Default.ImportStartRequest).ConfigureAwait(false); var service = await hub.GetAsync(ct).ConfigureAwait(false); var run = await GuardAsync(() => service.StartAsync(body.Path, new ImportAnalysisOptions { StogramHypothesis = body.Stogram }, ct)).ConfigureAwait(false); return ApiHttp.Json(Run(service, run, clock, service.ActiveRunId), FebeJsonContext.Default.ImportRunDto); }); endpoints.MapGet("/api/import/{id:long}/progress", async (long id, ImportHub hub, CancellationToken ct) => { var service = await hub.GetAsync(ct).ConfigureAwait(false); var run = await GetRunAsync(service, id, ct).ConfigureAwait(false); var progress = await GuardAsync(() => service.GetProgressAsync(id)).ConfigureAwait(false); return ApiHttp.Json(Progress(progress, run, service.ActiveRunId == id), FebeJsonContext.Default.ImportProgressDto); }); endpoints.MapPost("/api/import/{id:long}/interrupt", async (long id, ImportHub hub, CancellationToken ct) => { var service = await hub.GetAsync(ct).ConfigureAwait(false); await GetRunAsync(service, id, ct).ConfigureAwait(false); await service.InterruptAsync(id).ConfigureAwait(false); return ApiHttp.Message("Interrotta: quello che è stato fatto resta, e riprendi da qui quando vuoi."); }); endpoints.MapPost("/api/import/{id:long}/resume", async (long id, ImportHub hub, IClock clock, CancellationToken ct) => { var service = await hub.GetAsync(ct).ConfigureAwait(false); var run = await GuardAsync(() => service.ResumeAsync(id, ct)).ConfigureAwait(false); return ApiHttp.Json(Run(service, run, clock, service.ActiveRunId), FebeJsonContext.Default.ImportRunDto); }); endpoints.MapGet("/api/import/{id:long}/review", async (long id, ImportHub hub, IFebeStorage storage, IClock clock, CancellationToken ct) => { var service = await hub.GetAsync(ct).ConfigureAwait(false); return ApiHttp.Json(await ReviewAsync(service, storage, id, clock, ct).ConfigureAwait(false), FebeJsonContext.Default.ImportReviewDto); }); endpoints.MapPut("/api/import/{id:long}/folders", async (long id, HttpContext context, ImportHub hub, IFebeStorage storage, IClock clock, CancellationToken ct) => { var body = await ApiHttp.BodyAsync(context, FebeJsonContext.Default.FolderTargetRequest).ConfigureAwait(false); var service = await hub.GetAsync(ct).ConfigureAwait(false); var target = body.Kind?.ToLowerInvariant() switch { "none" => FolderTarget.None, "existing" => FolderTarget.Existing(body.AccountId ?? throw new BadRequestException("Scegli l'account.")), "newarchiveaccount" => FolderTarget.NewArchiveAccount(body.Username ?? throw new BadRequestException("Scrivi il nome del nuovo account.")), _ => throw new BadRequestException("Destinazione sconosciuta."), }; await GuardAsync(async () => { await service.SetFolderTargetAsync(id, body.Folder ?? string.Empty, target, ct).ConfigureAwait(false); return true; }).ConfigureAwait(false); return ApiHttp.Json(await ReviewAsync(service, storage, id, clock, ct).ConfigureAwait(false), FebeJsonContext.Default.ImportReviewDto); }); endpoints.MapPut("/api/import/{id:long}/mode", async (long id, HttpContext context, ImportHub hub, IFebeStorage storage, IClock clock, CancellationToken ct) => { var body = await ApiHttp.BodyAsync(context, FebeJsonContext.Default.ImportModeRequest).ConfigureAwait(false); var service = await hub.GetAsync(ct).ConfigureAwait(false); await GuardAsync(() => service.SetModeAsync(id, ParseMode(body.Mode), ct)).ConfigureAwait(false); return ApiHttp.Json(Plan(await GuardAsync(() => service.GetPlanAsync(id, ct)).ConfigureAwait(false)), FebeJsonContext.Default.ImportPlanDto); }); endpoints.MapGet("/api/import/{id:long}/duplicates", async (long id, long? after, bool? all, ImportHub hub, IClock clock, CancellationToken ct) => { var service = await hub.GetAsync(ct).ConfigureAwait(false); await GetRunAsync(service, id, ct).ConfigureAwait(false); var cases = await GuardAsync(() => service.ListDuplicatesAsync(id, pendingOnly: all != true, after ?? 0, 50, ct)).ConfigureAwait(false); var review = await GuardAsync(() => service.GetReviewAsync(id, ct)).ConfigureAwait(false); return ApiHttp.Json(new DuplicatePageDto([.. cases.Select(c => Case(c, clock))], review.PendingDuplicates), FebeJsonContext.Default.DuplicatePageDto); }); endpoints.MapPost("/api/import/{id:long}/duplicates/{pairId:long}", async (long id, long pairId, HttpContext context, ImportHub hub, CancellationToken ct) => { var body = await ApiHttp.BodyAsync(context, FebeJsonContext.Default.DecideRequest).ConfigureAwait(false); var service = await hub.GetAsync(ct).ConfigureAwait(false); var decision = DuplicatesApi.ParseDecision(body.Decision); int decided; if (decision == DuplicateDecision.Pending) { await GuardAsync(async () => { await service.Review.UndecideAsync(id, pairId, ct).ConfigureAwait(false); return true; }).ConfigureAwait(false); decided = 1; } else { decided = await GuardAsync(() => service.DecideAsync(id, pairId, decision, body.ApplyToAll ? DecisionScope.AllPending : DecisionScope.ThisPair, ct)).ConfigureAwait(false); } var review = await GuardAsync(() => service.GetReviewAsync(id, ct)).ConfigureAwait(false); var message = decided == 1 ? "Scelta registrata: vale quando importi." : $"Scelta registrata per {ItalianTextCount(decided)}: vale quando importi."; return ApiHttp.Json(new DecideDto(decided, 0, [], message, review.PendingDuplicates), FebeJsonContext.Default.DecideDto); }); endpoints.MapGet("/api/import/files/{fileId:long}/preview", async (long fileId, HttpContext context, ImportHub hub, CancellationToken ct) => { var service = await hub.GetAsync(ct).ConfigureAwait(false); var jpeg = await GuardAsync(() => service.Review.RenderPreviewAsync(fileId, 640, ct)).ConfigureAwait(false) ?? throw new NotFoundException("Anteprima non disponibile per questo file."); context.Response.Headers.CacheControl = "private, max-age=600"; return Results.Bytes(jpeg, "image/jpeg"); }); endpoints.MapPost("/api/import/{id:long}/copy", async (long id, HttpContext context, ImportHub hub, IClock clock, CancellationToken ct) => { var body = context.Request.ContentLength is null or > 0 ? await ApiHttp.BodyAsync(context, FebeJsonContext.Default.ImportModeRequest).ConfigureAwait(false) : null; var service = await hub.GetAsync(ct).ConfigureAwait(false); ImportMode? mode = body?.Mode is { Length: > 0 } m ? ParseMode(m) : null; var run = await GuardAsync(() => service.StartCopyAsync(id, mode, ct)).ConfigureAwait(false); return ApiHttp.Json(Run(service, run, clock, service.ActiveRunId), FebeJsonContext.Default.ImportRunDto); }); endpoints.MapGet("/api/import/{id:long}/report", async (long id, ImportHub hub, IClock clock, CancellationToken ct) => { var service = await hub.GetAsync(ct).ConfigureAwait(false); var report = await GuardAsync(() => service.GetReportAsync(id, ct)).ConfigureAwait(false); return ApiHttp.Json(Report(service, report, clock), FebeJsonContext.Default.ImportReportDto); }); endpoints.MapGet("/api/import/{id:long}/report.csv", async (long id, HttpContext context, ImportHub hub, CancellationToken ct) => { var service = await hub.GetAsync(ct).ConfigureAwait(false); var run = await GetRunAsync(service, id, ct).ConfigureAwait(false); var name = service.Reports.CsvFileName(run); context.Response.ContentType = "text/csv; charset=utf-8"; context.Response.Headers.ContentDisposition = $"attachment; filename=\"{name}\""; context.Response.Headers.CacheControl = "no-store"; await service.WriteCsvAsync(id, context.Response.Body, ct).ConfigureAwait(false); }); endpoints.MapPost("/api/import/{id:long}/discard", async (long id, ImportHub hub, CancellationToken ct) => { var service = await hub.GetAsync(ct).ConfigureAwait(false); await GetRunAsync(service, id, ct).ConfigureAwait(false); await service.DiscardAsync(id, ct).ConfigureAwait(false); return ApiHttp.Message("Importazione annullata: nessun file della sorgente è stato toccato."); }); } // ---------------------------------------------------------------------------------------------- mapping public static ImportRunDto Run(ImportService service, ImportRun run, IClock clock, long? active) { ArgumentNullException.ThrowIfNull(service); ArgumentNullException.ThrowIfNull(run); ArgumentNullException.ThrowIfNull(clock); return new ImportRunDto( run.Id, run.SourcePath, service.Sources.DisplayPath(run.SourcePath), State(run.State), run.Mode == ImportMode.Move ? "move" : "copy", run.SourceReadOnly, ApiMapping.Local(run.StartedAt, clock.Now, clock.LocalZone), run.CompletedAt is { } done ? ApiMapping.Local(done, clock.Now, clock.LocalZone) : null, run.LastError, active == run.Id); } public static string State(ImportRunState state) => state switch { ImportRunState.Analyzing => "analyzing", ImportRunState.AnalysisInterrupted => "analysisInterrupted", ImportRunState.Reviewing => "reviewing", ImportRunState.Copying => "copying", ImportRunState.CopyInterrupted => "copyInterrupted", ImportRunState.Completed => "completed", _ => "cancelled", }; public static OutcomeCountsDto Outcomes(IReadOnlyDictionary counts) { ArgumentNullException.ThrowIfNull(counts); int Of(ImportOutcome o) => counts.TryGetValue(o, out var n) ? n : 0; return new OutcomeCountsDto(Of(ImportOutcome.New), Of(ImportOutcome.AlreadyInLibrary), Of(ImportOutcome.ProbableDuplicate), Of(ImportOutcome.Unrecognized), Of(ImportOutcome.IdenticalInSource)); } public static ImportProgressDto Progress(ImportProgress progress, ImportRun run, bool running) { ArgumentNullException.ThrowIfNull(progress); ArgumentNullException.ThrowIfNull(run); var phase = progress.Phase switch { ImportPhase.Listing => "listing", ImportPhase.Analyzing => "analyzing", ImportPhase.Copying => "copying", _ => "finished", }; return new ImportProgressDto( progress.RunId, phase, State(run.State), running, progress.FilesDone, progress.FilesTotal, progress.BytesDone, progress.BytesTotal, progress.Fraction, progress.CurrentFile, Outcomes(progress.Outcomes), progress.Copied, run.LastError); } public static ImportPlanDto Plan(ImportPlan plan) { ArgumentNullException.ThrowIfNull(plan); return new ImportPlanDto( plan.ToImport, plan.ToReplace, plan.Skipped, plan.LeftOutNoAccount, plan.FoldersWithoutAccount, plan.LeftOutUndecided, plan.AlreadyDone, plan.Bytes, plan.Mode == ImportMode.Move ? "move" : "copy", plan.ButtonLabel, plan.LeftOutLabel); } public static FolderTargetDto Target(FolderTarget target) { ArgumentNullException.ThrowIfNull(target); return new FolderTargetDto( target.Kind switch { FolderTargetKind.Existing => "existing", FolderTargetKind.NewArchiveAccount => "newArchiveAccount", _ => "none" }, target.AccountId, target.Username); } public static async Task ReviewAsync(ImportService service, IFebeStorage storage, long runId, IClock clock, CancellationToken ct) { ArgumentNullException.ThrowIfNull(service); ArgumentNullException.ThrowIfNull(storage); var review = await GuardAsync(() => service.GetReviewAsync(runId, ct)).ConfigureAwait(false); var plan = await GuardAsync(() => service.GetPlanAsync(runId, ct)).ConfigureAwait(false); var accounts = await storage.Accounts.ListAsync(ct).ConfigureAwait(false); return new ImportReviewDto( Run(service, review.Run, clock, service.ActiveRunId), review.DisplayPath, review.Headline, review.Files, review.Bytes, Outcomes(review.ByOutcome), review.Recognized, review.PendingDuplicates, [.. review.Folders.Select(f => new ImportFolderDto(f.Folder, f.DisplayName, f.ProbableAccount, f.Files, f.Bytes, f.New, f.AlreadyInLibrary, f.ProbableDuplicates, f.Unrecognized, f.Recognized, Target(f.Target), f.IsBlocked))], Plan(plan), review.CanMove, review.StogramHypothesis, review.Copied, [.. accounts.OrderBy(a => a.Username, StringComparer.Ordinal).Select(a => new AccountChoiceDto(a.Id, a.Username, ApiMapping.State(a.State)))]); } public static DuplicateCaseDto Case(DuplicateCase item, IClock clock) { ArgumentNullException.ThrowIfNull(item); ArgumentNullException.ThrowIfNull(clock); DuplicateSideDto Side(DuplicateSide s) => new( s.InLibrary, s.MediaId, s.ImportFileId, s.Username, s.Path, s.Kind is { } k ? ApiMapping.Media(k) : null, s.Width, s.Height, s.Bytes, s.Date is { } date ? ApiMapping.Local(date, clock.Now, clock.LocalZone) : null, s.InLibrary ? (s.HasThumbnail && s.Sha256 is { } sha ? "/api/thumbnails/" + sha : null) : s.ImportFileId is { } f && s.Kind == MediaKind.Photo ? "/api/import/files/" + f.ToString(System.Globalization.CultureInfo.InvariantCulture) + "/preview" : null); return new DuplicateCaseDto( item.Pair.Id, DuplicatesApi.Reason(item.Pair.Reason), DuplicatesApi.Decision(item.Pair.Decision), Side(item.Existing), Side(item.Candidate), item.CandidateIsLarger, item.InsideSource); } public static ImportReportDto Report(ImportService service, ImportReport report, IClock clock) { ArgumentNullException.ThrowIfNull(service); ArgumentNullException.ThrowIfNull(report); return new ImportReportDto( Run(service, report.Run, clock, service.ActiveRunId), report.DisplayPath, report.Files, report.Bytes, Outcomes(report.ByOutcome), report.Copied, report.Moved, report.Replaced, report.Skipped, report.LeftOut, report.Failed, report.BytesCopied, [.. report.ImportedByAccount.Select(a => new ImportCountDto(a.Username, a.Files))], [.. report.AlreadyInLibraryByAccount.Select(a => new ImportCountDto(a.Username, a.Files))], report.Unrecognized, [.. report.Problems.Select(p => new ImportNoteDto(p.Path, p.Note))]); } private static ImportMode ParseMode(string? text) => text?.ToLowerInvariant() switch { "copy" => ImportMode.Copy, "move" => ImportMode.Move, _ => throw new BadRequestException("Modo sconosciuto: copia o sposta."), }; private static string ItalianTextCount(int n) => Core.Engine.ItalianText.Count(n, "doppione", "doppioni"); private static async Task GetRunAsync(ImportService service, long id, CancellationToken ct) => await service.GetRunAsync(id, ct).ConfigureAwait(false) ?? throw new NotFoundException("Questa importazione non esiste più."); // ---------------------------------------------------------------------------------------------- errors /// The import's own exceptions become Italian answers: 400 for a refused request, 404 for a missing import. internal static T Guard(Func action) { try { return action(); } catch (Exception ex) when (Translate(ex) is { } translated) { throw translated; } } internal static async Task GuardAsync(Func> action) { try { return await action().ConfigureAwait(false); } catch (Exception ex) when (Translate(ex) is { } translated) { throw translated; } } private static Exception? Translate(Exception ex) => ex switch { ImportSourceException => new BadRequestException(ex.Message, ex), KeyNotFoundException => new NotFoundException(ex.Message, ex), ArgumentException a => new BadRequestException(ApiErrorMiddleware.CleanMessage(a), ex), InvalidOperationException => new BadRequestException(ex.Message, ex), _ => null, }; }