Update img_verify

This commit is contained in:
2023-08-07 19:28:25 +08:00
parent cd86f44cee
commit af5667f337
14 changed files with 310 additions and 90 deletions

View File

@@ -0,0 +1,46 @@
export class SortableURLSearchParams extends URLSearchParams {
excludes;
constructor(
init?: string[][] | Record<string, string> | string | URLSearchParams,
excludes: string[] = [],
) {
super(init);
this.excludes = excludes;
}
entries(): IterableIterator<[string, string]> {
this.sort();
const a: [string, string][] = [];
for (const i of super.entries()) {
if (!this.excludes.includes(i[0])) a.push(i);
}
return a.values();
}
forEach(
callbackfn: (value: string, key: string, parent: this) => void,
thisArg?: unknown,
): void {
for (const [k, v] of this.entries()) {
callbackfn.apply(thisArg, [v, k, this]);
}
}
keys(): IterableIterator<string> {
this.sort();
const a: string[] = [];
for (const i of super.keys()) {
if (!this.excludes.includes(i)) a.push(i);
}
return a.values();
}
toString(): string {
return Array.from(this.entries()).map((v) =>
`${encodeURIComponent(v[0])}=${encodeURIComponent(v[1])}`
).join("&");
}
toString2(): string {
const s = this.toString();
return s.length ? `?${s}` : "";
}
values(): IterableIterator<string> {
return Array.from(this.entries()).map((v) => v[1]).values();
}
}

View File

@@ -0,0 +1,11 @@
import { assertEquals } from "std/assert/mod.ts";
import { SortableURLSearchParams } from "./SortableURLSearchParams.ts";
Deno.test("SortableURLSearchParams_test", () => {
const s = new SortableURLSearchParams(undefined, ["dad"]);
s.append("a", "1");
s.append("d", "3");
s.append("b", "4");
s.append("dad", "4");
assertEquals(s.toString(), "a=1&b=4&d=3");
});

29
server/check_auth.ts Normal file
View File

@@ -0,0 +1,29 @@
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

@@ -5,4 +5,12 @@ export type EhFileBasic = {
is_original: boolean;
};
export type EhFileExtend = {
id: number;
width: number;
height: number;
is_original: boolean;
token: string;
};
export type EhFiles = Record<string, EhFileBasic[]>;