upload image to s3
This commit is contained in:
parent
4d3672ae5d
commit
518a4a120b
3 changed files with 45 additions and 5 deletions
36
app.js
36
app.js
|
@ -402,13 +402,15 @@ app.get('/me', function (req, res) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
//upload to imgur
|
|
||||||
|
//upload image
|
||||||
app.post('/uploadimage', function (req, res) {
|
app.post('/uploadimage', function (req, res) {
|
||||||
var form = new formidable.IncomingForm();
|
var form = new formidable.IncomingForm();
|
||||||
|
|
||||||
|
form.keepExtensions = true;
|
||||||
|
|
||||||
if (config.imageUploadType === 'filesystem') {
|
if (config.imageUploadType === 'filesystem') {
|
||||||
form.uploadDir = "public/uploads";
|
form.uploadDir = "public/uploads";
|
||||||
form.keepExtensions = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
form.parse(req, function (err, fields, files) {
|
form.parse(req, function (err, fields, files) {
|
||||||
|
@ -418,17 +420,43 @@ app.post('/uploadimage', function (req, res) {
|
||||||
if (config.debug)
|
if (config.debug)
|
||||||
logger.info('SERVER received uploadimage: ' + JSON.stringify(files.image));
|
logger.info('SERVER received uploadimage: ' + JSON.stringify(files.image));
|
||||||
|
|
||||||
|
var path = require('path');
|
||||||
try {
|
try {
|
||||||
switch (config.imageUploadType) {
|
switch (config.imageUploadType) {
|
||||||
case 'filesystem':
|
case 'filesystem':
|
||||||
var path = require('path');
|
|
||||||
|
|
||||||
res.send({
|
res.send({
|
||||||
link: path.join(config.serverurl, files.image.path.match(/^public(.+$)/)[1])
|
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}`
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
case 'imgur':
|
case 'imgur':
|
||||||
default:
|
default:
|
||||||
imgur.setClientId(config.imgur.clientID);
|
imgur.setClientId(config.imgur.clientID);
|
||||||
|
|
|
@ -59,6 +59,15 @@ var documentmaxlength = config.documentmaxlength || 100000;
|
||||||
// image upload setting, available options are imgur/s3/filesystem
|
// image upload setting, available options are imgur/s3/filesystem
|
||||||
var imageUploadType = config.imageUploadType || 'imgur';
|
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
|
// auth
|
||||||
var facebook = (process.env.HMD_FACEBOOK_CLIENTID && process.env.HMD_FACEBOOK_CLIENTSECRET) ? {
|
var facebook = (process.env.HMD_FACEBOOK_CLIENTID && process.env.HMD_FACEBOOK_CLIENTSECRET) ? {
|
||||||
clientID: process.env.HMD_FACEBOOK_CLIENTID,
|
clientID: process.env.HMD_FACEBOOK_CLIENTID,
|
||||||
|
@ -143,5 +152,7 @@ module.exports = {
|
||||||
dropbox: dropbox,
|
dropbox: dropbox,
|
||||||
google: google,
|
google: google,
|
||||||
imgur: imgur,
|
imgur: imgur,
|
||||||
imageUploadType: imageUploadType
|
imageUploadType: imageUploadType,
|
||||||
|
s3: s3,
|
||||||
|
s3bucket: s3bucket
|
||||||
};
|
};
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"Idle.Js": "github:shawnmclean/Idle.js",
|
"Idle.Js": "github:shawnmclean/Idle.js",
|
||||||
"async": "^2.0.1",
|
"async": "^2.0.1",
|
||||||
|
"aws-sdk": "^2.7.0",
|
||||||
"blueimp-md5": "^2.4.0",
|
"blueimp-md5": "^2.4.0",
|
||||||
"body-parser": "^1.15.2",
|
"body-parser": "^1.15.2",
|
||||||
"bootstrap": "^3.3.7",
|
"bootstrap": "^3.3.7",
|
||||||
|
|
Loading…
Reference in a new issue