From c53f669aaded7e740b0e7479efc959bf323c282c Mon Sep 17 00:00:00 2001 From: bingus_violet Date: Mon, 3 Jun 2024 00:04:34 -0500 Subject: [PATCH] Wait for image --- index.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index f99a6b1..550fe88 100644 --- a/index.js +++ b/index.js @@ -13,14 +13,33 @@ if (!fs.existsSync(cacheFile)) { fs.writeFileSync(cacheFile, "{}") } var app = express() +const imgWaitMax = 15 + app.get("/cached/*", (req, res) => { var imgURL = req.originalUrl imgURL = imgURL.substring(imgURL.indexOf("/", 2) + 1) var imgData = cachedImages[imgURL] + + var imgWait = 0 + function waitForImage() { + if (imgWait < imgWaitMax) { + imgWait += 0.5 + setTimeout(() => { + imgData = cachedImages[imgURL] + if (imgData) { + fs.createReadStream(path.join(cachePath, imgData.file)).pipe(res) + } else { + waitForImage() + } + }, 500); + } else { + fs.createReadStream(path.join(__dirname, "/imgs/notFound.png")).pipe(res) + } + } if (imgData) { fs.createReadStream(path.join(cachePath, imgData.file)).pipe(res) } else { - fs.createReadStream(path.join(__dirname, "/imgs/notFound.png")).pipe(res) + waitForImage() } })