23 lines
1.1 KiB
TypeScript
23 lines
1.1 KiB
TypeScript
import { HostError, UPLOAD_TIMEOUT_MS, fileFor, type ImageHost, type LitterboxTime } from "./host";
|
|
|
|
export const LITTERBOX_API = "https://litterbox.catbox.moe/resources/internals/api.php";
|
|
const LABEL: Readonly<Record<LitterboxTime, string>> = { "1h": "1 hour", "12h": "12 hours", "24h": "24 hours", "72h": "72 hours" };
|
|
|
|
/** Temporary (≤ 72 h); CORS is open, so the page uploads straight from the browser. */
|
|
export const litterbox: ImageHost = {
|
|
id: "litterbox",
|
|
lifetime: (opts) => LABEL[opts.litterboxTime ?? "72h"],
|
|
async upload(image, ctx) {
|
|
const form = new FormData();
|
|
form.set("reqtype", "fileupload");
|
|
form.set("time", ctx.options.litterboxTime ?? "72h");
|
|
form.set("fileToUpload", fileFor(image), `${image.sha256}.${image.ext}`);
|
|
const res = await ctx.transport.fetch(LITTERBOX_API, { method: "POST", body: form, timeoutMs: UPLOAD_TIMEOUT_MS });
|
|
const body = (await res.text()).trim();
|
|
if (!res.ok || !/^https:\/\/litter\.catbox\.moe\/\S+$/.test(body)) {
|
|
throw new HostError(`litterbox: ${body.slice(0, 200) || `HTTP ${res.status}`}`, res.status);
|
|
}
|
|
return { url: body };
|
|
},
|
|
};
|