nodebb-plugin-s3-uploads
Advanced tools
Comparing version 0.2.3 to 0.3.0
193
index.js
@@ -7,8 +7,9 @@ var Package = require("./package.json"); | ||
fs = require('fs'), | ||
request = require('request'), | ||
path = require('path'), | ||
winston = module.parent.require('winston'), | ||
gm = module.parent.require('gm').subClass({imageMagick: true}), | ||
meta = module.parent.require('./meta'), | ||
db = module.parent.require('./database'); | ||
winston = module.parent.require('winston'), | ||
db = module.parent.require('./database'); | ||
(function(plugin) { | ||
@@ -176,129 +177,117 @@ "use strict"; | ||
plugin.handleUpload = function (image, callback) { | ||
if(!image || !image.path){ | ||
winston.error(image); | ||
return callback(makeError("Invalid image data from plugin hook 'filter:uploadImage'")); | ||
plugin.uploadImage = function (data, callback) { | ||
var image = data.image; | ||
if (!image) { | ||
return callback(new Error('invalid image')); | ||
} | ||
fs.readFile(image.path, putObject); | ||
var type = image.url ? 'url' : 'file'; | ||
function putObject(err, buffer){ | ||
if(err) { | ||
return callback(makeError(err)); | ||
if (type === 'file') { | ||
if (!image.path) { | ||
return callback(new Error('invalid image path')); | ||
} | ||
var s3Path; | ||
if (settings.path && 0 < settings.path.length) { | ||
s3Path = settings.path; | ||
fs.readFile(image.path, function(err, buffer) { | ||
uploadToS3(image.name, err, buffer, callback); | ||
}); | ||
} | ||
else { | ||
var filename = image.url.split('/').pop(); | ||
if (!s3Path.match(/\/$/)) { | ||
// Add trailing slash | ||
s3Path = s3Path + '/'; | ||
} | ||
} | ||
else { | ||
s3Path = '/'; | ||
} | ||
var imageDimension = parseInt(meta.config.profileImageDimension, 10) || 128; | ||
var s3KeyPath = s3Path.replace(/^\//, ''); // S3 Key Path should not start with slash. | ||
// Resize image. | ||
gm(request(image.url), filename) | ||
.resize(imageDimension + "^", imageDimension + "^") | ||
.stream(function(err, stdout, stderr) { | ||
if (err) { | ||
return callback(makeError(err)); | ||
} | ||
var params = { | ||
Bucket: settings.bucket, | ||
ACL: "public-read", | ||
Key: s3KeyPath + uuid() + path.extname(image.name), | ||
Body: buffer, | ||
ContentLength: buffer.length, | ||
ContentType: mime.lookup(image.name) | ||
}; | ||
S3().putObject(params, function(err){ | ||
if(err){ | ||
return callback(makeError(err)); | ||
// This is sort of a hack - We're going to stream the gm output to a buffer and then upload. | ||
// See https://github.com/aws/aws-sdk-js/issues/94 | ||
var buf = new Buffer(0); | ||
stdout.on('data', function(d) { | ||
buf = Buffer.concat([buf, d]); | ||
}); | ||
stdout.on('end', function() { | ||
uploadToS3(filename, null, buf, callback); | ||
}); | ||
} | ||
); | ||
} | ||
}; | ||
var s3Host; | ||
if (settings.host && 0 < settings.host.length) { | ||
s3Host = settings.host; | ||
plugin.uploadFile = function (data, callback) { | ||
var file = data.file; | ||
if (!s3Host.match(/\/$/)) { | ||
// Add trailing slash | ||
s3Host = s3Host + '/'; | ||
} | ||
} | ||
else { | ||
s3Host = params.Bucket + ".s3.amazonaws.com/"; | ||
} | ||
if (!file) { | ||
return callback(new Error('invalid file')); | ||
} | ||
callback(null, { | ||
name: image.name, | ||
// Use protocol-less urls so that both HTTP and HTTPS work: | ||
url: "//" + s3Host + params.Key | ||
}); | ||
}); | ||
if (!file.path) { | ||
return callback(new Error('invalid file path')); | ||
} | ||
fs.readFile(file.path, function(err, buffer) { | ||
uploadToS3(file.name, err, buffer, callback); | ||
}); | ||
}; | ||
plugin.handleFileUpload = function (file, callback) { | ||
if(!file || !file.path){ | ||
winston.error(file); | ||
return callback(makeError("Invalid file data from plugin hook 'filter:uploadFile'")); | ||
function uploadToS3(filename, err, buffer, callback) { | ||
if (err) { | ||
return callback(makeError(err)); | ||
} | ||
fs.readFile(file.path, putObject); | ||
var s3Path; | ||
if (settings.path && 0 < settings.path.length) { | ||
s3Path = settings.path; | ||
function putObject(err, buffer){ | ||
if(err) { | ||
if (!s3Path.match(/\/$/)) { | ||
// Add trailing slash | ||
s3Path = s3Path + '/'; | ||
} | ||
} | ||
else { | ||
s3Path = '/'; | ||
} | ||
var s3KeyPath = s3Path.replace(/^\//, ''); // S3 Key Path should not start with slash. | ||
var params = { | ||
Bucket: settings.bucket, | ||
ACL: "public-read", | ||
Key: s3KeyPath + uuid() + path.extname(filename), | ||
Body: buffer, | ||
ContentLength: buffer.length, | ||
ContentType: mime.lookup(filename) | ||
}; | ||
S3().putObject(params, function(err) { | ||
if (err) { | ||
return callback(makeError(err)); | ||
} | ||
var s3Path; | ||
if (settings.path && 0 < settings.path.length) { | ||
s3Path = settings.path; | ||
var s3Host; | ||
if (settings.host && 0 < settings.host.length) { | ||
s3Host = settings.host; | ||
if (!s3Path.match(/\/$/)) { | ||
if (!s3Host.match(/\/$/)) { | ||
// Add trailing slash | ||
s3Path = s3Path + '/'; | ||
s3Host = s3Host + '/'; | ||
} | ||
} | ||
else { | ||
s3Path = '/'; | ||
s3Host = params.Bucket + ".s3.amazonaws.com/"; | ||
} | ||
var s3KeyPath = s3Path.replace(/^\//, ''); // S3 Key Path should not start with slash. | ||
var params = { | ||
Bucket: settings.bucket, | ||
ACL: "public-read", | ||
Key: s3KeyPath + uuid() + path.extname(file.name), | ||
Body: buffer, | ||
ContentLength: buffer.length, | ||
ContentType: mime.lookup(file.name) | ||
}; | ||
S3().putObject(params, function(err){ | ||
if(err){ | ||
return callback(makeError(err)); | ||
} | ||
var s3Host; | ||
if (settings.host && 0 < settings.host.length) { | ||
s3Host = settings.host; | ||
if (!s3Host.match(/\/$/)) { | ||
// Add trailing slash | ||
s3Host = s3Host + '/'; | ||
} | ||
} | ||
else { | ||
s3Host = params.Bucket + ".s3.amazonaws.com/"; | ||
} | ||
callback(null, { | ||
name: file.name, | ||
// Use protocol-less urls so that both HTTP and HTTPS work: | ||
url: "//" + s3Host + params.Key | ||
}); | ||
callback(null, { | ||
name: filename, | ||
// Use protocol-less urls so that both HTTP and HTTPS work: | ||
url: "//" + s3Host + params.Key | ||
}); | ||
} | ||
}; | ||
}); | ||
} | ||
@@ -305,0 +294,0 @@ var admin = plugin.admin = {}; |
{ | ||
"name": "nodebb-plugin-s3-uploads", | ||
"version": "0.2.3", | ||
"version": "0.3.0", | ||
"description": "A plugin for NodeBB to take file uploads and store them on S3", | ||
@@ -29,2 +29,5 @@ "main": "index.js", | ||
}, | ||
"nbbpm": { | ||
"compatibility": "^0.6.0" | ||
}, | ||
"dependencies": { | ||
@@ -31,0 +34,0 @@ "aws-sdk": "^2.0.23", |
@@ -17,4 +17,4 @@ { | ||
{ "hook": "filter:admin.header.build", "method": "admin.menu"}, | ||
{ "hook": "filter:uploadImage", "method": "handleUpload", "priority": 6 }, | ||
{ "hook": "filter:uploadFile", "method": "handleFileUpload", "priority": 6 } | ||
{ "hook": "filter:uploadImage", "method": "uploadImage", "priority": 6 }, | ||
{ "hook": "filter:uploadFile", "method": "uploadFile", "priority": 6 } | ||
], | ||
@@ -21,0 +21,0 @@ "compatibility": "~0.5.0", |
@@ -6,5 +6,6 @@ # NodeBB S3 Uploads Plugin | ||
| Dependency | Version Requirement | | ||
| -------------- |:-----------------------:| | ||
| NodeBB | >= 0.3.2 or [a909a253](https://github.com/designcreateplay/NodeBB/commit/a909a253931c20427c14c777c1bb6629a79d449d) | | ||
| Plugin Version | Dependency | Version Requirement | | ||
| ---------------| -------------- |:-----------------------:| | ||
| 0.2.x | NodeBB | <= 0.5.3 and >= 0.3.2 | | ||
| 0.3.x | NodeBB | >= 0.6.0 | | ||
@@ -11,0 +12,0 @@ A plugin for NodeBB to take file uploads and store them on S3, uses the `filter:uploadImage` hook in NodeBB. |
70
18344
268