Config sharp image preprocessing

This commit is contained in:
Yukai Huang 2016-11-16 17:07:00 +08:00
parent 1f409197c3
commit 2279986f97
2 changed files with 70 additions and 53 deletions

16
app.js
View File

@ -413,10 +413,25 @@ app.post('/uploadimage', function (req, res) {
form.uploadDir = "public/uploads"; form.uploadDir = "public/uploads";
} }
function preprocessImage(path) {
return new Promise((resolve) => {
var oldFile = `${path}-old`;
fs.rename(path, oldFile, function() {
var sharp = require('sharp');
sharp(oldFile).toFile(path).then(() => {
fs.unlink(oldFile, function() {
resolve(path);
})
});
});
});
}
form.parse(req, function (err, fields, files) { form.parse(req, function (err, fields, files) {
if (err || !files.image || !files.image.path) { if (err || !files.image || !files.image.path) {
response.errorForbidden(res); response.errorForbidden(res);
} else { } else {
preprocessImage(files.image.path).then(() => {
if (config.debug) if (config.debug)
logger.info('SERVER received uploadimage: ' + JSON.stringify(files.image)); logger.info('SERVER received uploadimage: ' + JSON.stringify(files.image));
@ -478,6 +493,7 @@ app.post('/uploadimage', function (req, res) {
logger.error(err); logger.error(err);
return res.status(500).end('upload image error'); return res.status(500).end('upload image error');
} }
});
} }
}); });
}); });

View File

@ -87,6 +87,7 @@
"reveal.js": "^3.3.0", "reveal.js": "^3.3.0",
"sequelize": "^3.24.3", "sequelize": "^3.24.3",
"sequelize-cli": "^2.4.0", "sequelize-cli": "^2.4.0",
"sharp": "^0.16.2",
"shortid": "2.2.6", "shortid": "2.2.6",
"socket.io": "1.5.0", "socket.io": "1.5.0",
"socket.io-client": "^1.5.0", "socket.io-client": "^1.5.0",