36 lines
940 B
TypeScript
36 lines
940 B
TypeScript
|
import axios from "axios";
|
||
|
import { Database } from "./database";
|
||
|
|
||
|
const imgBaseURL = "https://mcg.zyg.ovh/images/cards/";
|
||
|
|
||
|
export function cardImageURL(cardid: string): string {
|
||
|
return `${imgBaseURL}${cardid}.webp`;
|
||
|
}
|
||
|
|
||
|
async function getCardImageList(): Promise<string[]> {
|
||
|
const req = await axios(`${imgBaseURL}list.txt`);
|
||
|
return req.data;
|
||
|
}
|
||
|
|
||
|
export async function getImages() {
|
||
|
if (Database == null) {
|
||
|
throw new Error("Database was not initialized, init with 'initDB()'");
|
||
|
}
|
||
|
const itemcount = await Database.images.count();
|
||
|
if (itemcount > 100) {
|
||
|
// DB already filled, exit early
|
||
|
return;
|
||
|
}
|
||
|
const imglist = await getCardImageList();
|
||
|
|
||
|
let table = Database.images;
|
||
|
const promises = imglist.map(async img => {
|
||
|
const req = await axios({
|
||
|
url: `${imgBaseURL}${img}`,
|
||
|
responseType: "blob"
|
||
|
});
|
||
|
return table.put({ id: img, image: req.data });
|
||
|
});
|
||
|
return await Promise.all(promises);
|
||
|
}
|