Better handle bigint

This commit is contained in:
2024-05-31 14:24:57 +08:00
parent 15ff42d8c4
commit 8437bf1436
36 changed files with 320 additions and 227 deletions

View File

@@ -1,29 +0,0 @@
import { get_task_manager } from "../server.ts";
import { parse_cookies } from "./cookies.ts";
export function check_auth(req: Request) {
if (req.method === "OPTIONS") return true;
const m = get_task_manager();
if (m.db.get_user_count() === 0) return true;
const u = new URL(req.url);
let token: string | null | undefined = req.headers.get("X-TOKEN");
const cookies = parse_cookies(req.headers.get("Cookie"));
if (!token) {
token = cookies.get("token");
}
const check = () => {
if (u.pathname === "/api/token" && req.method === "PUT") return true;
if (u.pathname === "/api/status" && req.method === "GET") return true;
return false;
};
if (!token) return check();
const t = m.db.get_token(token);
const now = (new Date()).getTime();
if (!t || t.expired.getTime() < now) return check();
const user = m.db.get_user(t.uid);
if (!user) {
m.db.delete_token(token);
return check();
}
return true;
}

View File

@@ -1,16 +1,23 @@
import { Uint8ArrayReader, ZipWriter } from "zipjs/index.js";
import type { EhDb, PMeta } from "../db.ts";
import type { ExportZipConfig } from "../tasks/export_zip.ts";
import { addZero, configureZipJs, limitFilename } from "../utils.ts";
import {
addZero,
compareNum,
configureZipJs,
limitFilename,
} from "../utils.ts";
export function get_export_zip_response(
gid: number,
gid: number | bigint,
db: EhDb,
cfg: ExportZipConfig,
) {
const gmeta = db.get_gmeta_by_gid(gid);
if (!gmeta) return new Response("Gallery not found.", { status: 404 });
const pmetas = db.get_pmeta(gid).sort((a, b) => a.index - b.index);
const pmetas = db.get_pmeta(gid).sort((a, b) =>
compareNum(a.index, b.index)
);
const p = pmetas.length;
const l = gmeta.filecount.toString().length;
let c = 0;

View File

@@ -1,14 +1,14 @@
export type EhFileBasic = {
id: number;
width: number;
height: number;
id: number | bigint;
width: number | bigint;
height: number | bigint;
is_original: boolean;
};
export type EhFileExtend = {
id: number;
width: number;
height: number;
id: number | bigint;
width: number | bigint;
height: number | bigint;
is_original: boolean;
token: string;
};

View File

@@ -1,3 +1,5 @@
import { isNumNaN, parseBigInt } from "../utils.ts";
export async function get_string(value: FormDataEntryValue | null) {
if (value === null) return null;
return typeof value === "string" ? value : await value.text();
@@ -28,3 +30,14 @@ export async function parse_int<T extends number | null>(
if (isNaN(n)) return def;
return n;
}
export async function parse_big_int<T extends number | bigint | null>(
value: FormDataEntryValue | null,
def: T,
): Promise<number | bigint | T> {
if (value === null) return def;
const v = typeof value === "string" ? value : await value.text();
const n = parseBigInt(v);
if (isNumNaN(n)) return def;
return n;
}

View File

@@ -1,5 +1,5 @@
import { assertEquals } from "@std/assert";
import { parse_bool, parse_int } from "./parse_form.ts";
import { parse_big_int, parse_bool, parse_int } from "./parse_form.ts";
Deno.test("parse_bool_test", async () => {
const f = new FormData();
@@ -27,3 +27,17 @@ Deno.test("parse_int_test", async () => {
f.append("d", "-1");
assertEquals(await parse_int(f.get("d"), null), -1);
});
Deno.test("parse_big_int_test", async () => {
const f = new FormData();
f.append("a", "d");
assertEquals(await parse_big_int(f.get("a"), null), null);
assertEquals(await parse_big_int(f.get("a"), 1), 1);
f.append("c", "1");
assertEquals(await parse_big_int(f.get("c"), null), 1);
assertEquals(await parse_big_int(f.get("c"), 2), 1);
f.append("d", "-1");
assertEquals(await parse_big_int(f.get("d"), null), -1);
f.append("b", "3152921504606847000");
assertEquals(await parse_big_int(f.get("b"), null), 3152921504606847000n);
});

View File

@@ -10,14 +10,18 @@ export type TaskServerSocketData =
| {
type: "tasks";
tasks: Task[];
running: number[];
running: (number | bigint)[];
}
| { type: "ping" }
| { type: "pong" };
type EventMap = {
new_download_task: { gid: number; token: string; cfg?: DownloadConfig };
new_export_zip_task: { gid: number; cfg?: ExportZipConfig };
new_download_task: {
gid: number | bigint;
token: string;
cfg?: DownloadConfig;
};
new_export_zip_task: { gid: number | bigint; cfg?: ExportZipConfig };
};
export type TaskClientSocketData =

View File

@@ -1,7 +1,7 @@
import type { UserPermission } from "../db.ts";
export type BUser = {
id: number;
id: number | bigint;
username: string;
is_admin: boolean;
permissions: UserPermission;