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

+20 -12
View File
@@ -31,25 +31,33 @@ export async function POST(request: NextRequest) {
const trimmedPurpose = purpose.trim();
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);
}
}
}