Skip to content

Commit 5958654

Browse files
committed
Remove preprocess image on upload image or it will losing support of image some formats
1 parent 840cac7 commit 5958654

2 files changed

Lines changed: 51 additions & 73 deletions

File tree

app.js

Lines changed: 51 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -462,91 +462,70 @@ app.post('/uploadimage', function (req, res) {
462462
form.uploadDir = "public/uploads";
463463
}
464464

465-
function preprocessImage(path) {
466-
return new Promise((resolve) => {
467-
var oldFile = `${path}-old`;
468-
fs.rename(path, oldFile, function() {
469-
var sharp = require('sharp');
470-
sharp(oldFile).toFile(path).then(() => {
471-
fs.unlink(oldFile, function() {
472-
resolve(path);
473-
})
474-
});
475-
});
476-
});
477-
}
478-
479465
form.parse(req, function (err, fields, files) {
480466
if (err || !files.image || !files.image.path) {
481467
response.errorForbidden(res);
482468
} else {
483-
preprocessImage(files.image.path).then(() => {
484-
if (config.debug)
485-
logger.info('SERVER received uploadimage: ' + JSON.stringify(files.image));
486-
487-
var path = require('path');
488-
try {
489-
switch (config.imageUploadType) {
490-
case 'filesystem':
491-
res.send({
492-
link: path.join(config.serverurl, files.image.path.match(/^public(.+$)/)[1])
493-
});
469+
if (config.debug)
470+
logger.info('SERVER received uploadimage: ' + JSON.stringify(files.image));
471+
472+
try {
473+
switch (config.imageUploadType) {
474+
case 'filesystem':
475+
res.send({
476+
link: path.join(config.serverurl, files.image.path.match(/^public(.+$)/)[1])
477+
});
494478

495-
break;
496-
497-
case 's3':
498-
var AWS = require('aws-sdk');
499-
var awsConfig = new AWS.Config(config.s3);
500-
var s3 = new AWS.S3(awsConfig);
501-
502-
fs.readFile(files.image.path, function (err, buffer) {
503-
var params = {
504-
Bucket: 'hackmd',
505-
Key: path.join('uploads', path.basename(files.image.path)),
506-
Body: buffer
507-
};
508-
509-
s3.putObject(params, function (err, data) {
510-
if (err) {
511-
logger.error(err);
512-
res.status(500).end('upload image error');
513-
} else {
514-
res.send({
515-
link: `https://s3-${config.s3.region}.amazonaws.com/${config.s3bucket}/${params.Key}`
516-
});
517-
}
518-
});
479+
break;
519480

520-
});
481+
case 's3':
482+
var AWS = require('aws-sdk');
483+
var awsConfig = new AWS.Config(config.s3);
484+
var s3 = new AWS.S3(awsConfig);
521485

522-
break;
486+
fs.readFile(files.image.path, function (err, buffer) {
487+
var params = {
488+
Bucket: 'hackmd',
489+
Key: path.join('uploads', path.basename(files.image.path)),
490+
Body: buffer
491+
};
523492

524-
case 'imgur':
525-
default:
526-
imgur.setClientId(config.imgur.clientID);
527-
imgur.uploadFile(files.image.path)
528-
.then(function (json) {
529-
if (config.debug)
530-
logger.info('SERVER uploadimage success: ' + JSON.stringify(json));
493+
s3.putObject(params, function (err, data) {
494+
if (err) {
495+
logger.error(err);
496+
res.status(500).end('upload image error');
497+
} else {
531498
res.send({
532-
link: json.data.link.replace(/^http:\/\//i, 'https://')
499+
link: `https://s3-${config.s3.region}.amazonaws.com/${config.s3bucket}/${params.Key}`
533500
});
534-
})
535-
.catch(function (err) {
536-
logger.error(err);
537-
return res.status(500).end('upload image error');
501+
}
502+
});
503+
504+
});
505+
506+
break;
507+
508+
case 'imgur':
509+
default:
510+
imgur.setClientId(config.imgur.clientID);
511+
imgur.uploadFile(files.image.path)
512+
.then(function (json) {
513+
if (config.debug)
514+
logger.info('SERVER uploadimage success: ' + JSON.stringify(json));
515+
res.send({
516+
link: json.data.link.replace(/^http:\/\//i, 'https://')
538517
});
539-
break;
540-
}
541-
} catch (err) {
542-
logger.error(err);
543-
return res.status(500).end('upload image error');
518+
})
519+
.catch(function (err) {
520+
logger.error(err);
521+
return res.status(500).end('upload image error');
522+
});
523+
break;
544524
}
545-
546-
}).catch((err) => {
525+
} catch (err) {
547526
logger.error(err);
548-
return res.status(500).end('process image error');
549-
});
527+
return res.status(500).end('upload image error');
528+
}
550529
}
551530
});
552531
});

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@
101101
"scrypt": "^6.0.3",
102102
"select2": "^3.5.2-browserify",
103103
"sequelize-cli": "^2.4.0",
104-
"sharp": "^0.16.2",
105104
"shortid": "2.2.6",
106105
"socket.io": "~1.6.0",
107106
"socket.io-client": "~1.6.0",

0 commit comments

Comments
 (0)