From 5958654ea4d722cb6af6a170da94c0d5373015d2 Mon Sep 17 00:00:00 2001 From: Wu Cheng-Han Date: Sat, 3 Dec 2016 14:37:12 +0800 Subject: Remove preprocess image on upload image or it will losing support of image some formats --- app.js | 123 +++++++++++++++++++++++++---------------------------------- package.json | 1 - 2 files changed, 51 insertions(+), 73 deletions(-) diff --git a/app.js b/app.js index 91d8145c..60b3c41d 100644 --- a/app.js +++ b/app.js @@ -462,91 +462,70 @@ app.post('/uploadimage', function (req, res) { 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) { if (err || !files.image || !files.image.path) { response.errorForbidden(res); } else { - preprocessImage(files.image.path).then(() => { - if (config.debug) - logger.info('SERVER received uploadimage: ' + JSON.stringify(files.image)); - - var path = require('path'); - try { - switch (config.imageUploadType) { - case 'filesystem': - res.send({ - link: path.join(config.serverurl, files.image.path.match(/^public(.+$)/)[1]) - }); + if (config.debug) + logger.info('SERVER received uploadimage: ' + JSON.stringify(files.image)); + + try { + switch (config.imageUploadType) { + case 'filesystem': + res.send({ + link: path.join(config.serverurl, files.image.path.match(/^public(.+$)/)[1]) + }); - break; - - case 's3': - var AWS = require('aws-sdk'); - var awsConfig = new AWS.Config(config.s3); - var s3 = new AWS.S3(awsConfig); - - fs.readFile(files.image.path, function (err, buffer) { - var params = { - Bucket: 'hackmd', - Key: path.join('uploads', path.basename(files.image.path)), - Body: buffer - }; - - s3.putObject(params, function (err, data) { - if (err) { - logger.error(err); - res.status(500).end('upload image error'); - } else { - res.send({ - link: `https://s3-${config.s3.region}.amazonaws.com/${config.s3bucket}/${params.Key}` - }); - } - }); + break; - }); + case 's3': + var AWS = require('aws-sdk'); + var awsConfig = new AWS.Config(config.s3); + var s3 = new AWS.S3(awsConfig); - break; + fs.readFile(files.image.path, function (err, buffer) { + var params = { + Bucket: 'hackmd', + Key: path.join('uploads', path.basename(files.image.path)), + Body: buffer + }; - case 'imgur': - default: - imgur.setClientId(config.imgur.clientID); - imgur.uploadFile(files.image.path) - .then(function (json) { - if (config.debug) - logger.info('SERVER uploadimage success: ' + JSON.stringify(json)); + s3.putObject(params, function (err, data) { + if (err) { + logger.error(err); + res.status(500).end('upload image error'); + } else { res.send({ - link: json.data.link.replace(/^http:\/\//i, 'https://') + link: `https://s3-${config.s3.region}.amazonaws.com/${config.s3bucket}/${params.Key}` }); - }) - .catch(function (err) { - logger.error(err); - return res.status(500).end('upload image error'); + } + }); + + }); + + break; + + case 'imgur': + default: + imgur.setClientId(config.imgur.clientID); + imgur.uploadFile(files.image.path) + .then(function (json) { + if (config.debug) + logger.info('SERVER uploadimage success: ' + JSON.stringify(json)); + res.send({ + link: json.data.link.replace(/^http:\/\//i, 'https://') }); - break; - } - } catch (err) { - logger.error(err); - return res.status(500).end('upload image error'); + }) + .catch(function (err) { + logger.error(err); + return res.status(500).end('upload image error'); + }); + break; } - - }).catch((err) => { + } catch (err) { logger.error(err); - return res.status(500).end('process image error'); - }); + return res.status(500).end('upload image error'); + } } }); }); diff --git a/package.json b/package.json index e6bd2c5d..c3b71f1c 100644 --- a/package.json +++ b/package.json @@ -101,7 +101,6 @@ "scrypt": "^6.0.3", "select2": "^3.5.2-browserify", "sequelize-cli": "^2.4.0", - "sharp": "^0.16.2", "shortid": "2.2.6", "socket.io": "~1.6.0", "socket.io-client": "~1.6.0", -- cgit v1.2.3