Skip to content

Commit

Permalink
Add new API
Browse files Browse the repository at this point in the history
  • Loading branch information
lifegpc committed May 30, 2024
1 parent a694eea commit 4991b87
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions fresh.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import * as $api_filemeta_token_ from "./routes/api/filemeta/[token].ts";
import * as $api_files_token_ from "./routes/api/files/[token].ts";
import * as $api_gallery_gid_ from "./routes/api/gallery/[gid].ts";
import * as $api_gallery_list from "./routes/api/gallery/list.ts";
import * as $api_gallery_meta_gids_ from "./routes/api/gallery/meta/[gids].ts";
import * as $api_health_check from "./routes/api/health_check.ts";
import * as $api_status from "./routes/api/status.ts";
import * as $api_tag_id_ from "./routes/api/tag/[id].ts";
Expand Down Expand Up @@ -62,6 +63,7 @@ const manifest = {
"./routes/api/files/[token].ts": $api_files_token_,
"./routes/api/gallery/[gid].ts": $api_gallery_gid_,
"./routes/api/gallery/list.ts": $api_gallery_list,
"./routes/api/gallery/meta/[gids].ts": $api_gallery_meta_gids_,
"./routes/api/health_check.ts": $api_health_check,
"./routes/api/status.ts": $api_status,
"./routes/api/tag/[id].ts": $api_tag_id_,
Expand Down
39 changes: 39 additions & 0 deletions routes/api/gallery/meta/[gids].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Handlers } from "$fresh/server.ts";
import { get_task_manager } from "../../../../server.ts";
import {
gen_data,
gen_error,
JSONResult,
return_data,
return_error,
} from "../../../../server/utils.ts";
import { GMeta, User, UserPermission } from "../../../../db.ts";

export const handler: Handlers = {
GET(_req, ctx) {
const u = <User | undefined> ctx.state.user;
if (
u && !u.is_admin &&
!(u.permissions & UserPermission.ReadGallery ||
u.permissions & UserPermission.ManageTasks)
) {
return return_error(403, "Permission denied.");
}
const gids = new Set(
ctx.params.gids.split(",").map((v) => parseInt(v)).filter((v) =>
!isNaN(v)
),
);
const m = get_task_manager();
const re: Record<number, JSONResult<GMeta>> = {};
for (const gid of gids) {
const meta = m.db.get_gmeta_by_gid(gid);
if (meta) {
re[gid] = gen_data(meta);
} else {
re[gid] = gen_error(404, "Not found");
}
}
return return_data(re);
},
};

0 comments on commit 4991b87

Please sign in to comment.