Skip to content

Commit

Permalink
Support add task via PUT requests
Browse files Browse the repository at this point in the history
  • Loading branch information
lifegpc committed Feb 7, 2024
1 parent 1e72c05 commit f5a5608
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 6 deletions.
9 changes: 8 additions & 1 deletion db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import { unescape } from "std/html/mod.ts";
import { join, resolve } from "std/path/mod.ts";
import { SqliteError } from "sqlite/mod.ts";
import { SqliteError as Sqlite3Error } from "sqlite3/mod.ts";
import { Status } from "sqlite/src/constants.ts";
import { parse_bool, sleep, sure_dir_sync, try_remove_sync } from "./utils.ts";
import { Task, TaskType } from "./task.ts";
Expand Down Expand Up @@ -131,7 +132,9 @@ export enum UserPermission {
None = 0,
ReadGallery = 1 << 0,
EditGallery = 1 << 1,
All = ~(~0 << 2),
DeleteGallery = 1 << 2,
ManageTasks = 1 << 3,
All = ~(~0 << 4),
}
export type User = {
id: number;
Expand Down Expand Up @@ -721,6 +724,10 @@ export class EhDb {
if (e instanceof SqliteError) {
if (e.code == Status.SqliteBusy) return false;
}
if (e instanceof Sqlite3Error) {
// SQLITE_BUSY
if (e.code == 5) return false;
}
throw e;
}
}
Expand Down
3 changes: 2 additions & 1 deletion routes/api/health_check.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Handlers } from "$fresh/server.ts";
import { return_data } from "../../server/utils.ts";

export const handler: Handlers = {
GET(_req, _ctx) {
return new Response("OK");
return return_data(true);
},
};
80 changes: 78 additions & 2 deletions routes/api/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@ import type {
TaskServerSocketData,
} from "../../server/task.ts";
import { ExitTarget } from "../../signal_handler.ts";
import { get_string, parse_int } from "../../server/parse_form.ts";
import { return_data, return_error } from "../../server/utils.ts";
import type { DownloadConfig } from "../../tasks/download.ts";
import type { ExportZipConfig } from "../../tasks/export_zip.ts";
import { User, UserPermission } from "../../db.ts";

export const handler: Handlers<Task[]> = {
GET(req, _ctx) {
export const handler: Handlers = {
GET(req, ctx) {
const u = <User | undefined> ctx.state.user;
if (u && !u.is_admin && !(u.permissions & UserPermission.ManageTasks)) {
return return_error(403, "Permission denied.");
}
const t = get_task_manager();
const { socket, response } = Deno.upgradeWebSocket(req);
const handle = (
Expand Down Expand Up @@ -77,4 +86,71 @@ export const handler: Handlers<Task[]> = {
};
return response;
},
async PUT(req, ctx) {
const u = <User | undefined> ctx.state.user;
if (u && !u.is_admin && !(u.permissions & UserPermission.ManageTasks)) {
return return_error(403, "Permission denied.");
}
const t = get_task_manager();
let form: FormData | null = null;
try {
form = await req.formData();
} catch (_) {
return return_error(400, "Bad Request");
}
const typ = await get_string(form.get("type"));
if (!typ) {
return return_error(1, "type is required");
}
if (typ == "download") {
const gid = await parse_int(form.get("gid"), null);
const token = await get_string(form.get("token"));
if (gid === null) {
return return_error(2, "gid is required");
}
if (!token) {
return return_error(3, "token is required");
}
const cfg = await get_string(form.get("cfg"));
let dcfg: DownloadConfig | undefined = undefined;
if (cfg) {
try {
dcfg = JSON.parse(cfg);
} catch (_) {
return return_error(4, "cfg is invalid");
}
}
try {
const task = t.add_download_task(gid, token, dcfg, true);
if (task === null) {
return return_error(6, "task is already in the list");
}
return return_data(task, 201);
} catch (e) {
return return_error(500, e.message);
}
} else if (typ == "export_zip") {
const gid = await parse_int(form.get("gid"), null);
if (gid === null) {
return return_error(2, "gid is required");
}
const cfg = await get_string(form.get("cfg"));
let dcfg: ExportZipConfig | undefined = undefined;
if (cfg) {
try {
dcfg = JSON.parse(cfg);
} catch (_) {
return return_error(4, "cfg is invalid");
}
}
try {
const task = t.add_export_zip_task(gid, dcfg);
return return_data(task, 201);
} catch (e) {
return return_error(500, e.message);
}
} else {
return return_error(5, "unknown type");
}
},
};
9 changes: 7 additions & 2 deletions task_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,17 @@ export class TaskManager extends EventTarget {
get aborts() {
return this.#abort.signal;
}
async add_download_task(gid: number, token: string, cfg?: DownloadConfig) {
async add_download_task(
gid: number,
token: string,
cfg?: DownloadConfig,
mark_already = false,
) {
this.#check_closed();
const otask = await this.db.check_download_task(gid, token);
if (otask !== undefined) {
console.log("The task is already in list.");
return otask;
return mark_already ? null : otask;
}
const task: Task = {
gid,
Expand Down

0 comments on commit f5a5608

Please sign in to comment.