display cart images
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { Octokit } from "octokit";
|
||||
import { decrypt } from "./crypt";
|
||||
import { authors } from "../../data/authors";
|
||||
|
||||
type Game = { carts: { name: string; src: string }[] };
|
||||
|
||||
const gameCache: Record<string, Game> = {};
|
||||
|
||||
export const getGame = async (
|
||||
author: string,
|
||||
name: string,
|
||||
): Promise<Game | null> => {
|
||||
const authorData = authors.find((x) => x.username === author);
|
||||
|
||||
if (!authorData) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const pat = decrypt({
|
||||
password: process.env.ENCRYPTION_PASSWORD!,
|
||||
cyphertext: authorData.auth.pat,
|
||||
});
|
||||
|
||||
const octokit = new Octokit({
|
||||
auth: pat,
|
||||
});
|
||||
|
||||
const gameData = (
|
||||
await octokit.rest.repos.getContent({
|
||||
owner: authorData.username,
|
||||
repo: authorData.repo,
|
||||
path: `.picobook/${name}.p8.png`,
|
||||
})
|
||||
).data;
|
||||
|
||||
if (Array.isArray(gameData)) {
|
||||
return null;
|
||||
}
|
||||
if (gameData.type !== "file") {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!(gameData.sha in gameCache)) {
|
||||
gameCache[gameData.sha] = {
|
||||
carts: [{ name, src: `data:image/png;base64,${gameData.content}` }],
|
||||
};
|
||||
}
|
||||
return gameCache[gameData.sha];
|
||||
};
|
||||
|
||||
export const getGameFromSha = async (sha: string): Promise<Game | null> => {
|
||||
if (gameCache[sha]) {
|
||||
console.log("cache hit");
|
||||
}
|
||||
return gameCache[sha] ?? null;
|
||||
};
|
||||
@@ -0,0 +1 @@
|
||||
export type Game = { carts: { name: string; src: string }[] };
|
||||
Reference in New Issue
Block a user