From 4684e16c82440d863a660ea417e379328b4535de Mon Sep 17 00:00:00 2001 From: lifegpc Date: Mon, 19 Jun 2023 10:15:11 +0800 Subject: [PATCH] add /api/file/random --- db.ts | 8 ++++++++ fresh.gen.ts | 14 ++++++++------ routes/api/file/random.ts | 12 ++++++++++++ 3 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 routes/api/file/random.ts diff --git a/db.ts b/db.ts index b96630c..2b41a07 100644 --- a/db.ts +++ b/db.ts @@ -602,6 +602,14 @@ export class EhDb { ); return s.length ? s[0] : undefined; } + get_random_file() { + const s = this.convert_file( + this.db.queryEntries( + "SELECT * FROM file ORDER BY RANDOM() LIMIT 1;", + ), + ); + return s.length ? s[0] : undefined; + } get_tasks() { return this.transaction(() => this.db.queryEntries("SELECT * FROM task;") diff --git a/fresh.gen.ts b/fresh.gen.ts index dac3b63..43e4cf7 100644 --- a/fresh.gen.ts +++ b/fresh.gen.ts @@ -9,9 +9,10 @@ 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/file/[id].ts"; -import * as $6 from "./routes/api/gallery/[gid].ts"; -import * as $7 from "./routes/api/task.ts"; -import * as $8 from "./routes/index.tsx"; +import * as $6 from "./routes/api/file/random.ts"; +import * as $7 from "./routes/api/gallery/[gid].ts"; +import * as $8 from "./routes/api/task.ts"; +import * as $9 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"; @@ -24,9 +25,10 @@ const manifest = { "./routes/api/exit.ts": $3, "./routes/api/export/gallery/zip/[gid].ts": $4, "./routes/api/file/[id].ts": $5, - "./routes/api/gallery/[gid].ts": $6, - "./routes/api/task.ts": $7, - "./routes/index.tsx": $8, + "./routes/api/file/random.ts": $6, + "./routes/api/gallery/[gid].ts": $7, + "./routes/api/task.ts": $8, + "./routes/index.tsx": $9, }, islands: { "./islands/Container.tsx": $$0, diff --git a/routes/api/file/random.ts b/routes/api/file/random.ts new file mode 100644 index 0000000..2d32b3d --- /dev/null +++ b/routes/api/file/random.ts @@ -0,0 +1,12 @@ +import { Handlers } from "$fresh/server.ts"; +import { get_task_manager } from "../../../server.ts"; + +export const handler: Handlers = { + GET(req, _ctx) { + const m = get_task_manager(); + const f = m.db.get_random_file(); + if (!f) return new Response("File not found.", { status: 404 }); + const u = new URL(req.url); + return Response.redirect(`${u.origin}/api/file/${f.id}`); + } +}