From 81b368c11c81c7c759d4abcdff455121f2e87d9a Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Mon, 14 Nov 2016 16:45:57 +0800 Subject: upload image to public/uploads --- app.js | 3 +++ 1 file changed, 3 insertions(+) (limited to 'app.js') diff --git a/app.js b/app.js index adb7f81f..30baf1ca 100644 --- a/app.js +++ b/app.js @@ -405,6 +405,9 @@ app.get('/me', function (req, res) { //upload to imgur app.post('/uploadimage', function (req, res) { var form = new formidable.IncomingForm(); + form.uploadDir = "public/uploads"; + form.keepExtensions = true; + form.parse(req, function (err, fields, files) { if (err || !files.image || !files.image.path) { response.errorForbidden(res); -- cgit v1.2.3 From a5dad2930037b466542f1e77d3cbb0d0cbf184aa Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Mon, 14 Nov 2016 17:07:07 +0800 Subject: support filesystem image upload --- app.js | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) (limited to 'app.js') diff --git a/app.js b/app.js index 30baf1ca..f5f8a3a5 100644 --- a/app.js +++ b/app.js @@ -405,6 +405,7 @@ app.get('/me', function (req, res) { //upload to imgur app.post('/uploadimage', function (req, res) { var form = new formidable.IncomingForm(); + form.uploadDir = "public/uploads"; form.keepExtensions = true; @@ -414,20 +415,33 @@ app.post('/uploadimage', function (req, res) { } else { if (config.debug) logger.info('SERVER received uploadimage: ' + JSON.stringify(files.image)); - imgur.setClientId(config.imgur.clientID); + try { - 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://') - }); - }) - .catch(function (err) { - logger.error(err); - return res.status(500).end('upload image error'); + switch (config.imageUploadType) { + case 'filesystem': + res.send({ + link: files.image.path.match(/^public(.+$)/)[1] }); + + 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://') + }); + }) + .catch(function (err) { + logger.error(err); + return res.status(500).end('upload image error'); + }); + break; + } } catch (err) { logger.error(err); return res.status(500).end('upload image error'); -- cgit v1.2.3 From 8db6624ae99ed23485213e68bfb41b76af4b4fc5 Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Tue, 15 Nov 2016 23:25:41 +0800 Subject: save to upload folder only when option enabled --- app.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'app.js') diff --git a/app.js b/app.js index f5f8a3a5..b67c77c2 100644 --- a/app.js +++ b/app.js @@ -406,8 +406,10 @@ app.get('/me', function (req, res) { app.post('/uploadimage', function (req, res) { var form = new formidable.IncomingForm(); - form.uploadDir = "public/uploads"; - form.keepExtensions = true; + if (config.imageUploadType === 'filesystem') { + form.uploadDir = "public/uploads"; + form.keepExtensions = true; + } form.parse(req, function (err, fields, files) { if (err || !files.image || !files.image.path) { -- cgit v1.2.3 From 4d3672ae5d52b781ea1a740142a74ba416833113 Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Wed, 16 Nov 2016 10:50:07 +0800 Subject: Join image path with config.serverurl --- app.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'app.js') diff --git a/app.js b/app.js index b67c77c2..16ef798e 100644 --- a/app.js +++ b/app.js @@ -421,8 +421,10 @@ app.post('/uploadimage', function (req, res) { try { switch (config.imageUploadType) { case 'filesystem': + var path = require('path'); + res.send({ - link: files.image.path.match(/^public(.+$)/)[1] + link: path.join(config.serverurl, files.image.path.match(/^public(.+$)/)[1]) }); break; -- cgit v1.2.3 From 518a4a120b1fe30953b6b526da1eff826484aee0 Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Wed, 16 Nov 2016 12:05:24 +0800 Subject: upload image to s3 --- app.js | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to 'app.js') diff --git a/app.js b/app.js index 16ef798e..7cb33a1d 100644 --- a/app.js +++ b/app.js @@ -402,13 +402,15 @@ app.get('/me', function (req, res) { }); } }); -//upload to imgur + +//upload image app.post('/uploadimage', function (req, res) { var form = new formidable.IncomingForm(); + form.keepExtensions = true; + if (config.imageUploadType === 'filesystem') { form.uploadDir = "public/uploads"; - form.keepExtensions = true; } form.parse(req, function (err, fields, files) { @@ -418,17 +420,43 @@ app.post('/uploadimage', function (req, res) { if (config.debug) logger.info('SERVER received uploadimage: ' + JSON.stringify(files.image)); + var path = require('path'); try { switch (config.imageUploadType) { case 'filesystem': - var path = require('path'); - 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 'imgur': default: imgur.setClientId(config.imgur.clientID); -- cgit v1.2.3 From 2279986f97520efccb8f68a9e9f8535cd4eb3bf2 Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Wed, 16 Nov 2016 17:07:00 +0800 Subject: Config sharp image preprocessing --- app.js | 122 +++++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 69 insertions(+), 53 deletions(-) (limited to 'app.js') diff --git a/app.js b/app.js index 7cb33a1d..1c1120d0 100644 --- a/app.js +++ b/app.js @@ -413,71 +413,87 @@ 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 { - 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]) - }); + 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]) + }); - break; + 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}` + }); + } + }); - 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 - }; + break; - s3.putObject(params, function (err, data) { - if (err) { - logger.error(err); - res.status(500).end('upload image error'); - } else { + 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: `https://s3-${config.s3.region}.amazonaws.com/${config.s3bucket}/${params.Key}` + link: json.data.link.replace(/^http:\/\//i, 'https://') }); - } - }); - - }); - - 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://') + }) + .catch(function (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; + break; + } + } catch (err) { + logger.error(err); + return res.status(500).end('upload image error'); } - } catch (err) { - logger.error(err); - return res.status(500).end('upload image error'); - } + }); } }); }); -- cgit v1.2.3 From 1a4f3950e6d03797beb79c63a3e6f071d8298c17 Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Tue, 22 Nov 2016 07:20:48 +0800 Subject: Handle preprocess image error --- app.js | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'app.js') diff --git a/app.js b/app.js index 1c1120d0..eb0ee228 100644 --- a/app.js +++ b/app.js @@ -493,6 +493,10 @@ app.post('/uploadimage', function (req, res) { logger.error(err); return res.status(500).end('upload image error'); } + + }).catch((err) => { + logger.error(err); + return res.status(500).end('process image error'); }); } }); -- cgit v1.2.3