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:
1 parent
962e2c7dc2
commit
6a31e5f411
7 files changed
+84
-40
No files matched your search
@@ -32,24 +32,32 @@ export async function POST(request: NextRequest) {
|
||||
|
||||
const grantedAlbumIds = await albumGrantService.getGrantedAlbumIds(principal.editorialId);
|
||||
|
||||
// Fetch all granted albums to build a map of assetId -> validAlbumId
|
||||
// Build a map of assetId -> a granted album that contains it. Immich's
|
||||
// GET /albums/:id no longer returns assets (metadata only), so fetch each
|
||||
// granted album's assets via /search/metadata instead.
|
||||
// ponytail: ceiling of 1000 assets per album; tail assets beyond that
|
||||
// won't map and are skipped from bulk download. Raise if needed.
|
||||
const assetToGrantedAlbum = new Map<string, string>();
|
||||
const grantedAlbums = await Promise.all(
|
||||
const albumAssets = await Promise.all(
|
||||
grantedAlbumIds.map(async (albumId) => {
|
||||
try {
|
||||
return await immichClient.getAlbum(albumId);
|
||||
const page = await immichClient.searchPhotos({
|
||||
albumIds: [albumId],
|
||||
type: "IMAGE",
|
||||
page: 1,
|
||||
size: 1000,
|
||||
});
|
||||
return { albumId, items: page.items };
|
||||
} catch (err) {
|
||||
console.error("Failed to fetch granted album", albumId, err);
|
||||
return null;
|
||||
console.error("Failed to fetch granted album assets", albumId, err);
|
||||
return { albumId, items: [] };
|
||||
}
|
||||
})
|
||||
}),
|
||||
);
|
||||
|
||||
for (const album of grantedAlbums) {
|
||||
if (!album) continue;
|
||||
for (const asset of album.assets) {
|
||||
for (const { albumId, items } of albumAssets) {
|
||||
for (const asset of items) {
|
||||
if (!assetToGrantedAlbum.has(asset.id)) {
|
||||
assetToGrantedAlbum.set(asset.id, album.id);
|
||||
assetToGrantedAlbum.set(asset.id, albumId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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>;
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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;
|
||||
try {
|
||||
album = await immich.getAlbum(albumId);
|
||||
} catch {
|
||||
throw new UsageAccountingError("IMMICH_UNAVAILABLE");
|
||||
}
|
||||
|
||||
if (!album.assets.some((a) => a.id === assetId)) {
|
||||
// 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");
|
||||
}
|
||||
|
||||
return album;
|
||||
try {
|
||||
return await immich.getAlbum(albumId);
|
||||
} catch {
|
||||
throw new UsageAccountingError("IMMICH_UNAVAILABLE");
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
async function getValidatedAsset(immich: UsageImmich, assetId: string) {
|
||||
|
||||
@@ -199,6 +199,7 @@ test("0002 preserves dirty rows and reconstructs only provable legacy intents",
|
||||
const immich = {
|
||||
getAlbum: vi.fn(),
|
||||
getAsset: vi.fn(),
|
||||
searchPhotos: vi.fn(),
|
||||
};
|
||||
const usageService = createUsageService({
|
||||
repository: createUsageServiceRepository(testDb.database),
|
||||
|
||||
@@ -46,6 +46,12 @@ function createServices() {
|
||||
const immich = {
|
||||
getAlbum: vi.fn().mockResolvedValue({ id: albumId, albumName: "Presse 2026" }),
|
||||
getAsset: vi.fn().mockResolvedValue(asset),
|
||||
searchPhotos: vi.fn().mockResolvedValue({
|
||||
items: [{ id: asset.id }],
|
||||
total: 1,
|
||||
nextPage: null,
|
||||
previousPage: null,
|
||||
}),
|
||||
};
|
||||
return {
|
||||
usageService: createUsageService({
|
||||
|
||||
Reference in new issue
Block a user