add /api/file/random

This commit is contained in:
2023-06-19 10:15:11 +08:00
parent 3561c30587
commit 4684e16c82
3 changed files with 28 additions and 6 deletions

8
db.ts
View File

@@ -602,6 +602,14 @@ export class EhDb {
);
return s.length ? s[0] : undefined;
}
get_random_file() {
const s = this.convert_file(
this.db.queryEntries<EhFileRaw>(
"SELECT * FROM file ORDER BY RANDOM() LIMIT 1;",
),
);
return s.length ? s[0] : undefined;
}
get_tasks() {
return this.transaction(() =>
this.db.queryEntries<Task>("SELECT * FROM task;")

View File

@@ -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,

12
routes/api/file/random.ts Normal file
View File

@@ -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}`);
}
}