Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 29 additions & 4 deletions lib/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,31 @@ exports.upload = {

var fileKey = this.getFileKey(file.hapi.filename);

var upload = s3Stream(this.s3).upload({
var uploadOptions = {
'Bucket': self.bucketName,
'Key': fileKey,
'ContentType': file.hapi.headers['content-type']
});
};

if (self.acl) uploadOptions['ACL'] = self.acl;
if (self.storageClass) uploadOptions['StorageClass'] = self.storageClass;

var upload = s3Stream(this.s3).upload(uploadOptions);

upload.on('error', function(err) {
reply(Boom.wrap(err, 500, 'put'));
if (err instanceof Error === false) {
err = new Error(err);
}
return reply(Boom.wrap(err, 500, 'put'));
});

upload.on('uploaded', function(data) {
reply(self.getS3Url(fileKey));

if (self.nextUrl) {
return reply.redirect(self.nextUrl);
}

return reply(self.getS3Url(fileKey));
});

file.pipe(upload);
Expand Down Expand Up @@ -98,10 +111,22 @@ exports.image = {
Body: buf,
ContentType: file.hapi.headers['content-type']
};

if (self.acl) data['ACL'] = self.acl;
if (self.storageClass) data['StorageClass'] = self.storageClass;

self.s3.putObject(data, function(err, resp) {
if (err) {
if (err instanceof Error === false) {
err = new Error(err);
}
return reply(Boom.wrap(err, 500, 'put'));
}

if (self.nextUrl) {
return reply.redirect(self.nextUrl);
}

return reply(self.getS3Url(fileKey));
});
});
Expand Down
5 changes: 4 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ module.exports = function(plugin, options, next) {
bucketName: options.s3Bucket,
Hapi: plugin.hapi,
getFileKey: options.getFileKey || getFileKey,
getS3Url: options.getS3Url || getS3Url
getS3Url: options.getS3Url || getS3Url,
acl: options.acl || false,
storageClass: options.storageClass || false,
nextUrl: options.nextUrl || false
});

var maxBytes = {
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
"description": "Upload files and images directly to s3",
"homepage": "https://github.com/firstandthird/hapi-upload-s3",
"author": "First+Third",
"version": "0.5.0",
"contributors": [{
"name": "Tane Piper",
"email": "tane@pebblecode.com",
"url": "https://github.com/tanepiper"
}],
"version": "0.5.1",
"repository": {
"type": "git",
"url": "https://github.com/firstandthird/hapi-upload-s3.git"
Expand Down