Start adding image cache logic

This commit is contained in:
Hamcha 2019-09-08 20:24:23 +02:00
parent 3f975e0d10
commit df70f34e7a
Signed by: hamcha
GPG key ID: 44AD3571EB09A39E
6 changed files with 39 additions and 10 deletions

View file

@ -1,4 +1,5 @@
import Vue from "vue"; import Vue from "vue";
import "./plugins/axios";
import App from "./App.vue"; import App from "./App.vue";
import router from "./router"; import router from "./router";
import store from "./store"; import store from "./store";

View file

@ -1,11 +1,5 @@
import { Card } from "./types"; import { Card } from "./types";
const imgBaseURL = "https://mcg.zyg.ovh/images/cards/";
export function cardImageURL(cardid: string): string {
return `${imgBaseURL}${cardid}.webp`;
}
export function cardFullName(card: Card): string { export function cardFullName(card: Card): string {
if (card.Subname != "") { if (card.Subname != "") {
return `${card.Name}, ${card.Subname}`; return `${card.Name}, ${card.Subname}`;

View file

@ -4,6 +4,7 @@ import { cardFullName } from "./card";
class CardDatabase extends Dexie { class CardDatabase extends Dexie {
public cards: Dexie.Table<Card, string>; public cards: Dexie.Table<Card, string>;
public images: Dexie.Table<Blob, string>;
public constructor() { public constructor() {
super("CardDatabase"); super("CardDatabase");
@ -11,6 +12,7 @@ class CardDatabase extends Dexie {
cards: "ID,Set,Type,Cost,Power" cards: "ID,Set,Type,Cost,Power"
}); });
this.cards = this.table("cards"); this.cards = this.table("cards");
this.images = this.table("images");
} }
} }

31
src/mlpccg/images.ts Normal file
View file

@ -0,0 +1,31 @@
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() {
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(req.data, img);
});
return await Promise.all(promises);
}

View file

@ -2,3 +2,4 @@ export * from "./card";
export * from "./database"; export * from "./database";
export * from "./set"; export * from "./set";
export * from "./types"; export * from "./types";
export * from "./images";

View file

@ -1,5 +1,6 @@
import { SetFile } from "./types"; import { SetFile } from "./types";
import { Database } from "./database"; import { Database } from "./database";
import axios from "axios";
const baseURL = "https://mcg.zyg.ovh/setdata/"; const baseURL = "https://mcg.zyg.ovh/setdata/";
export const allSets = [ export const allSets = [
@ -34,11 +35,10 @@ export async function loadSets() {
} }
async function downloadSet(setid: string): Promise<SetFile> { async function downloadSet(setid: string): Promise<SetFile> {
const setfile = await fetch(`${baseURL}${setid.toLowerCase()}.json`); const setdata = await axios(`${baseURL}${setid.toLowerCase()}.json`);
const setdata: SetFile = await setfile.json(); setdata.data.Cards = (setdata.data as SetFile).Cards.map(c => {
setdata.Cards = setdata.Cards.map(c => {
c.Set = setid; c.Set = setid;
return c; return c;
}); });
return setdata; return setdata.data;
} }