summaryrefslogtreecommitdiff
path: root/app.js
diff options
context:
space:
mode:
authorYukai Huang2016-11-16 12:05:24 +0800
committerYukai Huang2016-11-16 12:05:24 +0800
commit518a4a120b1fe30953b6b526da1eff826484aee0 (patch)
tree3cbbc3b19a302404045f9501610434b9d7b80b61 /app.js
parent4d3672ae5d52b781ea1a740142a74ba416833113 (diff)
upload image to s3
Diffstat (limited to 'app.js')
-rw-r--r--app.js36
1 files changed, 32 insertions, 4 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);