Fixed unhandled promise rejection #254 (and some cleanup)
This commit is contained in:
parent
0629194f56
commit
1d76eefe27
1 changed files with 38 additions and 24 deletions
|
@ -1,5 +1,6 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
let log = require('npmlog');
|
||||||
let config = require('config');
|
let config = require('config');
|
||||||
let express = require('express');
|
let express = require('express');
|
||||||
let router = new express.Router();
|
let router = new express.Router();
|
||||||
|
@ -70,19 +71,15 @@ let listImages = (dir, dirURL, callback) => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
let getStaticImageUrl = ({
|
const getStaticImageUrl = (dynamicUrl, staticDir, staticDirUrl, callback) => {
|
||||||
dynamicUrl,
|
|
||||||
staticDir,
|
|
||||||
staticDirUrl
|
|
||||||
}, callback) => {
|
|
||||||
mkdirp(staticDir, err => {
|
mkdirp(staticDir, err => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(dynamicUrl);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.readdir(staticDir, (err, files) => {
|
fs.readdir(staticDir, (err, files) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(dynamicUrl);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
let hash = crypto.createHash('md5').update(dynamicUrl).digest('hex');
|
let hash = crypto.createHash('md5').update(dynamicUrl).digest('hex');
|
||||||
|
@ -90,7 +87,7 @@ let getStaticImageUrl = ({
|
||||||
let headers = {};
|
let headers = {};
|
||||||
|
|
||||||
if (match) {
|
if (match) {
|
||||||
return callback(staticDirUrl + '/' + match);
|
return callback(null, staticDirUrl + '/' + match);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dynamicUrl.includes('/editorapi/img?')) {
|
if (dynamicUrl.includes('/editorapi/img?')) {
|
||||||
|
@ -103,24 +100,27 @@ let getStaticImageUrl = ({
|
||||||
headers
|
headers
|
||||||
})
|
})
|
||||||
.then(res => {
|
.then(res => {
|
||||||
|
if (res.status < 200 || res.status >= 300) {
|
||||||
|
throw new Error(`Received HTTP status code ${res.status} while fetching image ${dynamicUrl}`);
|
||||||
|
}
|
||||||
return res.buffer();
|
return res.buffer();
|
||||||
})
|
})
|
||||||
.then(buffer => {
|
.then(buffer => {
|
||||||
let ft = fileType(buffer);
|
let ft = fileType(buffer);
|
||||||
if (!ft) {
|
if (ft && ['image/jpeg', 'image/png', 'image/gif'].includes(ft.mime)) {
|
||||||
return callback(dynamicUrl);
|
|
||||||
}
|
|
||||||
if (['image/jpeg', 'image/png', 'image/gif'].includes(ft.mime)) {
|
|
||||||
fs.writeFile(path.join(staticDir, hash + '.' + ft.ext), buffer, err => {
|
fs.writeFile(path.join(staticDir, hash + '.' + ft.ext), buffer, err => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(dynamicUrl);
|
throw err;
|
||||||
}
|
}
|
||||||
let staticUrl = staticDirUrl + '/' + hash + '.' + ft.ext;
|
let staticUrl = staticDirUrl + '/' + hash + '.' + ft.ext;
|
||||||
callback(staticUrl);
|
callback(null, staticUrl);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
callback(dynamicUrl);
|
throw new Error(`Unsupported image MIME type for ${dynamicUrl}`);
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
callback(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -139,6 +139,7 @@ let prepareHtml = ({
|
||||||
let srcs = {};
|
let srcs = {};
|
||||||
let re = /<img[^>]+src="([^"]+)"/g;
|
let re = /<img[^>]+src="([^"]+)"/g;
|
||||||
let result;
|
let result;
|
||||||
|
|
||||||
while ((result = re.exec(html)) !== null) {
|
while ((result = re.exec(html)) !== null) {
|
||||||
srcs[result[1]] = result[1];
|
srcs[result[1]] = result[1];
|
||||||
}
|
}
|
||||||
|
@ -153,15 +154,26 @@ let prepareHtml = ({
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const staticDir = path.join(__dirname, '..', 'public', editorName, 'uploads', 'static');
|
||||||
|
const staticDirUrl = url.resolve(serviceUrl, editorName + '/uploads/static');
|
||||||
|
|
||||||
Object.keys(srcs).forEach(src => {
|
Object.keys(srcs).forEach(src => {
|
||||||
jobs++;
|
jobs++;
|
||||||
let dynamicUrl = src.replace(/&/g, '&');
|
let dynamicUrl = src.replace(/&/g, '&');
|
||||||
dynamicUrl = /^https?:\/\/|^\/\//i.test(dynamicUrl) ? dynamicUrl : url.resolve(serviceUrl, dynamicUrl);
|
dynamicUrl = /^https?:\/\/|^\/\//i.test(dynamicUrl) ? dynamicUrl : url.resolve(serviceUrl, dynamicUrl);
|
||||||
getStaticImageUrl({
|
|
||||||
dynamicUrl,
|
getStaticImageUrl(dynamicUrl, staticDir, staticDirUrl, (err, staticUrl) => {
|
||||||
staticDir: path.join(__dirname, '..', 'public', editorName, 'uploads', 'static'),
|
if (err) {
|
||||||
staticDirUrl: url.resolve(serviceUrl, editorName + '/uploads/static'),
|
// TODO: Send a warning back to the editor. For now we just skip image resizing.
|
||||||
}, staticUrl => {
|
log.error('editorapi', err.message || err);
|
||||||
|
|
||||||
|
if (dynamicUrl.includes('/editorapi/img?')) {
|
||||||
|
staticUrl = url.parse(dynamicUrl, true).query.src || dynamicUrl;
|
||||||
|
} else {
|
||||||
|
staticUrl = dynamicUrl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
srcs[src] = staticUrl;
|
srcs[src] = staticUrl;
|
||||||
jobs--;
|
jobs--;
|
||||||
done();
|
done();
|
||||||
|
@ -329,10 +341,12 @@ router.get('/upload', passport.csrfProtection, (req, res) => {
|
||||||
|
|
||||||
if (req.query.type === 'campaign' && Number(req.query.id) > 0) {
|
if (req.query.type === 'campaign' && Number(req.query.id) > 0) {
|
||||||
listImages(path.join(baseDir, req.query.id), baseDirUrl + '/' + req.query.id, (err, campaignImages) => {
|
listImages(path.join(baseDir, req.query.id), baseDirUrl + '/' + req.query.id, (err, campaignImages) => {
|
||||||
err ? res.status(500).send(err.message || err) :
|
if (err) {
|
||||||
res.json({
|
return res.status(500).send(err.message || err);
|
||||||
files: sharedImages.concat(campaignImages)
|
}
|
||||||
});
|
res.json({
|
||||||
|
files: sharedImages.concat(campaignImages)
|
||||||
|
});
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
res.json({
|
res.json({
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue