Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 1,352 Bytes
44fe180 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import { promises as fs } from "node:fs"
import path from "node:path"
import { RenderRequest, RenderedScene } from "../types.mts"
import { renderedDirFilePath } from "../config.mts"
import { computeSha256 } from "./computeSha256.mts"
export async function loadRenderedSceneFromCache(request?: RenderRequest, id?: string): Promise<RenderedScene> {
let pattern = ""
if (request?.prompt) {
try {
const requestJson = JSON.stringify(request)
const hash = computeSha256(requestJson)
pattern = `hash_${hash}`
} catch (err) {
}
} else if (id) {
pattern = `id_${id}`
}
if (!pattern) {
throw new Error("invalid request or id")
}
for (const cachedFile of await fs.readdir(renderedDirFilePath)) {
if (cachedFile.includes(pattern)) {
const cacheFilePath = path.join(renderedDirFilePath, cachedFile)
const scene = JSON.parse(
await fs.readFile(cacheFilePath, 'utf8')
) as RenderedScene
if (!scene.assetUrl) {
throw new Error("there is something wrong with the cached rendered scene (url is empty)")
}
if (!scene.assetUrl) {
throw new Error("there is something wrong with the cached rendered scene (statis is not completed)")
}
return scene
}
}
throw new Error("couldn't find a cached scene with id entry")
} |