From a04c6ef127f5982eec5353e1468ef881fbac03ea Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Mon, 14 Nov 2016 16:44:12 +0800 Subject: Install nodemon for local development --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 57e31ccf..4ec012f8 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "app.js", "license": "MIT", "scripts": { - "dev": "webpack --config webpack.config.js --progress --colors --watch", + "dev": "webpack --config webpack.config.js --progress --colors --watch & nodemon app.js", "build": "webpack --config webpack.production.js --progress --colors", "assets:install": "bower install", "postinstall": "bin/heroku", @@ -137,6 +137,7 @@ "json-loader": "^0.5.4", "less": "^2.7.1", "less-loader": "^2.2.3", + "nodemon": "^1.11.0", "optimize-css-assets-webpack-plugin": "^1.3.0", "script-loader": "^0.7.0", "style-loader": "^0.13.1", -- cgit v1.2.3 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 --- .gitignore | 3 +++ app.js | 3 +++ public/uploads/.gitkeep | 0 3 files changed, 6 insertions(+) create mode 100644 public/uploads/.gitkeep diff --git a/.gitignore b/.gitignore index 9a95231c..36c1efd8 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,6 @@ public/js/config.js # ignore webpack build public/build public/views/build + +public/uploads/* +!public/uploads/.gitkeep 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); diff --git a/public/uploads/.gitkeep b/public/uploads/.gitkeep new file mode 100644 index 00000000..e69de29b -- 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 ++++++++++++++++++++++++++------------ lib/config.js | 6 +++++- 2 files changed, 31 insertions(+), 13 deletions(-) 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'); diff --git a/lib/config.js b/lib/config.js index 76b21f0d..001cc8fa 100644 --- a/lib/config.js +++ b/lib/config.js @@ -56,6 +56,9 @@ var heartbeattimeout = config.heartbeattimeout || 10000; // document var documentmaxlength = config.documentmaxlength || 100000; +// image upload setting, available options are imgur/s3/filesystem +var imageUploadType = config.imageUploadType || 'imgur'; + // auth var facebook = (process.env.HMD_FACEBOOK_CLIENTID && process.env.HMD_FACEBOOK_CLIENTSECRET) ? { clientID: process.env.HMD_FACEBOOK_CLIENTID, @@ -139,5 +142,6 @@ module.exports = { gitlab: gitlab, dropbox: dropbox, google: google, - imgur: imgur + imgur: imgur, + imageUploadType: imageUploadType }; -- 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(-) 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(-) 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 ++++++++++++++++++++++++++++++++---- lib/config.js | 13 ++++++++++++- package.json | 1 + 3 files changed, 45 insertions(+), 5 deletions(-) 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); diff --git a/lib/config.js b/lib/config.js index 001cc8fa..ecc68f75 100644 --- a/lib/config.js +++ b/lib/config.js @@ -59,6 +59,15 @@ var documentmaxlength = config.documentmaxlength || 100000; // image upload setting, available options are imgur/s3/filesystem var imageUploadType = config.imageUploadType || 'imgur'; +var s3Keys = ['accessKeyId', 'secretAccessKey', 'region']; +config.s3 = config.s3 || {}; +var s3 = s3Keys.reduce(function(prev, cur) { + prev[cur] = config.s3[cur]; + return prev; +}, {}); + +var s3bucket = config.s3.bucket; + // auth var facebook = (process.env.HMD_FACEBOOK_CLIENTID && process.env.HMD_FACEBOOK_CLIENTSECRET) ? { clientID: process.env.HMD_FACEBOOK_CLIENTID, @@ -143,5 +152,7 @@ module.exports = { dropbox: dropbox, google: google, imgur: imgur, - imageUploadType: imageUploadType + imageUploadType: imageUploadType, + s3: s3, + s3bucket: s3bucket }; diff --git a/package.json b/package.json index 4ec012f8..7d9acf80 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "dependencies": { "Idle.Js": "github:shawnmclean/Idle.js", "async": "^2.0.1", + "aws-sdk": "^2.7.0", "blueimp-md5": "^2.4.0", "body-parser": "^1.15.2", "bootstrap": "^3.3.7", -- cgit v1.2.3 From 7830184993a2624a2aae4515676d1324fab76594 Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Wed, 16 Nov 2016 14:27:18 +0800 Subject: Create s3 config guide --- .../s3-image-upload/bucket-policy-editor.png | Bin 0 -> 54641 bytes .../images/s3-image-upload/bucket-property.png | Bin 0 -> 71762 bytes .../images/s3-image-upload/create-bucket.png | Bin 0 -> 70260 bytes .../images/s3-image-upload/custom-policy.png | Bin 0 -> 55804 bytes docs/guides/images/s3-image-upload/iam-user.png | Bin 0 -> 91526 bytes .../images/s3-image-upload/review-policy.png | Bin 0 -> 100067 bytes docs/guides/s3-image-upload.md | 62 +++++++++++++++++++++ 7 files changed, 62 insertions(+) create mode 100644 docs/guides/images/s3-image-upload/bucket-policy-editor.png create mode 100644 docs/guides/images/s3-image-upload/bucket-property.png create mode 100644 docs/guides/images/s3-image-upload/create-bucket.png create mode 100644 docs/guides/images/s3-image-upload/custom-policy.png create mode 100644 docs/guides/images/s3-image-upload/iam-user.png create mode 100644 docs/guides/images/s3-image-upload/review-policy.png create mode 100644 docs/guides/s3-image-upload.md diff --git a/docs/guides/images/s3-image-upload/bucket-policy-editor.png b/docs/guides/images/s3-image-upload/bucket-policy-editor.png new file mode 100644 index 00000000..d5f4427f Binary files /dev/null and b/docs/guides/images/s3-image-upload/bucket-policy-editor.png differ diff --git a/docs/guides/images/s3-image-upload/bucket-property.png b/docs/guides/images/s3-image-upload/bucket-property.png new file mode 100644 index 00000000..9f4dc828 Binary files /dev/null and b/docs/guides/images/s3-image-upload/bucket-property.png differ diff --git a/docs/guides/images/s3-image-upload/create-bucket.png b/docs/guides/images/s3-image-upload/create-bucket.png new file mode 100644 index 00000000..925f0926 Binary files /dev/null and b/docs/guides/images/s3-image-upload/create-bucket.png differ diff --git a/docs/guides/images/s3-image-upload/custom-policy.png b/docs/guides/images/s3-image-upload/custom-policy.png new file mode 100644 index 00000000..01c570cd Binary files /dev/null and b/docs/guides/images/s3-image-upload/custom-policy.png differ diff --git a/docs/guides/images/s3-image-upload/iam-user.png b/docs/guides/images/s3-image-upload/iam-user.png new file mode 100644 index 00000000..b26be122 Binary files /dev/null and b/docs/guides/images/s3-image-upload/iam-user.png differ diff --git a/docs/guides/images/s3-image-upload/review-policy.png b/docs/guides/images/s3-image-upload/review-policy.png new file mode 100644 index 00000000..7173788a Binary files /dev/null and b/docs/guides/images/s3-image-upload/review-policy.png differ diff --git a/docs/guides/s3-image-upload.md b/docs/guides/s3-image-upload.md new file mode 100644 index 00000000..4c9faded --- /dev/null +++ b/docs/guides/s3-image-upload.md @@ -0,0 +1,62 @@ +# Guide - Setup HackMD S3 image upload + +1. Go to [AWS S3 console](https://console.aws.amazon.com/s3/home) and create a new bucket. + + ![create-bucket](images/s3-image-upload/create-bucket.png) + +2. Click on bucket, select **Properties** on the side panel, and find **Permission** section. Click **Edit bucket policy**. + + ![bucket-property](images/s3-image-upload/bucket-property.png) + +3. Enter the following policy, replace `bucket_name` with your bucket name: + + ![bucket-policy-editor](images/s3-image-upload/bucket-policy-editor.png) + +```json +{ + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Principal": "*", + "Action": "s3:GetObject", + "Resource": "arn:aws:s3:::bucket_name/uploads/*" + } + ] +} +``` + +4. Go to IAM console and create a new IAM user. Remember your user credentials(`key`/`access token`) + +5. Enter user page, select **Permission** tab, look at **Inline Policies** section, and click **Create User Policy** + + ![iam-user](images/s3-image-upload/iam-user.png) + +6. Select **Custom Policy** + + ![custom-policy](images/s3-image-upload/custom-policy.png) + +7. Enter the following policy, replace `bucket_name` with your bucket name: + + ![review-policy](images/s3-image-upload/review-policy.png) + +```json +{ + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": [ + "s3:*" + ], + "Resource": [ + "arn:aws:s3:::bucket_name/uploads/*" + ] + } + ] +} +``` + +## Related Tools + +* [AWS Policy Generator](http://awspolicygen.s3.amazonaws.com/policygen.html) -- cgit v1.2.3 From 2b2c5935312372e9be4ca092024fdd5ce691de7b Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Wed, 16 Nov 2016 14:44:35 +0800 Subject: Config s3 through env variables --- lib/config.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/config.js b/lib/config.js index ecc68f75..99b94279 100644 --- a/lib/config.js +++ b/lib/config.js @@ -59,14 +59,13 @@ var documentmaxlength = config.documentmaxlength || 100000; // image upload setting, available options are imgur/s3/filesystem var imageUploadType = config.imageUploadType || 'imgur'; -var s3Keys = ['accessKeyId', 'secretAccessKey', 'region']; config.s3 = config.s3 || {}; -var s3 = s3Keys.reduce(function(prev, cur) { - prev[cur] = config.s3[cur]; - return prev; -}, {}); - -var s3bucket = config.s3.bucket; +var s3 = { + accessKeyId: process.env.S3_ACCESS_KEY_ID || config.s3.accessKeyId, + secretAccessKey: process.env.S3_SECRET_ACCESS_KEY || config.s3.secretAccessKey, + region: process.env.S3_REGION || config.s3.region +} +var s3bucket = process.env.S3_BUCKET || config.s3.bucket; // auth var facebook = (process.env.HMD_FACEBOOK_CLIENTID && process.env.HMD_FACEBOOK_CLIENTSECRET) ? { -- cgit v1.2.3 From 1f409197c350f04caf878d0e8926d5edd882f4f5 Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Wed, 16 Nov 2016 14:46:38 +0800 Subject: Fix order list --- docs/guides/s3-image-upload.md | 58 +++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/docs/guides/s3-image-upload.md b/docs/guides/s3-image-upload.md index 4c9faded..c92e91f8 100644 --- a/docs/guides/s3-image-upload.md +++ b/docs/guides/s3-image-upload.md @@ -12,19 +12,19 @@ ![bucket-policy-editor](images/s3-image-upload/bucket-policy-editor.png) -```json -{ - "Version": "2012-10-17", - "Statement": [ - { - "Effect": "Allow", - "Principal": "*", - "Action": "s3:GetObject", - "Resource": "arn:aws:s3:::bucket_name/uploads/*" - } - ] -} -``` + ```json + { + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Principal": "*", + "Action": "s3:GetObject", + "Resource": "arn:aws:s3:::bucket_name/uploads/*" + } + ] + } + ``` 4. Go to IAM console and create a new IAM user. Remember your user credentials(`key`/`access token`) @@ -40,22 +40,22 @@ ![review-policy](images/s3-image-upload/review-policy.png) -```json -{ - "Version": "2012-10-17", - "Statement": [ - { - "Effect": "Allow", - "Action": [ - "s3:*" - ], - "Resource": [ - "arn:aws:s3:::bucket_name/uploads/*" - ] - } - ] -} -``` + ```json + { + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": [ + "s3:*" + ], + "Resource": [ + "arn:aws:s3:::bucket_name/uploads/*" + ] + } + ] + } + ``` ## Related Tools -- 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 +++++++++++++++++++++++++++++++++-------------------------- package.json | 1 + 2 files changed, 70 insertions(+), 53 deletions(-) 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'); - } + }); } }); }); diff --git a/package.json b/package.json index 7d9acf80..9f2f9116 100644 --- a/package.json +++ b/package.json @@ -87,6 +87,7 @@ "reveal.js": "^3.3.0", "sequelize": "^3.24.3", "sequelize-cli": "^2.4.0", + "sharp": "^0.16.2", "shortid": "2.2.6", "socket.io": "1.5.0", "socket.io-client": "^1.5.0", -- cgit v1.2.3 From 975b1f248f99251462741f8725b047391fa367e4 Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Thu, 17 Nov 2016 16:24:30 +0800 Subject: Add libvips heroku buildpack --- app.json | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app.json b/app.json index 95c2ffbf..fcdc60a7 100644 --- a/app.json +++ b/app.json @@ -107,5 +107,13 @@ }, "addons": [ "heroku-postgresql" + ], + "buildpacks": [ + { + "url": "https://github.com/alex88/heroku-buildpack-vips" + }, + { + "url": "https://github.com/heroku/heroku-buildpack-nodejs" + } ] } -- cgit v1.2.3 From 46a4fe10abdec2cfb102336c8f6971eb7202b08b Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Thu, 17 Nov 2016 16:28:02 +0800 Subject: Add env config prefix --- lib/config.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/config.js b/lib/config.js index 99b94279..a906dfa4 100644 --- a/lib/config.js +++ b/lib/config.js @@ -57,15 +57,15 @@ var heartbeattimeout = config.heartbeattimeout || 10000; var documentmaxlength = config.documentmaxlength || 100000; // image upload setting, available options are imgur/s3/filesystem -var imageUploadType = config.imageUploadType || 'imgur'; +var imageUploadType = process.env.HMD_IMAGE_UPLOAD_TYPE || config.imageUploadType || 'imgur'; config.s3 = config.s3 || {}; var s3 = { - accessKeyId: process.env.S3_ACCESS_KEY_ID || config.s3.accessKeyId, - secretAccessKey: process.env.S3_SECRET_ACCESS_KEY || config.s3.secretAccessKey, - region: process.env.S3_REGION || config.s3.region + accessKeyId: process.env.HMD_S3_ACCESS_KEY_ID || config.s3.accessKeyId, + secretAccessKey: process.env.HMD_S3_SECRET_ACCESS_KEY || config.s3.secretAccessKey, + region: process.env.HMD_S3_REGION || config.s3.region } -var s3bucket = process.env.S3_BUCKET || config.s3.bucket; +var s3bucket = process.env.HMD_S3_BUCKET || config.s3.bucket; // auth var facebook = (process.env.HMD_FACEBOOK_CLIENTID && process.env.HMD_FACEBOOK_CLIENTSECRET) ? { -- cgit v1.2.3 From aaf034bfbc7b0b4ac6153b8fefdc04d10a9f0c1f Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Thu, 17 Nov 2016 18:13:57 +0800 Subject: Update README --- README.md | 7 +++++++ docs/guides/s3-image-upload.md | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/README.md b/README.md index 6d788ac2..7f42f862 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,11 @@ Environment variables (will overwrite other server configs) | HMD_GOOGLE_CLIENTID | no example | Google API client id | | HMD_GOOGLE_CLIENTSECRET | no example | Google API client secret | | HMD_IMGUR_CLIENTID | no example | Imgur API client id | +| HMD_IMAGE_UPLOAD_TYPE | `imgur`, `s3` or `filesystem` | Where to upload image. For S3, see our [S3 Image Upload Guide](docs/guides/s3-image-upload.md) | +| HMD_S3_ACCESS_KEY_ID | no example | AWS access key id | +| HMD_S3_SECRET_ACCESS_KEY | no example | AWS secret key | +| HMD_S3_REGION | `ap-northeast-1` | AWS S3 region | +| HMD_S3_BUCKET | no example | AWS S3 bucket name | Server settings `config.json` --- @@ -166,6 +171,8 @@ Server settings `config.json` | heartbeatinterval | `5000` | socket.io heartbeat interval | | heartbeattimeout | `10000` | socket.io heartbeat timeout | | documentmaxlength | `100000` | note max length | +| imageUploadType | `imgur`(default), `s3` or `filesystem` | Where to upload image +| s3 | `{ "accessKeyId": "YOUR_S3_ACCESS_KEY_ID", "secretAccessKey": "YOUR_S3_ACCESS_KEY", "region": "YOUR_S3_REGION", "bucket": "YOUR_S3_BUCKET_NAME" }` | When `imageUploadType` be setted to `s3`, you would also need to setup this key, check our [S3 Image Upload Guide](docs/guides/s3-image-upload.md) | Third-party integration api key settings --- diff --git a/docs/guides/s3-image-upload.md b/docs/guides/s3-image-upload.md index c92e91f8..3d6259b9 100644 --- a/docs/guides/s3-image-upload.md +++ b/docs/guides/s3-image-upload.md @@ -57,6 +57,25 @@ } ``` +8. Edit `config.json` and set following keys: + + ```javascript + { + "production": { + ... + "imageUploadType": "s3", + "s3": { + "accessKeyId": "YOUR_S3_ACCESS_KEY_ID", + "secretAccessKey": "YOUR_S3_ACCESS_KEY", + "region": "YOUR_S3_REGION", // example: ap-northeast-1 + "bucket": "YOUR_S3_BUCKET_NAME" + } + } + } + ``` + +9. In additional to edit `config.json` directly, you could also try [environment variable](https://github.com/hackmdio/hackmd#environment-variables-will-overwrite-other-server-configs). + ## Related Tools * [AWS Policy Generator](http://awspolicygen.s3.amazonaws.com/policygen.html) -- 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(+) 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