import { Database, initDB } from "@/mlpccg"; import axios from "axios"; import JSZip from "jszip"; import { send, runAsync } from "../worker-utils"; async function downloadImages() { if (!Database) { await initDB(); } let table = Database!.images; const itemcount = await table.count(); if (itemcount > 1900) { // DB already filled, exit early return "already-done"; } const zipdata = await axios({ url: "https://mcg.zyg.ovh/cards.zip", responseType: "blob", onDownloadProgress: (progressEvent: ProgressEvent) => { send("dl-progress", { progress: progressEvent.loaded, total: progressEvent.total }); } }); const zipfile = await JSZip.loadAsync(zipdata.data); const cards = zipfile.folder("Cards"); let loadingState = 0; let totalLoading = 0; cards.forEach(async () => { totalLoading += 2; }); let waitgroup = new Promise(resolve => { let timer = setInterval(() => { if (loadingState >= totalLoading) { clearInterval(timer); resolve(); } }, 1000); }); cards.forEach(async (filename, filedata) => { const data = await filedata.async("blob"); loadingState += 1; send("ex-progress", { progress: loadingState, total: totalLoading }); const result = await table.put({ id: filename, image: data }); loadingState += 1; send("ex-progress", { progress: loadingState, total: totalLoading }); }); await waitgroup; return "downloaded"; } runAsync(async () => { try { return await downloadImages(); } catch (e) { console.error(e); return new Error(e.message); } });