diff --git a/db.ts b/db.ts index 23357d3..2631ecb 100644 --- a/db.ts +++ b/db.ts @@ -60,7 +60,7 @@ export type PMeta = { width: number; height: number; }; -type Tag = { +export type Tag = { id: number; tag: string; translated: string | undefined; @@ -504,6 +504,12 @@ export class EhDb { ).map((v) => v[0]), ); } + get_gtags_full(gid: number) { + return this.db.queryEntries( + "SELECT tag.* FROM gtag INNER JOIN tag ON tag.id = gtag.id WHERE gid = ?;", + [gid], + ); + } get_pmeta(gid: number) { return this.db.queryEntries( "SELECT * FROM pmeta WHERE gid = ?;", diff --git a/fresh.gen.ts b/fresh.gen.ts index 3e8b450..5ea8397 100644 --- a/fresh.gen.ts +++ b/fresh.gen.ts @@ -8,8 +8,9 @@ import * as $1 from "./routes/api/config.ts"; import * as $2 from "./routes/api/deploy_id.ts"; import * as $3 from "./routes/api/exit.ts"; import * as $4 from "./routes/api/export/gallery/zip/[gid].ts"; -import * as $5 from "./routes/api/task.ts"; -import * as $6 from "./routes/index.tsx"; +import * as $5 from "./routes/api/gallery/[gid].ts"; +import * as $6 from "./routes/api/task.ts"; +import * as $7 from "./routes/index.tsx"; import * as $$0 from "./islands/Container.tsx"; import * as $$1 from "./islands/Settings.tsx"; import * as $$2 from "./islands/TaskManager.tsx"; @@ -21,8 +22,9 @@ const manifest = { "./routes/api/deploy_id.ts": $2, "./routes/api/exit.ts": $3, "./routes/api/export/gallery/zip/[gid].ts": $4, - "./routes/api/task.ts": $5, - "./routes/index.tsx": $6, + "./routes/api/gallery/[gid].ts": $5, + "./routes/api/task.ts": $6, + "./routes/index.tsx": $7, }, islands: { "./islands/Container.tsx": $$0, diff --git a/routes/_middleware.ts b/routes/_middleware.ts index bb7f963..ce5f60e 100644 --- a/routes/_middleware.ts +++ b/routes/_middleware.ts @@ -28,6 +28,7 @@ export async function handler(req: Request, ctx: MiddlewareHandlerContext) { k = p; } const data = map.inputs[k]; + // @ts-ignore add custom property data.md5 = await calFileMd5(k); }, ); @@ -41,6 +42,7 @@ export async function handler(req: Request, ctx: MiddlewareHandlerContext) { k = p; } const data = map.outputs[k]; + // @ts-ignore add custom property data.md5 = await calFileMd5(k); }, ); diff --git a/routes/api/gallery/[gid].ts b/routes/api/gallery/[gid].ts new file mode 100644 index 0000000..a593738 --- /dev/null +++ b/routes/api/gallery/[gid].ts @@ -0,0 +1,22 @@ +import { Handlers } from "$fresh/server.ts"; +import { get_task_manager } from "../../../server.ts"; +import { GalleryData } from "../../../server/gallery.ts"; +import { return_data, return_error } from "../../../server/utils.ts"; + +export const handler: Handlers = { + GET(_req, ctx) { + const gid = parseInt(ctx.params.gid); + if (isNaN(gid)) { + return return_error(400, "Failed to parse gid."); + } + const m = get_task_manager(); + const meta = m.db.get_gmeta_by_gid(gid); + if (!meta) return return_error(404, "Gallery not found."); + const data: GalleryData = { + meta, + tags: m.db.get_gtags_full(gid).sort((a, b) => a.id - b.id), + pages: m.db.get_pmeta(gid).sort((a, b) => a.index - b.index), + }; + return return_data(data); + }, +}; diff --git a/server/gallery.ts b/server/gallery.ts new file mode 100644 index 0000000..eeb2dcc --- /dev/null +++ b/server/gallery.ts @@ -0,0 +1,10 @@ +import { GMeta, PMeta, Tag } from "../db.ts"; +import { JSONResult } from "./utils.ts"; + +export type GalleryData = { + meta: GMeta; + tags: Tag[]; + pages: PMeta[]; +}; + +export type GalleryResult = JSONResult; diff --git a/server/utils.ts b/server/utils.ts index 4346361..b571039 100644 --- a/server/utils.ts +++ b/server/utils.ts @@ -2,3 +2,35 @@ export function get_ws_host() { const protocol = document.location.protocol === "https:" ? "wss:" : "ws:"; return `${protocol}//${document.location.host}`; } + +export type JSONResult = { + ok: true; + status: 0; + data: T; +} | { + ok: false; + status: Exclude; + error: string; +}; + +function gen_response(d: JSONResult) { + let status = 200; + if (d.status !== 0) { + status = (d.status >= 400 && d.status < 600) ? d.status : 400; + } + return new Response(JSON.stringify(d), { + status, + headers: { "Content-Type": "application/json" }, + }); +} + +export function return_error( + status: Exclude, + error: string, +) { + return gen_response({ ok: false, status, error }); +} + +export function return_data(data: T) { + return gen_response({ ok: true, status: 0, data }); +}