feat: add branded editorial photo catalog
This commit is contained in:
1 parent
f6e6018cc1
commit
66f0599653
20 files changed
+1299
-36
No files matched your search
@@ -0,0 +1,115 @@
|
||||
import "server-only";
|
||||
|
||||
import { requireActiveEditorial, requireAdmin, PrincipalAuthorizationError } from "@/modules/auth/principal";
|
||||
import { albumGrantService } from "@/modules/albums/grants";
|
||||
import { immichClient } from "@/modules/immich/client";
|
||||
import type { ImmichBinary } from "@/modules/immich/types";
|
||||
|
||||
type ThumbnailRouteContext = Readonly<{ params: Promise<{ assetId: string }> }>;
|
||||
|
||||
type ThumbnailImmich = {
|
||||
listAlbums(): Promise<Array<{ albumThumbnailAssetId: string | null }>>;
|
||||
getAlbum(albumId: string): Promise<{ assets: Array<{ id: string; type: "IMAGE" }> }>;
|
||||
getThumbnail(assetId: string): Promise<ImmichBinary>;
|
||||
};
|
||||
|
||||
export type ThumbnailHandlerDependencies = {
|
||||
requireActiveEditorial: typeof requireActiveEditorial;
|
||||
requireAdmin: typeof requireAdmin;
|
||||
getGrantedAlbumIds: typeof albumGrantService.getGrantedAlbumIds;
|
||||
immich: ThumbnailImmich;
|
||||
};
|
||||
|
||||
type Access = { kind: "admin" } | { kind: "editorial"; editorialId: string };
|
||||
|
||||
export function createThumbnailHandler(dependencies: ThumbnailHandlerDependencies) {
|
||||
return async function GET(request: Request, context: ThumbnailRouteContext): Promise<Response> {
|
||||
try {
|
||||
const { assetId } = await context.params;
|
||||
if (!isSafeAssetId(assetId)) {
|
||||
return genericNotFound();
|
||||
}
|
||||
|
||||
const access = await authorizeRequest(dependencies, request.headers);
|
||||
const authorized =
|
||||
access.kind === "admin"
|
||||
? await isAdminAlbumCover(dependencies.immich, assetId)
|
||||
: await isGrantedEditorialAsset(dependencies, access.editorialId, assetId);
|
||||
if (!authorized) {
|
||||
return genericNotFound();
|
||||
}
|
||||
|
||||
const thumbnail = await dependencies.immich.getThumbnail(assetId);
|
||||
return thumbnailResponse(thumbnail);
|
||||
} catch {
|
||||
return genericNotFound();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
async function authorizeRequest(
|
||||
dependencies: ThumbnailHandlerDependencies,
|
||||
requestHeaders: Headers,
|
||||
): Promise<Access> {
|
||||
try {
|
||||
const principal = await dependencies.requireActiveEditorial(requestHeaders);
|
||||
return { kind: "editorial", editorialId: principal.editorialId };
|
||||
} catch (error) {
|
||||
if (!(error instanceof PrincipalAuthorizationError) || error.code !== "ADMIN_REQUIRED") {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
await dependencies.requireAdmin(requestHeaders);
|
||||
return { kind: "admin" };
|
||||
}
|
||||
|
||||
async function isGrantedEditorialAsset(
|
||||
dependencies: ThumbnailHandlerDependencies,
|
||||
editorialId: string,
|
||||
assetId: string,
|
||||
): Promise<boolean> {
|
||||
const grantedAlbumIds = [...new Set(await dependencies.getGrantedAlbumIds(editorialId))];
|
||||
if (grantedAlbumIds.length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const grantedAlbums = await Promise.all(
|
||||
grantedAlbumIds.map((albumId) => dependencies.immich.getAlbum(albumId)),
|
||||
);
|
||||
return grantedAlbums.some((album) =>
|
||||
album.assets.some((asset) => asset.id === assetId && asset.type === "IMAGE"),
|
||||
);
|
||||
}
|
||||
|
||||
async function isAdminAlbumCover(immich: ThumbnailImmich, assetId: string): Promise<boolean> {
|
||||
const albums = await immich.listAlbums();
|
||||
return albums.some((album) => album.albumThumbnailAssetId === assetId);
|
||||
}
|
||||
|
||||
function thumbnailResponse(thumbnail: ImmichBinary): Response {
|
||||
const responseHeaders = new Headers({
|
||||
"cache-control": "private, no-store",
|
||||
"content-type": thumbnail.contentType,
|
||||
"x-content-type-options": "nosniff",
|
||||
});
|
||||
if (thumbnail.contentLength !== null) {
|
||||
responseHeaders.set("content-length", String(thumbnail.contentLength));
|
||||
}
|
||||
return new Response(thumbnail.body, { headers: responseHeaders });
|
||||
}
|
||||
|
||||
function genericNotFound(): Response {
|
||||
return new Response(null, { status: 404 });
|
||||
}
|
||||
|
||||
function isSafeAssetId(assetId: string): boolean {
|
||||
return /^[A-Za-z0-9_-]{1,200}$/.test(assetId);
|
||||
}
|
||||
|
||||
export const authorizedThumbnailHandler = createThumbnailHandler({
|
||||
requireActiveEditorial,
|
||||
requireAdmin,
|
||||
getGrantedAlbumIds: albumGrantService.getGrantedAlbumIds,
|
||||
immich: immichClient,
|
||||
});
|
||||
Reference in new issue
Block a user