SPB Git

Repository Public

exports.ts 2.8 KB 45 lines typescript

1/** Export requests (web side). Files are produced by the worker: src/worker/export.ts. */2import { z } from "zod";3import { j, sql } from "@/lib/db";4import { ApiError } from "@/lib/http";56export const ExportRequest = z.object({7  sheetId: z.string().uuid(),8  format: z.enum(["svg", "png", "pdf"]),9  /** PNG pixel ratio. */10  scale: z.union([z.literal(1), z.literal(2), z.literal(4)]).default(2),11  /** A single block (required for SVG: vector blocks only). */12  blockId: z.string().regex(/^[a-z0-9][a-z0-9-]{0,47}$/).optional(),13  /** A stored revision; defaults to the working copy. */14  revisionId: z.string().uuid().optional(),15}).strict();1617interface ExportRow { id: string; sheet_id: string; format: string; options: unknown; status: string; asset_id: string | null; error: string | null; created_at: Date; finished_at: Date | null }18const view = (r: ExportRow) => ({ id: r.id, sheetId: r.sheet_id, format: r.format, options: r.options, status: r.status, assetId: r.asset_id, error: r.error, createdAt: r.created_at.toISOString(), finishedAt: r.finished_at?.toISOString() ?? null, downloadUrl: r.asset_id ? `/api/admin/assets/${r.asset_id}/file?download=1` : null });1920export async function requestExport(req: z.infer<typeof ExportRequest>, userId: string) {21  const [sheet] = await sql<{ current_revision_id: string }[]>`SELECT current_revision_id FROM sheets WHERE id = ${req.sheetId} AND deleted_at IS NULL`;22  if (!sheet) throw new ApiError(404, "not_found", "Sheet not found");23  if (req.format === "pdf" && req.blockId) throw new ApiError(422, "whole_sheet_only", "PDF export applies to the whole sheet; export a single block as PNG or SVG");24  if (req.format === "svg" && !req.blockId) throw new ApiError(422, "block_required", "SVG export applies to one vector block (diagram, chart, matrix, timeline, illustration)");25  const revisionId = req.revisionId ?? sheet.current_revision_id;26  const [rev] = await sql`SELECT 1 FROM sheet_revisions WHERE id = ${revisionId} AND sheet_id = ${req.sheetId}`;27  if (!rev) throw new ApiError(404, "not_found", "Revision not found");28  const [row] = await sql<ExportRow[]>`29    INSERT INTO export_jobs (sheet_id, revision_id, format, options, created_by)30    VALUES (${req.sheetId}, ${revisionId}, ${req.format}, ${j({ scale: req.scale, blockId: req.blockId ?? null })}, ${userId}) RETURNING *`;31  await sql`SELECT pg_notify('sheet_jobs', ${row.id})`;32  return view(row);33}3435export async function getExport(id: string) {36  const [row] = await sql<ExportRow[]>`SELECT * FROM export_jobs WHERE id = ${id}`;37  if (!row) throw new ApiError(404, "not_found", "Export not found");38  return view(row);39}4041export async function listExports(sheetId: string) {42  const rows = await sql<ExportRow[]>`SELECT * FROM export_jobs WHERE sheet_id = ${sheetId} AND NOT is_public ORDER BY created_at DESC LIMIT 30`;43  return rows.map(view);44}45
Files main
Menu

Theme