Less code
This commit is contained in:
parent
71862d8254
commit
405e10289f
1 changed files with 23 additions and 35 deletions
58
index.js
58
index.js
|
@ -29,6 +29,26 @@ var app = express()
|
|||
|
||||
const imgWaitMax = 2
|
||||
|
||||
function sendImg(res, imgData, imgURL) {
|
||||
var imgWait = 0
|
||||
function waitForImage() {
|
||||
if (imgWait < imgWaitMax) {
|
||||
imgWait += 0.1
|
||||
imgData = cachedImages.imgs[imgURL]
|
||||
if (imgData) {
|
||||
fs.createReadStream(path.join(imgCache, imgData.file)).pipe(res)
|
||||
} else {
|
||||
setTimeout(() => {
|
||||
waitForImage()
|
||||
}, 100);
|
||||
}
|
||||
} else {
|
||||
fs.createReadStream(path.join(__dirname, "/imgs/notFound.png")).pipe(res)
|
||||
}
|
||||
}
|
||||
waitForImage()
|
||||
}
|
||||
|
||||
app.get("/cached/byID/*", (req, res) => {
|
||||
var imgURL = req.originalUrl
|
||||
imgURL = imgURL.substring(imgURL.indexOf("/", 8) + 1, imgURL.lastIndexOf("?"))
|
||||
|
@ -44,24 +64,8 @@ app.get("/cached/byID/*", (req, res) => {
|
|||
}
|
||||
|
||||
var imgData = cachedImages.imgs[imgURL]
|
||||
|
||||
var imgWait = 0
|
||||
function waitForImage() {
|
||||
if (imgWait < imgWaitMax) {
|
||||
imgWait += 0.1
|
||||
imgData = cachedImages.imgs[imgURL]
|
||||
if (imgData) {
|
||||
fs.createReadStream(path.join(imgCache, imgData.file)).pipe(res)
|
||||
} else {
|
||||
setTimeout(() => {
|
||||
waitForImage()
|
||||
}, 100);
|
||||
}
|
||||
} else {
|
||||
fs.createReadStream(path.join(__dirname, "/imgs/notFound.png")).pipe(res)
|
||||
}
|
||||
}
|
||||
waitForImage()
|
||||
|
||||
sendImg(res, imgData, imgURL)
|
||||
})
|
||||
|
||||
app.get("/cached/byURL/*", (req, res) => {
|
||||
|
@ -69,23 +73,7 @@ app.get("/cached/byURL/*", (req, res) => {
|
|||
imgURL = imgURL.substring(imgURL.indexOf("/", 8) + 1)
|
||||
var imgData = cachedImages.imgs[imgURL]
|
||||
|
||||
var imgWait = 0
|
||||
function waitForImage() {
|
||||
if (imgWait < imgWaitMax) {
|
||||
imgWait += 0.1
|
||||
imgData = cachedImages.imgs[imgURL]
|
||||
if (imgData) {
|
||||
fs.createReadStream(path.join(imgCache, imgData.file)).pipe(res)
|
||||
} else {
|
||||
setTimeout(() => {
|
||||
waitForImage()
|
||||
}, 100);
|
||||
}
|
||||
} else {
|
||||
fs.createReadStream(path.join(__dirname, "/imgs/notFound.png")).pipe(res)
|
||||
}
|
||||
}
|
||||
waitForImage()
|
||||
sendImg(res, imgData, imgURL)
|
||||
})
|
||||
|
||||
app.get("/cached/*", (req, res) => {
|
||||
|
|
Loading…
Reference in a new issue