Build PROMPT media library with masonry UI and persistent PostgreSQL S3 storage
PROMPT CI and deployment / verify-and-deploy (push) Successful in 8m18s
PROMPT CI and deployment / verify-and-deploy (push) Successful in 8m18s
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { ZodError } from "zod";
|
||||
export function sameOrigin(req: NextApiRequest) {
|
||||
const origin=req.headers.origin;
|
||||
if (origin && new URL(origin).host !== req.headers.host) throw new Error("Cross-origin request rejected");
|
||||
}
|
||||
export function apiError(res: NextApiResponse,error: unknown) {
|
||||
if(error instanceof ZodError) return res.status(400).json({error:"请检查输入内容",issues:error.issues});
|
||||
const message=error instanceof Error?error.message:"";
|
||||
const clientMessages:Record<string,string>={
|
||||
'Register the coded page before creating its post':'请先在代码中注册对应的作品页面',
|
||||
'Referenced asset does not exist':'引用的素材不存在',
|
||||
'A media post needs matching content':'素材包需要包含对应类型的素材',
|
||||
'Cover must reference an image':'封面需要引用一张图片',
|
||||
'Unsupported image format':'支持 JPG、PNG、WebP、GIF 和 AVIF 图片',
|
||||
'File has no video stream':'文件中没有可用的画面',
|
||||
'Select an audio file':'请选择独立的声音文件',
|
||||
'Unsupported media MIME type':'文件类型不受支持',
|
||||
};
|
||||
if(clientMessages[message])return res.status(400).json({error:clientMessages[message]});
|
||||
const code=(error as {code?:string})?.code;
|
||||
if(code==='23505')return res.status(409).json({error:'这个网址标识已经存在,请换一个'});
|
||||
if(code==='23503')return res.status(400).json({error:'引用的分类或素材不存在'});
|
||||
if(["Invalid cursor","Tag not found","Cross-origin request rejected"].includes(message)) return res.status(400).json({error:message});
|
||||
console.error("API failure",error instanceof Error?error.message:"Unknown error");
|
||||
return res.status(500).json({error:"操作没有完成,请稍后重试"});
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Pool, type PoolClient } from "pg";
|
||||
|
||||
const globalDb = globalThis as unknown as { promptPool?: Pool };
|
||||
export function db() {
|
||||
if (!process.env.DATABASE_URL) throw new Error("DATABASE_URL is required");
|
||||
return globalDb.promptPool ??= new Pool({ connectionString: process.env.DATABASE_URL, max: 10 });
|
||||
}
|
||||
export async function transaction<T>(run: (client: PoolClient) => Promise<T>): Promise<T> {
|
||||
const client = await db().connect();
|
||||
try { await client.query("BEGIN"); const result = await run(client); await client.query("COMMIT"); return result; }
|
||||
catch (error) { await client.query("ROLLBACK"); throw error; }
|
||||
finally { client.release(); }
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { db } from "./db";
|
||||
import { decodeCursor, encodeCursor, type FeedData, type Post, type PostKind, type Tag } from "./model";
|
||||
import { works } from "../content/registry";
|
||||
const tagTree = `WITH RECURSIVE tree AS (SELECT *,slug::text AS path FROM tags WHERE parent_id IS NULL UNION ALL SELECT t.*,tree.path||'/'||t.slug FROM tags t JOIN tree ON t.parent_id=tree.id) SELECT * FROM tree`;
|
||||
export async function tags(): Promise<Tag[]> { return (await db().query(`${tagTree} ORDER BY path`)).rows; }
|
||||
const expand = `SELECT p.*,
|
||||
COALESCE((SELECT jsonb_agg(ae ORDER BY ae.position) FROM (SELECT a.*,pa.position,pa.role,COALESCE((SELECT jsonb_agg(v.role) FROM asset_variants v WHERE v.asset_id=a.id),'[]') AS variants FROM post_assets pa JOIN assets a ON a.id=pa.asset_id WHERE pa.post_id=p.id) ae),'[]') AS assets,
|
||||
COALESCE((SELECT jsonb_agg(t) FROM tags t JOIN post_tags pt ON pt.tag_id=t.id WHERE pt.post_id=p.id),'[]') AS tags FROM posts p`;
|
||||
function serialize(posts: Post[], allTags: Tag[]): Post[] {
|
||||
return posts.map(p => ({...p, created_at: new Date(p.created_at).toISOString(), tags:p.tags.map(t=>allTags.find(a=>a.id===t.id)!).filter(Boolean)}));
|
||||
}
|
||||
export async function getPost(slug: string): Promise<Post | null> {
|
||||
const result = await db().query(`${expand} WHERE p.slug=$1`,[slug]);
|
||||
return serialize(result.rows,await tags())[0] || null;
|
||||
}
|
||||
export async function feed(options: {kind?: PostKind; path?: string; cursor?: string; q?: string} = {}): Promise<FeedData> {
|
||||
const allTags=await tags(); const target=options.path ? allTags.find(t=>t.path===options.path) : undefined;
|
||||
if(options.path&&!target) throw new Error("Tag not found");
|
||||
const cursor=decodeCursor(options.cursor); const values: unknown[]=[];
|
||||
const where=[`((p.kind IN ('article','motion') AND p.page_key=ANY($1::text[])) OR EXISTS(SELECT 1 FROM post_assets pa JOIN assets a ON a.id=pa.asset_id WHERE pa.post_id=p.id AND pa.role='content' AND a.kind=p.kind AND a.storage_key IS NOT NULL AND a.byte_size>0))`];
|
||||
values.push(Object.keys(works));
|
||||
if(options.kind){values.push(options.kind);where.push(`p.kind=$${values.length}`);}
|
||||
if(target){values.push(allTags.filter(t=>t.path===target.path||t.path.startsWith(target.path+"/")).map(t=>t.id));where.push(`EXISTS(SELECT 1 FROM post_tags WHERE post_id=p.id AND tag_id=ANY($${values.length}::uuid[]))`);}
|
||||
if(options.q){values.push(`%${options.q.replace(/[\\%_]/g,"\\$&")}%`);where.push(`p.title ILIKE $${values.length}`);}
|
||||
if(cursor){values.push(...cursor);where.push(`(p.created_at,p.id)<($${values.length-1}::timestamptz,$${values.length}::uuid)`);}
|
||||
const rows=(await db().query(`${expand} WHERE ${where.join(' AND ')} ORDER BY p.created_at DESC,p.id DESC LIMIT 25`,values)).rows as Post[];
|
||||
const hasMore=rows.length>24; const posts=serialize(rows.slice(0,24),allTags); const last=posts.at(-1);
|
||||
const contributions=(await db().query(`SELECT to_char(occurred_at AT TIME ZONE 'Asia/Shanghai','YYYY-MM-DD') AS day,sum(amount)::integer AS total FROM contribution_events WHERE occurred_at>=now()-interval '366 days' GROUP BY day ORDER BY day`)).rows;
|
||||
return {posts,tags:allTags,contributions,nextCursor:hasMore&&last?encodeCursor(last.created_at,last.id):null};
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
export const sections = [
|
||||
{ path: "images", kind: "image", name: "图片素材", hint: "照片、插画与可以反复使用的画面" },
|
||||
{ path: "videos", kind: "video", name: "视频素材", hint: "片段与镜头,声音随画面一起收好" },
|
||||
{ path: "audio", kind: "audio", name: "声音素材", hint: "音乐、环境声与声音片段" },
|
||||
{ path: "articles", kind: "article", name: "图文作品", hint: "用网页编写,慢慢打磨每一页" },
|
||||
{ path: "motion", kind: "motion", name: "动态作品", hint: "让网页本身成为时间里的画面" },
|
||||
] as const;
|
||||
export type PostKind = typeof sections[number]["kind"];
|
||||
export type Asset = { id: string; kind: "image" | "video" | "audio" | "text"; filename: string | null; body: string | null; width: number | null; height: number | null; duration: number | null; has_audio: boolean | null; processing_status: string; variants: string[] };
|
||||
export type Tag = { id: string; parent_id: string | null; name: string; slug: string; path: string };
|
||||
export type Post = { id: string; slug: string; kind: PostKind; title: string; created_at: string; page_key: string | null; cover_asset_id: string | null; assets: Asset[]; tags: Tag[] };
|
||||
export type FeedData = { posts: Post[]; tags: Tag[]; contributions: { day: string; total: number }[]; nextCursor: string | null; unavailable?: boolean };
|
||||
export function validSection(path: string) { return sections.find(s => s.path === path); }
|
||||
export const mediaUrl = (id: string, variant?: string) => `/api/media/${id}${variant ? `?variant=${variant}` : ""}`;
|
||||
export function encodeCursor(date: string, id: string) { return Buffer.from(JSON.stringify([date,id])).toString("base64url"); }
|
||||
export function decodeCursor(value?: string) {
|
||||
if (!value) return null;
|
||||
try { const [date,id] = JSON.parse(Buffer.from(value,"base64url").toString());
|
||||
if (!Number.isNaN(Date.parse(date)) && /^[0-9a-f-]{36}$/.test(id)) return [date,id];
|
||||
} catch { /* invalid pagination cursor */ }
|
||||
throw new Error("Invalid cursor");
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { promisify } from "node:util";
|
||||
export const run=promisify(execFile);
|
||||
export async function probe(file:string) {
|
||||
const {stdout}=await run(process.env.FFPROBE_PATH||'ffprobe',['-v','error','-protocol_whitelist','file,pipe','-show_streams','-show_format','-of','json',file],{timeout:30000,maxBuffer:2*1024*1024});
|
||||
return JSON.parse(stdout) as {streams:{codec_type:string;width?:number;height?:number}[];format:{duration?:string;format_name?:string}};
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { S3Client, PutObjectCommand, GetObjectCommand, DeleteObjectCommand, HeadBucketCommand, CreateBucketCommand } from "@aws-sdk/client-s3";
|
||||
import { createReadStream } from "node:fs";
|
||||
import { stat } from "node:fs/promises";
|
||||
let instance: S3Client;
|
||||
export function storage() {
|
||||
return instance ??= new S3Client({
|
||||
endpoint: process.env.S3_ENDPOINT, region: "us-east-1", forcePathStyle: true,
|
||||
credentials: { accessKeyId: process.env.S3_ACCESS_KEY!, secretAccessKey: process.env.S3_SECRET_KEY! },
|
||||
requestChecksumCalculation: "WHEN_REQUIRED", responseChecksumValidation: "WHEN_REQUIRED",
|
||||
});
|
||||
}
|
||||
export const bucket = () => process.env.S3_BUCKET || "prompt";
|
||||
export async function ensureBucket() {
|
||||
try { await storage().send(new HeadBucketCommand({ Bucket: bucket() })); }
|
||||
catch (e) {
|
||||
if ((e as { $metadata?: { httpStatusCode: number } }).$metadata?.httpStatusCode !== 404) throw e;
|
||||
await storage().send(new CreateBucketCommand({ Bucket: bucket() }));
|
||||
}
|
||||
}
|
||||
export async function putFile(key: string, path: string, mime: string) {
|
||||
const size = (await stat(path)).size;
|
||||
await storage().send(new PutObjectCommand({ Bucket: bucket(), Key: key, Body: createReadStream(path), ContentLength: size, ContentType: mime }));
|
||||
return size;
|
||||
}
|
||||
export const getObject = (key: string, range?: string) => storage().send(new GetObjectCommand({ Bucket: bucket(), Key: key, Range: range }));
|
||||
export const removeObject = (key: string) => storage().send(new DeleteObjectCommand({ Bucket: bucket(), Key: key }));
|
||||
@@ -0,0 +1,27 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { z } from "zod";
|
||||
import { transaction } from "./db";
|
||||
import { works } from "../content/registry";
|
||||
export const postInput=z.object({
|
||||
slug:z.string().regex(/^[a-z0-9][a-z0-9-]{0,119}$/).optional(),
|
||||
kind:z.enum(["image","video","audio","article","motion"]),title:z.string().trim().min(1).max(200),
|
||||
assets:z.array(z.object({id:z.uuid(),role:z.enum(["content","caption"]).default("content")})).max(100).default([]),
|
||||
text:z.string().trim().max(100000).optional(),tags:z.array(z.uuid()).max(50).default([]),
|
||||
coverAssetId:z.uuid().optional(),pageKey:z.string().max(120).optional(),
|
||||
});
|
||||
export async function createPost(input:z.infer<typeof postInput>) {
|
||||
const id=randomUUID(); const slug=input.slug||id;
|
||||
if(["article","motion"].includes(input.kind)&&(!input.pageKey||works[input.pageKey]?.kind!==input.kind)) throw new Error("Register the coded page before creating its post");
|
||||
return transaction(async client=>{
|
||||
const ids=[...new Set(input.assets.map(a=>a.id))];
|
||||
const assets=ids.length?(await client.query('SELECT id,kind FROM assets WHERE id=ANY($1::uuid[])',[ids])).rows:[];
|
||||
if(assets.length!==ids.length) throw new Error("Referenced asset does not exist");
|
||||
if(["image","video","audio"].includes(input.kind)&&!input.assets.some(link=>link.role==='content'&&assets.some(a=>a.id===link.id&&a.kind===input.kind))) throw new Error("A media post needs matching content");
|
||||
if(input.coverAssetId && !(await client.query("SELECT 1 FROM assets WHERE id=$1 AND kind='image'",[input.coverAssetId])).rowCount) throw new Error("Cover must reference an image");
|
||||
await client.query('INSERT INTO posts(id,slug,kind,title,page_key,cover_asset_id) VALUES($1,$2,$3,$4,$5,$6)',[id,slug,input.kind,input.title,input.pageKey||null,input.coverAssetId||null]);
|
||||
for(const [position,a] of input.assets.entries()) await client.query('INSERT INTO post_assets(post_id,asset_id,position,role) VALUES($1,$2,$3,$4)',[id,a.id,position,a.role]);
|
||||
if(input.text){const textId=randomUUID();await client.query("INSERT INTO assets(id,kind,body) VALUES($1,'text',$2)",[textId,input.text]);await client.query("INSERT INTO post_assets(post_id,asset_id,position,role) VALUES($1,$2,$3,'caption')",[id,textId,input.assets.length]);}
|
||||
for(const tag of new Set(input.tags)) await client.query('INSERT INTO post_tags(post_id,tag_id) VALUES($1,$2)',[id,tag]);
|
||||
return {id,slug};
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user