Wait for image

This commit is contained in:
bingus_violet 2024-06-03 00:04:34 -05:00
parent 5a6dad8698
commit c53f669aad

View file

@ -13,15 +13,34 @@ if (!fs.existsSync(cacheFile)) { fs.writeFileSync(cacheFile, "{}") }
var app = express() var app = express()
const imgWaitMax = 15
app.get("/cached/*", (req, res) => { app.get("/cached/*", (req, res) => {
var imgURL = req.originalUrl var imgURL = req.originalUrl
imgURL = imgURL.substring(imgURL.indexOf("/", 2) + 1) imgURL = imgURL.substring(imgURL.indexOf("/", 2) + 1)
var imgData = cachedImages[imgURL] var imgData = cachedImages[imgURL]
var imgWait = 0
function waitForImage() {
if (imgWait < imgWaitMax) {
imgWait += 0.5
setTimeout(() => {
imgData = cachedImages[imgURL]
if (imgData) { if (imgData) {
fs.createReadStream(path.join(cachePath, imgData.file)).pipe(res) fs.createReadStream(path.join(cachePath, imgData.file)).pipe(res)
} else {
waitForImage()
}
}, 500);
} else { } else {
fs.createReadStream(path.join(__dirname, "/imgs/notFound.png")).pipe(res) fs.createReadStream(path.join(__dirname, "/imgs/notFound.png")).pipe(res)
} }
}
if (imgData) {
fs.createReadStream(path.join(cachePath, imgData.file)).pipe(res)
} else {
waitForImage()
}
}) })
app.listen(PORT, () => { app.listen(PORT, () => {