Wait for image
This commit is contained in:
parent
5a6dad8698
commit
c53f669aad
1 changed files with 20 additions and 1 deletions
21
index.js
21
index.js
|
@ -13,14 +13,33 @@ 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) {
|
||||||
|
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) {
|
if (imgData) {
|
||||||
fs.createReadStream(path.join(cachePath, imgData.file)).pipe(res)
|
fs.createReadStream(path.join(cachePath, imgData.file)).pipe(res)
|
||||||
} else {
|
} else {
|
||||||
fs.createReadStream(path.join(__dirname, "/imgs/notFound.png")).pipe(res)
|
waitForImage()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue