1- var Uploader = require ( 's3-upload-stream' ) . Uploader ;
1+ var s3Stream = require ( 's3-upload-stream' ) ;
22var Joi = require ( 'joi' ) ;
33var gm = require ( 'gm' ) ;
4+ var Boom = require ( 'boom' ) ;
45
56exports . 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 } ) ;
0 commit comments