-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { Handlers } from "$fresh/server.ts"; | ||
import type { GID } from "../../../client.ts"; | ||
import { User, UserPermission } from "../../../db.ts"; | ||
import { get_task_manager } from "../../../server.ts"; | ||
import { EHMetaInfo } from "../../../server/eh.ts"; | ||
import { parse_int } from "../../../server/parse_form.ts"; | ||
import { | ||
gen_data, | ||
gen_error, | ||
return_data, | ||
return_error, | ||
} from "../../../server/utils.ts"; | ||
|
||
export const handler: Handlers = { | ||
async GET(req, ctx) { | ||
const user = <User | undefined> ctx.state.user; | ||
if ( | ||
user && !user.is_admin && | ||
!(user.permissions & UserPermission.ManageTasks) | ||
) { | ||
return return_error(403, "Permission denied."); | ||
} | ||
const m = get_task_manager(); | ||
const url = new URL(req.url); | ||
const gids: Array<number> = []; | ||
for (const gid of url.searchParams.getAll("gid")) { | ||
const i = await parse_int(gid, null); | ||
if (i === null) { | ||
return return_error(1, `Invalid gid: ${gid}`); | ||
} | ||
gids.push(i); | ||
} | ||
for (const gid of url.searchParams.getAll("gid[]")) { | ||
const i = await parse_int(gid, null); | ||
if (i === null) { | ||
return return_error(1, `Invalid gid: ${gid}`); | ||
} | ||
gids.push(i); | ||
} | ||
const tokens = url.searchParams.getAll("token").concat( | ||
url.searchParams.getAll("token[]"), | ||
); | ||
if (gids.length === 0 && tokens.length === 0) { | ||
return return_error(2, "No gids and tokens provided."); | ||
} | ||
if (gids.length !== tokens.length) { | ||
return return_error(3, "Length of gids and tokens do not match."); | ||
} | ||
const data: EHMetaInfo = {}; | ||
const needed: GID[] = []; | ||
for (let i = 0; i < gids.length; i++) { | ||
const gid = gids[i]; | ||
const token = tokens[i]; | ||
const cache = m.db.get_ehmeta(gid); | ||
if (cache && cache.gid === gid && cache.token === token) { | ||
data[gid] = gen_data(cache); | ||
} else if (cache && cache.gid === gid) { | ||
data[gid] = gen_error(1, "Token not matched."); | ||
} else { | ||
needed.push([gid, token]); | ||
} | ||
} | ||
while (needed.length > 0) { | ||
const query = needed.splice(0, 25); | ||
try { | ||
const metas = await m.client.fetchGalleryMetadataByAPI( | ||
...query, | ||
); | ||
for (const [k, v] of metas.map) { | ||
if (typeof v === "string") { | ||
data[k] = gen_error(2, v); | ||
} else { | ||
data[k] = gen_data(v); | ||
m.db.add_ehmeta(v); | ||
} | ||
} | ||
} catch (e) { | ||
return return_error( | ||
4, | ||
`Failed to fetch metadata: ${e.message}`, | ||
); | ||
} | ||
} | ||
return return_data(data); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
import type { JSONResult } from "./utils.ts"; | ||
import type { GalleryMetadataSingle } from "../page/GalleryMetadata.ts"; | ||
|
||
export type EHImageLimit = { | ||
current: number; | ||
max: number; | ||
}; | ||
|
||
export type EHMetaInfo = Record<string, JSONResult<GalleryMetadataSingle>>; |