@@ -44,11 +44,14 @@ function uploadByURL(url, bucket, fileName) {
4444 . on ( 'response' , ( response ) => { response . pause ( ) ; resolve ( response ) ; } ) // Create, then pause and return the stream
4545 . on ( 'error' , ( reject ) ) ;
4646 } ) . then ( ( response ) => { return response . pipe ( file . createWriteStream ( { gzip : true } ) ) ; } )
47+ . then ( ( ) => { return getURL ( fileName , bucket )
48+ . then ( ( result ) => { return { dest :result . url , src :url } ; } ) ;
49+ } )
4750 . catch ( ( error ) => { return { error } ; } ) ; // TODO: Add more detailed error handling. - statusCode etc.
4851}
4952
5053function upload ( url ) {
51- return uploadByURL ( url , storage . bucket ( config . bucket_name ) , path . basename ( url ) ) ;
54+ return uploadByURL ( url , storage . bucket ( config . bucket_name ) , path . basename ( url ) ) ; // Add UUID to filename.
5255}
5356
5457
@@ -61,30 +64,37 @@ function getURL(fileName, bucket) {
6164 return new Promise ( ( resolve , reject ) => {
6265 file . getSignedUrl ( { action :'read' , expires :'01-01-2020' } , function ( err , url ) {
6366 if ( err ) reject ( err ) ;
64- else resolve ( url ) ;
67+ else resolve ( { url} ) ;
6568 } ) ;
6669 } ) ;
6770}
6871
6972function getImage ( fileName , contentType ) {
7073 switch ( contentType ) {
71- case 'application/JSON ' :
74+ case 'application/json ' :
7275 return getURL ( fileName , storage . bucket ( config . bucket_name ) ) ;
7376 /*case 'image/jpeg':
7477 break;
7578 case 'image/png':
7679 break;*/
7780 }
81+ return Promise . reject ( { fileName, contentType, message :`ImageAPI does not currently accept ${ contentType } . Please use json.` } ) ;
7882}
7983
8084/**
8185 * images - /images webhook.
8286 */
8387exports . images = ( req , res ) => {
84- if ( req . method == 'PUT' && ! req . body . urls ) res . status ( 400 ) . json ( { error :'noURLs' , message :'No URLs were provided.' } ) ;
85- if ( req . method == 'GET' && ! req . body . filename ) res . status ( 400 ) . json ( { error :'noImageName' , message :'No Image Name was provided' } ) ;
88+ if ( req . method == 'PUT' && ! req . body . urls ) {
89+ res . status ( 400 ) . json ( { error :'noURLs' , message :'No URLs were provided.' } ) ;
90+ return ;
91+ }
92+ if ( req . method == 'GET' && ! req . query . name ) {
93+ res . status ( 400 ) . json ( { error :'noImageName' , message :'No Image Name was provided' } ) ;
94+ return ;
95+ }
8696 let urls = req . body . urls ;
87- let fileName = req . body . filename ;
97+ let fileName = req . query . name ;
8898
8999 switch ( req . method ) {
90100 case 'PUT' :
@@ -93,18 +103,18 @@ exports.images = (req, res) => {
93103 urls . map ( ( url ) => {
94104 return verifyURL ( url )
95105 // TODO: Check pre-existence of file
96- . then ( ( url ) => { upload ( url ) ; } ) ;
106+ . then ( upload ) ;
97107 } )
98108 )
99109 // TODO: Change status IDs depending on error/s from Promises.
100110 // Promise failure here is rudimentary - need to handle mixed results.
101- . then ( res . status ( 202 ) . json )
102- . catch ( res . status ( 400 ) . json ) ;
111+ . then ( ( result ) => { return res . status ( 202 ) . json ( result ) ; } )
112+ . catch ( ( err ) => { return res . status ( 400 ) . json ( err ) ; } ) ;
103113 break ;
104114 case 'GET' :
105115 getImage ( fileName , req . get ( 'content-type' ) )
106- . then ( res . status ( 202 ) . json )
107- . catch ( res . status ( 400 ) . json ) ;
116+ . then ( ( result ) => { return res . status ( 202 ) . json ( result ) ; } )
117+ . catch ( ( err ) => { return res . status ( 400 ) . json ( err ) ; } ) ;
108118 break ;
109119 }
110120} ;
0 commit comments