import { upload, UploadResult } from "../deno-gyazo/mod.ts"; import { getGyazoToken } from "../scrapbox-userscript-std/rest.ts"; export type Result = { success: false; reason: unknown; } | { success: true; value: T; }; export interface UploadOptions { title?: string; refererURL?: string; app?: string; noIndex?: boolean; accessToken?: string; } export async function* uploadBulk( images: File[], uploadOptions?: UploadOptions, ): AsyncGenerator, void, unknown> { const { noIndex, accessToken: token, ...options } = uploadOptions ?? {}; // access token取得 const accessToken = token ?? await (async () => { const result = await getGyazoToken(); if (!result.ok) throw new Error(JSON.stringify(result.value)); const accessToken = result.value; if (!accessToken) throw new Error("Could not get the access token"); return accessToken; })(); const zero = (n: number) => `${n}`.padStart(`${images.length}`.length, "0"); // 一枚ずつuploadして返す for (let index = 0; index < images.length; index++) { const result = await upload(images[index], { accessToken, description: `${images[index].name}${noIndex === true ? "" :` ${zero(index + 1)}`}`, ...options, }); if (!result.ok) { yield { success: false, reason: result.value }; continue; } yield { success: true, value: { index, name: images[index].name, ...result.value } }; } }