Wait for image
This commit is contained in:
parent
5a6dad8698
commit
c53f669aad
1 changed files with 20 additions and 1 deletions
19
index.js
19
index.js
|
@ -13,15 +13,34 @@ 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 {
|
||||
waitForImage()
|
||||
}
|
||||
})
|
||||
|
||||
app.listen(PORT, () => {
|
||||
|
|
Loading…
Reference in a new issue