-
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
7 changed files
with
209 additions
and
58 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,71 @@ | ||
import { DOMParser, Element } from "deno_dom/deno-dom-wasm-noinit.ts"; | ||
import { initDOMParser } from "../utils.ts"; | ||
|
||
export class HomeOverviewPage { | ||
dom; | ||
doc; | ||
#panel: Map<string, Element> | undefined; | ||
#current_image_limit: number | undefined; | ||
#max_image_limit: number | undefined; | ||
constructor(html: string) { | ||
const dom = (new DOMParser()).parseFromString(html, "text/html"); | ||
if (!dom) { | ||
throw Error("Failed to parse HTML document."); | ||
} | ||
this.dom = dom; | ||
const doc = this.dom.documentElement; | ||
if (!doc) { | ||
throw Error("HTML document don't have a document element."); | ||
} | ||
this.doc = doc; | ||
} | ||
get panel() { | ||
if (this.#panel === undefined) { | ||
const panel = new Map<string, Element>(); | ||
const keys = this.doc.querySelectorAll(".stuffbox > h2"); | ||
for (const key of keys) { | ||
const k = key as Element; | ||
const v = k.nextElementSibling; | ||
if (v) { | ||
panel.set(k.innerText, v as Element); | ||
} | ||
} | ||
this.#panel = panel; | ||
return panel; | ||
} else return this.#panel; | ||
} | ||
get current_image_limit() { | ||
if (this.#current_image_limit === undefined) { | ||
const panel = this.panel.get("Image Limits"); | ||
if (!panel) { | ||
throw Error('Failed to find panel "Image Limits".'); | ||
} | ||
const limits = panel.querySelectorAll("p:nth-child(1) > strong"); | ||
if (!limits.length) { | ||
throw Error("Failed to find limits."); | ||
} | ||
const current = limits[0] as Element; | ||
const max = limits[1] as Element; | ||
const current_image_limit = parseInt(current.innerText); | ||
const max_image_limit = parseInt(max.innerText); | ||
if (isNaN(current_image_limit) || isNaN(max_image_limit)) { | ||
throw Error("Failed to parse limits."); | ||
} | ||
this.#current_image_limit = current_image_limit; | ||
this.#max_image_limit = max_image_limit; | ||
return current_image_limit; | ||
} else return this.#current_image_limit; | ||
} | ||
get max_image_limit(): number { | ||
if (this.#max_image_limit === undefined) { | ||
this.current_image_limit; | ||
// @ts-ignore Already assigned in current_image_limit. | ||
return this.#max_image_limit; | ||
} else return this.#max_image_limit; | ||
} | ||
} | ||
|
||
export async function load_home_overview_page(html: string) { | ||
await initDOMParser(); | ||
return new HomeOverviewPage(html); | ||
} |
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,24 @@ | ||
import { assert } from "std/assert/mod.ts"; | ||
import { Client } from "../client.ts"; | ||
import { load_settings } from "../config.ts"; | ||
import { API_PERMISSION } from "../test_base.ts"; | ||
|
||
const MAX_IMAGE_LIMITS = [5000, 10000, 25000, 50000]; | ||
|
||
Deno.test({ | ||
name: "HomeOverviewPage_test", | ||
permissions: API_PERMISSION, | ||
}, async () => { | ||
const cfg = await load_settings("./config.json"); | ||
const client = new Client(cfg); | ||
const re = await client.fetchHomeOverviewPage(); | ||
if (re === null) return; | ||
console.log( | ||
"Image limit:", | ||
re.current_image_limit, | ||
"/", | ||
re.max_image_limit, | ||
); | ||
assert(re.current_image_limit <= re.max_image_limit); | ||
assert(MAX_IMAGE_LIMITS.includes(re.max_image_limit)); | ||
}); |
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,28 @@ | ||
import { Handlers } from "$fresh/server.ts"; | ||
import { User } from "../../../db.ts"; | ||
import { get_task_manager } from "../../../server.ts"; | ||
import { EHImageLimit } from "../../../server/eh.ts"; | ||
import { 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) { | ||
return return_error(403, "Permission denied."); | ||
} | ||
const m = get_task_manager(); | ||
try { | ||
const re = await m.client.fetchHomeOverviewPage(); | ||
if (re === null) { | ||
return return_error(1, "Not logged in."); | ||
} | ||
|
||
return return_data<EHImageLimit>({ | ||
max: re.max_image_limit, | ||
current: re.current_image_limit, | ||
}); | ||
} catch (e) { | ||
return return_error(500, e.message); | ||
} | ||
}, | ||
}; |
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,4 @@ | ||
export type EHImageLimit = { | ||
current: number; | ||
max: number; | ||
}; |