import { uploadBulk } from "./upload.ts"; import { useStatusBar } from "jsr:@cosense/std@0.29/browser/dom"; import { upload } from "../scrapbox-file-uploader/mod.ts"; import { isErr, unwrapErr, unwrapOk } from "npm:option-t@50/plain_result"; await (async () => { const URLs = [] as string[]; const errors = [] as number[]; const { render, dispose } = useStatusBar(); const errorBar = useStatusBar(); const infoBar = useStatusBar(); try { const fileList = await upload({ accept: "image/jpeg, image/png", multiple: true }); if (!fileList) return; const compare = new Intl.Collator().compare; const files = Array.from(fileList) .sort((a, b) => compare(a.name, b.name)); let counter = 0; for await (const result of uploadBulk( files, { title: "タイトル", noIndex: true, }, )) { counter++; if (isErr(result)) { if (!(unwrapErr(result) instanceof Error)) throw unwrapErr(result); console.error(unwrapErr(result)); errors.push(counter); errorBar.render( { type: "exclamation-triangle" }, { type: "text", text: `${unwrapErr(result)}`, }, ); continue; } const { permalink_url, name, index } = unwrapOk(result); URLs[index] = permalink_url; infoBar.render( { type: "text", text: `${name} ${permalink_url}` }, ); render( { type: "spinner" }, { type: "text", text: `${files.length} images, ${ counter - errors.length } uploaded, ${errors.length} failed`, }, ); } render( { type: "check-circle" }, { type: "text", text: `Finish uploading.` }, ); const blob = new Blob([JSON.stringify(URLs)], { type: "application/json" }); window.open(URL.createObjectURL(blob)); } finally { console.log(URLs); console.log(errors); setTimeout(() => { errorBar.dispose(); infoBar.dispose(); dispose(); }, 1000); } })();