fix: bulk-download repeat intents, error mapping, leaner membership map
The bulk-download handler still returned 500 after the membership fix. Three issues: 1. Repeat downloads were broken: the handler passed priorUsages[0].id (the usage id) straight as downloadId, but claimAuthorized looks up download_events.id (the intent id) — so repeat assets never matched and were silently skipped. Now calls createRepeatDownload to mint a proper repeat intent, matching the single-download flow. 2. The catch block returned a generic 500 for every error, hiding the cause (the user only saw "Download fehlgeschlagen"). Added mappedBulkError so UsageAccountingError surfaces as its real code (503 IMMICH_UNAVAILABLE, 404 ASSET_NOT_IN_ALBUM, …) and the server log keeps the full stack. 3. The assetId→album map was built by fetching up to 1000 assets per granted album WITH withExif, then createNewUsage re-fetched per asset — heavy enough to time Immich out under load. The map only needs ids, so it now passes withExif:false (searchPhotos gained a withExif option, default true to preserve the gallery's orientation filter and masonry aspect ratios). Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
ca5f79e193
commit
8eadd1f408
3 files changed
+57
-11
No files matched your search
@@ -1,6 +1,8 @@
|
|||||||
import { NextRequest } from "next/server";
|
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 { usageService } from "@/modules/usages/service";
|
||||||
|
import { createRepeatDownload } from "@/modules/downloads/service";
|
||||||
|
import { UsageAccountingError } from "@/modules/usages/types";
|
||||||
import { immichClient } from "@/modules/immich/client";
|
import { immichClient } from "@/modules/immich/client";
|
||||||
import { downloadDeliveryRepository } from "@/modules/downloads/stream";
|
import { downloadDeliveryRepository } from "@/modules/downloads/stream";
|
||||||
import { albumGrantService } from "@/modules/albums/grants";
|
import { albumGrantService } from "@/modules/albums/grants";
|
||||||
@@ -46,6 +48,7 @@ export async function POST(request: NextRequest) {
|
|||||||
type: "IMAGE",
|
type: "IMAGE",
|
||||||
page: 1,
|
page: 1,
|
||||||
size: 1000,
|
size: 1000,
|
||||||
|
withExif: false,
|
||||||
});
|
});
|
||||||
return { albumId, items: page.items };
|
return { albumId, items: page.items };
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -91,8 +94,15 @@ export async function POST(request: NextRequest) {
|
|||||||
|
|
||||||
let downloadId: string;
|
let downloadId: string;
|
||||||
if (isRepeat) {
|
if (isRepeat) {
|
||||||
// Skip usage creation for repeat downloads
|
// Create a repeat-download intent against the existing usage so the
|
||||||
downloadId = priorUsages[0].id;
|
// 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 {
|
} else {
|
||||||
const idempotencyKey = crypto.randomUUID();
|
const idempotencyKey = crypto.randomUUID();
|
||||||
|
|
||||||
@@ -176,7 +186,42 @@ export async function POST(request: NextRequest) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Bulk download failed", err);
|
// Surface the real cause. Without this, any UsageAccountingError from
|
||||||
return new Response("Internal Server Error", { status: 500 });
|
// 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<UsageAccountingError["code"], number> = {
|
||||||
|
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" } },
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -372,11 +372,12 @@ export function createImmichClient({
|
|||||||
type: "IMAGE",
|
type: "IMAGE",
|
||||||
page: input.page,
|
page: input.page,
|
||||||
size: input.size,
|
size: input.size,
|
||||||
// Required so exifImageWidth/exifImageHeight are populated — the
|
// Default on: exifImageWidth/exifImageHeight populate the
|
||||||
// album page filters by orientation (hoch/quer) and lays out the
|
// orientation filter (hoch/quer) and the masonry aspect ratios.
|
||||||
// masonry grid from each asset's native aspect ratio. Without it
|
// Bulk-download's membership map passes withExif:false — it only
|
||||||
// Immich returns exifInfo as null and both break.
|
// needs asset ids, and the EXIF payload across every granted album
|
||||||
withExif: true,
|
// is large enough to time Immich out under load.
|
||||||
|
withExif: input.withExif ?? true,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
metadataSearchSchema.transform(({ assets }) => ({
|
metadataSearchSchema.transform(({ assets }) => ({
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ export interface ImmichClient {
|
|||||||
getAlbum(albumId: string, signal?: AbortSignal): Promise<ImmichAlbum>;
|
getAlbum(albumId: string, signal?: AbortSignal): Promise<ImmichAlbum>;
|
||||||
getAsset(assetId: string, signal?: AbortSignal): Promise<ImmichAsset>;
|
getAsset(assetId: string, signal?: AbortSignal): Promise<ImmichAsset>;
|
||||||
searchPhotos(
|
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,
|
signal?: AbortSignal,
|
||||||
): Promise<ImmichSearchPage>;
|
): Promise<ImmichSearchPage>;
|
||||||
getThumbnail(assetId: string, signal?: AbortSignal): Promise<ImmichBinary>;
|
getThumbnail(assetId: string, signal?: AbortSignal): Promise<ImmichBinary>;
|
||||||
|
|||||||
Reference in new issue
Block a user