Skip to content

Commit f0be08a

Browse files
committed
Merge pull request #5 from firstandthird/feature/update
Feature/update
2 parents 7f3cce5 + a373c38 commit f0be08a

File tree

5 files changed

+68
-37
lines changed

5 files changed

+68
-37
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
# hapi-upload-s3
22

33
A hapi plugin to upload files and images to S3. See example for usage.
4+
5+
### Installation
6+
7+
`npm install hapi-upload-s3`
8+
9+
Install graphicsmagick
10+
11+
Brew (osx): `brew install graphicsmagick`
12+
Apt: `apt-get install graphicsmagick`
13+
Windows: [Installation instructions](http://www.graphicsmagick.org/INSTALL-windows.html)

example/server.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
var Hapi = require('hapi');
22
var port = process.env.PORT || 8080;
3-
var server = new Hapi.Server(port, '0.0.0.0');
3+
var server = new Hapi.Server();
44
var fs = require('fs');
55

6-
server.pack.register([
6+
server.connection({ port: port });
7+
8+
server.register([
79
{
8-
plugin: require('../'),
10+
register: require('../'),
911
options: {
1012
s3AccessKey: '',
1113
s3SecretAccessKey: '',
1214
s3Region: 'us-east-1',
13-
s3Bucket: ''
15+
s3Bucket: '',
16+
contentTypes: ['image/jpeg'],
17+
maxBytes: 30000
1418
}
1519
}
1620
], function(err) {

lib/handler.js

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
var Uploader = require('s3-upload-stream').Uploader;
1+
var s3Stream = require('s3-upload-stream');
22
var Joi = require('joi');
33
var gm = require('gm');
4+
var Boom = require('boom');
45

56
exports.upload = {
67
payload: {
@@ -14,29 +15,32 @@ exports.upload = {
1415
handler: function(request, reply) {
1516
var self = this;
1617
var file = request.payload.file;
18+
1719
if (!file.hapi.filename) {
18-
return reply(this.Hapi.error.badRequest('must be a file'));
20+
return reply(Boom.badData('must be a file'));
1921
}
22+
23+
if (this.options.contentTypes.length && this.options.contentTypes.indexOf(file.hapi.headers['content-type']) === -1) {
24+
return reply(Boom.unsupportedMediaType('content-type not allowed'));
25+
}
26+
2027
var fileKey = this.getFileKey(file.hapi.filename);
2128

22-
new Uploader({
23-
s3Client: this.s3
24-
}, {
25-
'Bucket': self.bucketName,
26-
'Key': fileKey,
27-
'ContentType': file.hapi.headers['content-type']
28-
}, function (err, uploadStream) {
29-
if(err) {
30-
return reply(err);
31-
}
32-
uploadStream.on('uploaded', function (data) {
33-
return reply(self.getS3Url(fileKey));
34-
});
29+
var upload = s3Stream(this.s3).upload({
30+
'Bucket': self.bucketName,
31+
'Key': fileKey,
32+
'ContentType': file.hapi.headers['content-type']
33+
});
3534

36-
file.pipe(uploadStream);
37-
}
38-
);
35+
upload.on('error', function(err) {
36+
reply(Boom.wrap(err, 500, 'put'));
37+
});
3938

39+
upload.on('uploaded', function(data) {
40+
reply(self.getS3Url(fileKey));
41+
});
42+
43+
file.pipe(upload);
4044
}
4145
};
4246

@@ -54,9 +58,15 @@ exports.image = {
5458
handler: function(request, reply) {
5559
var self = this;
5660
var file = request.payload.file;
61+
5762
if (!file.hapi.filename) {
58-
return reply(this.Hapi.error.badRequest('must be a file'));
63+
return reply(Boom.badData('must be a file'));
64+
}
65+
66+
if (this.options.contentTypes.length && this.options.contentTypes.indexOf(file.hapi.headers['content-type']) === -1) {
67+
return reply(Boom.unsupportedMediaType('content-type not allowed'));
5968
}
69+
6070
var fileKey = this.getFileKey(file.hapi.filename);
6171

6272
var w = request.params.width;
@@ -70,7 +80,7 @@ exports.image = {
7080
.quality(80)
7181
.stream(function(err, stdout, stderr) {
7282
if (err) {
73-
return reply(self.Hapi.error.internal('gm', err));
83+
return reply(Boom.wrap(err, 500, 'gm'));
7484
}
7585

7686
var buf = new Buffer(0);
@@ -85,7 +95,7 @@ exports.image = {
8595
};
8696
self.s3.putObject(data, function(err, resp) {
8797
if (err) {
88-
return reply(self.Hapi.error.internal('put', err));
98+
return reply(Boom.wrap(err, 500, 'put'));
8999
}
90100
return reply(self.getS3Url(fileKey));
91101
});

lib/index.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
var handler = require('./handler');
22
var AWS = require('aws-sdk');
3-
var path = require('path');
4-
var uuid = require('uuid');
53
var datefmt = require('datefmt');
4+
var Hoek = require('hoek');
65

76
module.exports = function(plugin, options, next) {
87

98
options = options || {};
109

1110
var endpoint = options.endpoint || '/upload';
1211

12+
options.contentTypes = options.contentTypes || [];
13+
1314
var s3 = new AWS.S3({
1415
accessKeyId: options.s3AccessKey,
1516
secretAccessKey: options.s3SecretAccessKey,
1617
region: options.s3Region
1718
});
1819

1920
var getFileKey = function(filename) {
20-
var fileExtension = path.extname(filename);
21-
var fileKey = datefmt('%Y-%m-%d', new Date()) + '/' + uuid.v4() + fileExtension;
21+
var fileKey = datefmt('%Y-%m-%d', new Date()) + '/' + (+new Date) + '/' + filename;
2222
return fileKey;
2323
};
2424

@@ -36,9 +36,15 @@ module.exports = function(plugin, options, next) {
3636
getS3Url: options.getS3Url || getS3Url
3737
});
3838

39+
var maxBytes = {
40+
payload: {
41+
maxBytes: options.maxBytes || 1048576 // Hapi default (1MB)
42+
}
43+
};
44+
3945
plugin.route([
40-
{ path: endpoint, method: 'POST', config: handler.upload },
41-
{ path: endpoint + '/image/{type}/{width}/{height}', method: 'POST', config: handler.image }
46+
{ path: endpoint, method: 'POST', config: Hoek.applyToDefaults(handler.upload, maxBytes) },
47+
{ path: endpoint + '/image/{type}/{width}/{height}', method: 'POST', config: Hoek.applyToDefaults(handler.image, maxBytes) }
4248
]);
4349
next();
4450
};

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
"url": "https://github.com/firstandthird/hapi-upload-s3.git"
1010
},
1111
"devDependencies": {
12-
"hapi": "^6.2.2"
12+
"hapi": "^8.1.0"
1313
},
1414
"dependencies": {
15+
"aws-sdk": "^2.1.6",
16+
"boom": "^2.6.1",
1517
"datefmt": "^0.4.0",
16-
"uuid": "^1.4.1",
17-
"s3-upload-stream": "^0.4.0",
18-
"joi": "^4.6.2",
19-
"gm": "^1.16.0",
20-
"aws-sdk": "^2.0.11"
18+
"gm": "^1.17.0",
19+
"hoek": "^2.11.0",
20+
"joi": "^5.1.0",
21+
"s3-upload-stream": "^1.0.7"
2122
}
2223
}

0 commit comments

Comments
 (0)