import { json, readJsonBody, requireAdmin } from "../../../../_lib/auth"; import { MAX_PHOTOS_PER_ALBUM, scrapeAlbum } from "../../../../_lib/photos"; import { validAlbumUrl } from "../../../../_lib/store"; export const dynamic = "force-dynamic"; /** Reads a share link without saving it: album title and how many photos/videos it has. */ export async function POST(request: Request) { const denied = await requireAdmin(request, { mutating: true }); if (denied) return denied; const body = await readJsonBody<{ shareUrl?: string }>(request); const shareUrl = validAlbumUrl(body?.shareUrl); if (!shareUrl) return json({ ok: false, error: "Not a Google Photos share link." }, 400); try { const album = await scrapeAlbum(shareUrl); const videos = album.items.filter((item) => item.video).length; return json({ ok: true, title: album.title, photos: album.items.length - videos, videos, truncated: album.hasMorePages || album.items.length - videos > MAX_PHOTOS_PER_ALBUM, }); } catch (error) { return json({ ok: false, error: error instanceof Error ? error.message : "Could not read the album." }); } }