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
+83
-39
No files matched your search
@@ -31,25 +31,33 @@ export async function POST(request: NextRequest) {
|
|||||||
const trimmedPurpose = purpose.trim();
|
const trimmedPurpose = purpose.trim();
|
||||||
|
|
||||||
const grantedAlbumIds = await albumGrantService.getGrantedAlbumIds(principal.editorialId);
|
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 assetToGrantedAlbum = new Map<string, string>();
|
||||||
const grantedAlbums = await Promise.all(
|
const albumAssets = await Promise.all(
|
||||||
grantedAlbumIds.map(async (albumId) => {
|
grantedAlbumIds.map(async (albumId) => {
|
||||||
try {
|
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) {
|
} catch (err) {
|
||||||
console.error("Failed to fetch granted album", albumId, err);
|
console.error("Failed to fetch granted album assets", albumId, err);
|
||||||
return null;
|
return { albumId, items: [] };
|
||||||
}
|
}
|
||||||
})
|
}),
|
||||||
);
|
);
|
||||||
|
for (const { albumId, items } of albumAssets) {
|
||||||
for (const album of grantedAlbums) {
|
for (const asset of items) {
|
||||||
if (!album) continue;
|
|
||||||
for (const asset of album.assets) {
|
|
||||||
if (!assetToGrantedAlbum.has(asset.id)) {
|
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" },
|
headers: { "content-type": "application/json" },
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
albumIds: input.albumIds,
|
albumIds: input.albumIds,
|
||||||
|
...(input.id === undefined ? {} : { id: input.id }),
|
||||||
...(input.description === undefined ? {} : { description: input.description }),
|
...(input.description === undefined ? {} : { description: input.description }),
|
||||||
type: "IMAGE",
|
type: "IMAGE",
|
||||||
page: input.page,
|
page: input.page,
|
||||||
|
|||||||
@@ -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[]; description?: string; page: number; size: number; type?: "IMAGE" },
|
input: { albumIds: string[]; id?: string; description?: string; page: number; size: number; type?: "IMAGE" },
|
||||||
signal?: AbortSignal,
|
signal?: AbortSignal,
|
||||||
): Promise<ImmichSearchPage>;
|
): Promise<ImmichSearchPage>;
|
||||||
getThumbnail(assetId: string, signal?: AbortSignal): Promise<ImmichBinary>;
|
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 idempotencyKey = "2222222b-2222-4222-8222-22222222222b";
|
||||||
const assetId = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa";
|
const assetId = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa";
|
||||||
const albumId = "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb";
|
const albumId = "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb";
|
||||||
const otherAlbumId = "cccccccc-cccc-4ccc-8ccc-cccccccccccc";
|
|
||||||
|
|
||||||
const newUsageInput = {
|
const newUsageInput = {
|
||||||
editorialId,
|
editorialId,
|
||||||
@@ -103,6 +102,12 @@ function createService() {
|
|||||||
originalMimeType: "image/jpeg",
|
originalMimeType: "image/jpeg",
|
||||||
albums: [{ id: albumId }],
|
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 }) };
|
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 () => {
|
test("rejects an asset outside the exact requested album", async () => {
|
||||||
const { service, immich, usages } = createService();
|
const { service, immich, usages } = createService();
|
||||||
immich.getAsset.mockResolvedValue({
|
immich.searchPhotos.mockResolvedValue({
|
||||||
id: assetId,
|
items: [],
|
||||||
type: "IMAGE" as const,
|
total: 0,
|
||||||
originalFileName: "jubel.jpg",
|
nextPage: null,
|
||||||
originalMimeType: "image/jpeg",
|
previousPage: null,
|
||||||
albums: [{ id: otherAlbumId }],
|
|
||||||
});
|
});
|
||||||
|
|
||||||
await expect(service.createNewUsage(newUsageInput)).rejects.toMatchObject({
|
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 () => {
|
test("rejects removal from the requested album between preflight and locked refresh", async () => {
|
||||||
const { service, immich, usages } = createService();
|
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({
|
.mockResolvedValueOnce({
|
||||||
id: assetId,
|
items: [{ id: assetId }],
|
||||||
type: "IMAGE" as const,
|
total: 1,
|
||||||
originalFileName: "jubel.jpg",
|
nextPage: null,
|
||||||
originalMimeType: "image/jpeg",
|
previousPage: null,
|
||||||
albums: [{ id: albumId }],
|
|
||||||
})
|
})
|
||||||
.mockResolvedValueOnce({
|
.mockResolvedValueOnce({
|
||||||
id: assetId,
|
items: [],
|
||||||
type: "IMAGE" as const,
|
total: 0,
|
||||||
originalFileName: "jubel.jpg",
|
nextPage: null,
|
||||||
originalMimeType: "image/jpeg",
|
previousPage: null,
|
||||||
albums: [],
|
|
||||||
});
|
});
|
||||||
|
|
||||||
await expect(service.createNewUsage(newUsageInput)).rejects.toMatchObject({
|
await expect(service.createNewUsage(newUsageInput)).rejects.toMatchObject({
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ export type UsageRepository = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
type UsageImmich = {
|
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<{
|
getAsset(assetId: string): Promise<{
|
||||||
id: string;
|
id: string;
|
||||||
originalFileName: string;
|
originalFileName: string;
|
||||||
@@ -65,6 +65,9 @@ type UsageImmich = {
|
|||||||
type: string;
|
type: string;
|
||||||
albums: Array<{ id: 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({
|
export function createUsageService({
|
||||||
@@ -333,18 +336,39 @@ async function loadRequestedAlbum(
|
|||||||
assetId: string,
|
assetId: string,
|
||||||
albumId: string,
|
albumId: string,
|
||||||
): Promise<{ id: string; albumName: 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 {
|
try {
|
||||||
album = await immich.getAlbum(albumId);
|
return await immich.getAlbum(albumId);
|
||||||
} catch {
|
} catch {
|
||||||
throw new UsageAccountingError("IMMICH_UNAVAILABLE");
|
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) {
|
async function getValidatedAsset(immich: UsageImmich, assetId: string) {
|
||||||
|
|||||||
@@ -199,6 +199,7 @@ test("0002 preserves dirty rows and reconstructs only provable legacy intents",
|
|||||||
const immich = {
|
const immich = {
|
||||||
getAlbum: vi.fn(),
|
getAlbum: vi.fn(),
|
||||||
getAsset: vi.fn(),
|
getAsset: vi.fn(),
|
||||||
|
searchPhotos: vi.fn(),
|
||||||
};
|
};
|
||||||
const usageService = createUsageService({
|
const usageService = createUsageService({
|
||||||
repository: createUsageServiceRepository(testDb.database),
|
repository: createUsageServiceRepository(testDb.database),
|
||||||
|
|||||||
@@ -46,6 +46,12 @@ function createServices() {
|
|||||||
const immich = {
|
const immich = {
|
||||||
getAlbum: vi.fn().mockResolvedValue({ id: albumId, albumName: "Presse 2026" }),
|
getAlbum: vi.fn().mockResolvedValue({ id: albumId, albumName: "Presse 2026" }),
|
||||||
getAsset: vi.fn().mockResolvedValue(asset),
|
getAsset: vi.fn().mockResolvedValue(asset),
|
||||||
|
searchPhotos: vi.fn().mockResolvedValue({
|
||||||
|
items: [{ id: asset.id }],
|
||||||
|
total: 1,
|
||||||
|
nextPage: null,
|
||||||
|
previousPage: null,
|
||||||
|
}),
|
||||||
};
|
};
|
||||||
return {
|
return {
|
||||||
usageService: createUsageService({
|
usageService: createUsageService({
|
||||||
|
|||||||
Reference in new issue
Block a user