fix: download 404 — Immich getAlbum no longer returns assets

The previous getAlbum fix (removing ?withoutAssets=true) was insufficient:
current Immich's GET /albums/:id NEVER returns assets — AlbumService.get
calls findOrFail with { withAssets: false } and the withoutAssets param is
gone. So album.assets was always empty and every download still failed with
ASSET_NOT_IN_ALBUM → 404. The AI_MEMORY "getAlbum + iterate album.assets"
guidance was stale (written for an older Immich API).

Membership is now verified via /search/metadata with an id + albumIds filter,
which returns the asset only if it belongs to the album. This is the
authoritative test (asset.albums is unreliable for older assets too).

- usages/service.ts: loadRequestedAlbum checks isAssetInAlbum via searchPhotos
  before fetching the album name; getAlbum now only provides albumName.
- bulk-download/route.ts: build the assetId→album map via searchPhotos per
  granted album instead of the empty getAlbum().assets iteration.
- immich client/types: add optional `id` to the searchPhotos input.
- tests: usages mocks now stub searchPhotos; the 13 pre-existing service
  test failures (mock returned getAlbum without assets → TypeError) are
  resolved — 20/20 pass.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Leon TonhäuserandClaude committed 2026-08-11 14:25:55 +02:00
1 parent 962e2c7dc2
commit 6a31e5f411
7 files changed
+83 -39

No files matched your search

+1
View File
@@ -367,6 +367,7 @@ export function createImmichClient({
headers: { "content-type": "application/json" },
body: JSON.stringify({
albumIds: input.albumIds,
...(input.id === undefined ? {} : { id: input.id }),
...(input.description === undefined ? {} : { description: input.description }),
type: "IMAGE",
page: input.page,
+1 -1
View File
@@ -46,7 +46,7 @@ export interface ImmichClient {
getAlbum(albumId: string, signal?: AbortSignal): Promise<ImmichAlbum>;
getAsset(assetId: string, signal?: AbortSignal): Promise<ImmichAsset>;
searchPhotos(
input: { albumIds: string[]; description?: string; page: number; size: number; type?: "IMAGE" },
input: { albumIds: string[]; id?: string; description?: string; page: number; size: number; type?: "IMAGE" },
signal?: AbortSignal,
): Promise<ImmichSearchPage>;
getThumbnail(assetId: string, signal?: AbortSignal): Promise<ImmichBinary>;
+22 -18
View File
@@ -12,7 +12,6 @@ const editorialId = "1111111a-1111-4111-8111-11111111111a";
const idempotencyKey = "2222222b-2222-4222-8222-22222222222b";
const assetId = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa";
const albumId = "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb";
const otherAlbumId = "cccccccc-cccc-4ccc-8ccc-cccccccccccc";
const newUsageInput = {
editorialId,
@@ -103,6 +102,12 @@ function createService() {
originalMimeType: "image/jpeg",
albums: [{ id: albumId }],
}),
searchPhotos: vi.fn().mockResolvedValue({
items: [{ id: assetId }],
total: 1,
nextPage: null,
previousPage: null,
}),
};
return { ...state, immich, service: createUsageService({ repository: state.repository, immich }) };
}
@@ -231,12 +236,11 @@ test.each([
test("rejects an asset outside the exact requested album", async () => {
const { service, immich, usages } = createService();
immich.getAsset.mockResolvedValue({
id: assetId,
type: "IMAGE" as const,
originalFileName: "jubel.jpg",
originalMimeType: "image/jpeg",
albums: [{ id: otherAlbumId }],
immich.searchPhotos.mockResolvedValue({
items: [],
total: 0,
nextPage: null,
previousPage: null,
});
await expect(service.createNewUsage(newUsageInput)).rejects.toMatchObject({
@@ -248,20 +252,20 @@ test("rejects an asset outside the exact requested album", async () => {
test("rejects removal from the requested album between preflight and locked refresh", async () => {
const { service, immich, usages } = createService();
immich.getAsset
// Preflight membership passes, then the locked refresh sees the asset removed
// (search returns empty), so the refresh throws ASSET_NOT_IN_ALBUM.
immich.searchPhotos
.mockResolvedValueOnce({
id: assetId,
type: "IMAGE" as const,
originalFileName: "jubel.jpg",
originalMimeType: "image/jpeg",
albums: [{ id: albumId }],
items: [{ id: assetId }],
total: 1,
nextPage: null,
previousPage: null,
})
.mockResolvedValueOnce({
id: assetId,
type: "IMAGE" as const,
originalFileName: "jubel.jpg",
originalMimeType: "image/jpeg",
albums: [],
items: [],
total: 0,
nextPage: null,
previousPage: null,
});
await expect(service.createNewUsage(newUsageInput)).rejects.toMatchObject({
+32 -8
View File
@@ -57,7 +57,7 @@ export type UsageRepository = {
};
type UsageImmich = {
getAlbum(albumId: string): Promise<{ id: string; albumName: string; assets: Array<{ id: string }> }>;
getAlbum(albumId: string): Promise<{ id: string; albumName: string }>;
getAsset(assetId: string): Promise<{
id: string;
originalFileName: string;
@@ -65,6 +65,9 @@ type UsageImmich = {
type: string;
albums: Array<{ id: string }>;
}>;
searchPhotos(
input: { albumIds: string[]; id?: string; page: number; size: number; type?: "IMAGE" },
): Promise<{ items: Array<{ id: string }>; total: number; nextPage: string | number | null; previousPage: string | number | null }>;
};
export function createUsageService({
@@ -333,18 +336,39 @@ async function loadRequestedAlbum(
assetId: string,
albumId: string,
): Promise<{ id: string; albumName: string }> {
let album;
// Immich's GET /albums/:id no longer returns assets (metadata only), and
// `asset.albums` is unreliable (empty for older assets), so neither can
// authorize membership. Verify via /search/metadata with an id + albumIds
// filter — returns the asset only if it belongs to the album. Check before
// fetching the album name so a rejected asset doesn't waste that call.
if (!(await isAssetInAlbum(immich, assetId, albumId))) {
throw new UsageAccountingError("ASSET_NOT_IN_ALBUM");
}
try {
album = await immich.getAlbum(albumId);
return await immich.getAlbum(albumId);
} catch {
throw new UsageAccountingError("IMMICH_UNAVAILABLE");
}
if (!album.assets.some((a) => a.id === assetId)) {
throw new UsageAccountingError("ASSET_NOT_IN_ALBUM");
}
async function isAssetInAlbum(
immich: UsageImmich,
assetId: string,
albumId: string,
): Promise<boolean> {
try {
const result = await immich.searchPhotos({
albumIds: [albumId],
id: assetId,
type: "IMAGE",
page: 1,
size: 1,
});
return result.items.some((asset) => asset.id === assetId);
} catch {
throw new UsageAccountingError("IMMICH_UNAVAILABLE");
}
return album;
}
async function getValidatedAsset(immich: UsageImmich, assetId: string) {