Add shared_token API

This commit is contained in:
2024-08-11 18:07:11 +08:00
parent 10eeeaf1ac
commit c75d2d9dbb
7 changed files with 229 additions and 10 deletions

View File

@@ -2,7 +2,13 @@ import { Handlers } from "$fresh/server.ts";
import { get_task_manager } from "../../../server.ts";
import type { EhFiles } from "../../../server/files.ts";
import { return_data, return_error } from "../../../server/utils.ts";
import { User, UserPermission } from "../../../db.ts";
import {
SharedToken,
SharedTokenType,
User,
UserPermission,
} from "../../../db.ts";
import { compareNum } from "../../../utils.ts";
export const handler: Handlers = {
GET(_req, ctx) {
@@ -10,6 +16,7 @@ export const handler: Handlers = {
if (u && !u.is_admin && !(u.permissions & UserPermission.ReadGallery)) {
return return_error(403, "Permission denied.");
}
const st = <SharedToken | undefined> ctx.state.shared_token;
const tokens = ctx.params.token.split(",");
const m = get_task_manager();
const enable_server_timing = m.cfg.enable_server_timing;
@@ -17,6 +24,12 @@ export const handler: Handlers = {
const headers: HeadersInit = {};
const data: EhFiles = {};
for (const token of tokens) {
if (st && st.type == SharedTokenType.Gallery) {
const pmetas = m.db.get_pmeta_by_token_only(token);
if (!pmetas.some((m) => !compareNum(m.gid, st.info.gid))) {
return return_error(403, "Permission denied.");
}
}
data[token] = m.db.get_files(token).map((d) => {
/**@ts-ignore */
delete d.path;