Add panorama previews and repair beta export rendering
This commit is contained in:
93
lib/api.ts
93
lib/api.ts
@@ -6,6 +6,7 @@ import { admin, required, user } from "./supabase";
|
||||
import { datasetSchema, faces, difficulties } from "../shared/protocol";
|
||||
import { mediaUrl } from "./catalog";
|
||||
import { requestOrigin, verifyOrigin } from "./request-origin";
|
||||
import { selectDraftLocations } from "../shared/draft-selection";
|
||||
const hash = (value: string) =>
|
||||
createHash("sha256").update(value).digest("hex");
|
||||
const idSchema = z.string().uuid();
|
||||
@@ -355,6 +356,21 @@ export async function handle(request: NextRequest, path: string[]) {
|
||||
});
|
||||
}
|
||||
const d = await ownedDataset(request, path[1]);
|
||||
if (method === "GET" && path[2] === "panoramas") {
|
||||
const input = required(
|
||||
await client
|
||||
.from("dataset_inputs")
|
||||
.select("manifest")
|
||||
.eq("dataset_id", d.id)
|
||||
.single(),
|
||||
);
|
||||
const manifest = datasetSchema.parse(input.manifest);
|
||||
return panoramaResponse(
|
||||
d.id,
|
||||
manifest.availableViewpoints ?? manifest.viewpoints,
|
||||
path[3],
|
||||
);
|
||||
}
|
||||
if (method === "GET") {
|
||||
const input = required(
|
||||
await client
|
||||
@@ -376,12 +392,14 @@ export async function handle(request: NextRequest, path: string[]) {
|
||||
manifest,
|
||||
map: await mediaUrl(d.map_path),
|
||||
previews: await Promise.all(
|
||||
manifest.viewpoints.map(async (p: { id: string }) => ({
|
||||
id: p.id,
|
||||
image: d.map_path
|
||||
? await mediaUrl(`${d.id}/${p.id}/preview.webp`)
|
||||
: null,
|
||||
})),
|
||||
(manifest.availableViewpoints ?? manifest.viewpoints).map(
|
||||
async (p: { id: string }) => ({
|
||||
id: p.id,
|
||||
image: d.map_path
|
||||
? await mediaUrl(`${d.id}/${p.id}/preview.webp`)
|
||||
: null,
|
||||
}),
|
||||
),
|
||||
),
|
||||
});
|
||||
}
|
||||
@@ -408,9 +426,7 @@ export async function handle(request: NextRequest, path: string[]) {
|
||||
description: body.description,
|
||||
};
|
||||
if (body.selectedIds)
|
||||
manifest.viewpoints = manifest.viewpoints.filter(
|
||||
(p: { id: string }) => body.selectedIds!.includes(p.id),
|
||||
);
|
||||
manifest = selectDraftLocations(manifest, body.selectedIds);
|
||||
manifest = datasetSchema.parse(manifest);
|
||||
required(
|
||||
await client
|
||||
@@ -427,8 +443,10 @@ export async function handle(request: NextRequest, path: string[]) {
|
||||
name: body.name,
|
||||
description: body.description,
|
||||
panorama_count: manifest.viewpoints.length,
|
||||
cover_path: `${d.id}/${manifest.viewpoints[0].id}/preview.webp`,
|
||||
state: "draft",
|
||||
rejection_reason: null,
|
||||
updated_at: new Date().toISOString(),
|
||||
})
|
||||
.eq("id", d.id)
|
||||
.select("id")
|
||||
@@ -596,6 +614,29 @@ export async function handle(request: NextRequest, path: string[]) {
|
||||
}
|
||||
if (path[0] === "moderation") {
|
||||
const reviewer = await moderator(request);
|
||||
if (method === "GET" && path[2] === "panoramas") {
|
||||
const d = required(
|
||||
await client
|
||||
.from("datasets")
|
||||
.select("id,state")
|
||||
.eq("id", uuid(path[1]))
|
||||
.single(),
|
||||
);
|
||||
if (!["review", "published"].includes(d.state))
|
||||
throw new Error("Map is not awaiting review or published");
|
||||
const input = required(
|
||||
await client
|
||||
.from("dataset_inputs")
|
||||
.select("manifest")
|
||||
.eq("dataset_id", d.id)
|
||||
.single(),
|
||||
);
|
||||
return panoramaResponse(
|
||||
d.id,
|
||||
datasetSchema.parse(input.manifest).viewpoints,
|
||||
path[3],
|
||||
);
|
||||
}
|
||||
if (method === "GET") {
|
||||
const maps = required(
|
||||
await client
|
||||
@@ -657,6 +698,21 @@ export async function handle(request: NextRequest, path: string[]) {
|
||||
);
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
if (path[0] === "reports" && method === "PATCH") {
|
||||
await moderator(request);
|
||||
const b = z
|
||||
.object({ state: z.literal("resolved") })
|
||||
.parse(await jsonBody(request));
|
||||
required(
|
||||
await client
|
||||
.from("reports")
|
||||
.update({ state: b.state })
|
||||
.eq("id", uuid(path[1]))
|
||||
.select("id")
|
||||
.single(),
|
||||
);
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
} catch (error) {
|
||||
const message =
|
||||
@@ -675,3 +731,22 @@ export async function handle(request: NextRequest, path: string[]) {
|
||||
return NextResponse.json({ error: message }, { status });
|
||||
}
|
||||
}
|
||||
|
||||
async function panoramaResponse(
|
||||
datasetId: string,
|
||||
locations: { id: string }[],
|
||||
pointId?: string,
|
||||
) {
|
||||
if (!pointId)
|
||||
return NextResponse.json({ locations: locations.map((p) => p.id) });
|
||||
if (!locations.some((p) => p.id === pointId))
|
||||
throw new Error("Location not found");
|
||||
return NextResponse.json(
|
||||
{
|
||||
faces: await Promise.all(
|
||||
faces.map((f) => mediaUrl(`${datasetId}/${pointId}/${f}.webp`)),
|
||||
),
|
||||
},
|
||||
{ headers: { "Cache-Control": "no-store" } },
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user