diff --git a/src/app/api/lightbox/bulk-download/route.ts b/src/app/api/lightbox/bulk-download/route.ts index 8e2cfb3..40f48ee 100644 --- a/src/app/api/lightbox/bulk-download/route.ts +++ b/src/app/api/lightbox/bulk-download/route.ts @@ -1,6 +1,8 @@ import { NextRequest } from "next/server"; -import { requireActiveEditorial } from "@/modules/auth/principal"; +import { requireActiveEditorial, PrincipalAuthorizationError } from "@/modules/auth/principal"; import { usageService } from "@/modules/usages/service"; +import { createRepeatDownload } from "@/modules/downloads/service"; +import { UsageAccountingError } from "@/modules/usages/types"; import { immichClient } from "@/modules/immich/client"; import { downloadDeliveryRepository } from "@/modules/downloads/stream"; import { albumGrantService } from "@/modules/albums/grants"; @@ -46,6 +48,7 @@ export async function POST(request: NextRequest) { type: "IMAGE", page: 1, size: 1000, + withExif: false, }); return { albumId, items: page.items }; } catch (err) { @@ -88,14 +91,21 @@ export async function POST(request: NextRequest) { // Check for prior usages to avoid double charging const priorUsages = await usageService.listPriorUsages(principal.editorialId, assetId); const isRepeat = priorUsages.length > 0; - + let downloadId: string; if (isRepeat) { - // Skip usage creation for repeat downloads - downloadId = priorUsages[0].id; + // Create a repeat-download intent against the existing usage so the + // delivery claim (claimAuthorized, which looks up download_events.id) + // finds a valid intent. Reusing the usage id directly never matches. + const repeatIntent = await createRepeatDownload({ + editorialId: principal.editorialId, + usageId: priorUsages[0].id, + idempotencyKey: crypto.randomUUID(), + }); + downloadId = repeatIntent.id; } else { const idempotencyKey = crypto.randomUUID(); - + const newUsage = await usageService.createNewUsage({ editorialId: principal.editorialId, assetId, @@ -176,7 +186,42 @@ export async function POST(request: NextRequest) { }); } catch (err) { - console.error("Bulk download failed", err); - return new Response("Internal Server Error", { status: 500 }); + // Surface the real cause. Without this, any UsageAccountingError from + // createNewUsage/createRepeatDownload (e.g. IMMICH_UNAVAILABLE under load) + // became an opaque 500 and the user only saw "Download fehlgeschlagen". + console.error("[bulk-download] failed:", err); + return mappedBulkError(err); } } + +function mappedBulkError(error: unknown): Response { + if (error instanceof PrincipalAuthorizationError) { + return Response.json( + { code: "FORBIDDEN" }, + { status: error.code === "AUTHENTICATION_REQUIRED" ? 401 : 403, headers: { "cache-control": "private, no-store" } }, + ); + } + if (error instanceof UsageAccountingError) { + const statusByCode: Record = { + INVALID_INPUT: 400, + IDEMPOTENCY_CONFLICT: 409, + ALBUM_NOT_GRANTED: 404, + ASSET_NOT_FOUND: 404, + ASSET_NOT_IN_ALBUM: 404, + ASSET_NOT_IMAGE: 422, + USAGE_NOT_FOUND: 404, + IMMICH_UNAVAILABLE: 503, + EDITORIAL_NOT_ACTIVE: 403, + INVALID_PRICE: 409, + }; + const status = statusByCode[error.code] ?? 500; + return Response.json( + { code: error.code }, + { status, headers: { "cache-control": "private, no-store" } }, + ); + } + return Response.json( + { code: "REQUEST_FAILED" }, + { status: 500, headers: { "cache-control": "private, no-store" } }, + ); +} diff --git a/src/modules/immich/client.ts b/src/modules/immich/client.ts index 7e9c919..96fe822 100644 --- a/src/modules/immich/client.ts +++ b/src/modules/immich/client.ts @@ -372,11 +372,12 @@ export function createImmichClient({ type: "IMAGE", page: input.page, size: input.size, - // Required so exifImageWidth/exifImageHeight are populated — the - // album page filters by orientation (hoch/quer) and lays out the - // masonry grid from each asset's native aspect ratio. Without it - // Immich returns exifInfo as null and both break. - withExif: true, + // Default on: exifImageWidth/exifImageHeight populate the + // orientation filter (hoch/quer) and the masonry aspect ratios. + // Bulk-download's membership map passes withExif:false — it only + // needs asset ids, and the EXIF payload across every granted album + // is large enough to time Immich out under load. + withExif: input.withExif ?? true, }), }, metadataSearchSchema.transform(({ assets }) => ({ diff --git a/src/modules/immich/types.ts b/src/modules/immich/types.ts index 471ddac..4c6e010 100644 --- a/src/modules/immich/types.ts +++ b/src/modules/immich/types.ts @@ -46,7 +46,7 @@ export interface ImmichClient { getAlbum(albumId: string, signal?: AbortSignal): Promise; getAsset(assetId: string, signal?: AbortSignal): Promise; searchPhotos( - input: { albumIds: string[]; id?: string; description?: string; page: number; size: number; type?: "IMAGE" }, + input: { albumIds: string[]; id?: string; description?: string; page: number; size: number; type?: "IMAGE"; withExif?: boolean }, signal?: AbortSignal, ): Promise; getThumbnail(assetId: string, signal?: AbortSignal): Promise;