@@ -56,8 +56,7 @@ module.exports = [
5656 config . get ( 'projectAttachmentPathPrefix' ) ,
5757 fileName
5858 ] , '/' )
59- var destinationUri = null ,
60- newAttachment = null
59+ var newAttachment = null
6160
6261 // get presigned Url
6362 var httpClient = util . getHttpClient ( req )
@@ -75,64 +74,60 @@ module.exports = [
7574 isPublic : false
7675 }
7776 } )
78- . then ( ( resp ) => {
79- req . log . debug ( 'Presigned Url resp: ' , JSON . stringify ( resp . data , null , 2 ) )
80- if ( resp . status !== 200 || resp . data . result . status !== 200 ) {
81- return Promise . reject ( new Error ( resp . data . result . message ) )
82- }
83- // store deistination path & url
84- destinationUri = _ . join ( [
85- 's3:/' ,
86- config . get ( 'attachmentsS3Bucket' ) ,
87- filePath
88- ] , '/' )
89- let sourceUri = 's3://' + data . s3Bucket + '/' + data . filePath
90- req . log . debug ( 'Moving s3 file' )
91- return util . s3FileTransfer ( req , sourceUri , destinationUri )
92- } )
93- . then ( ( ) => {
94- // file copied to final destination, create DB record
95- req . log . debug ( 'creating db record' )
96- return models . ProjectAttachment
97- . create ( {
98- projectId : projectId ,
99- createdBy : req . authUser . userId ,
100- updatedBy : req . authUser . userId ,
101- title : data . title ,
102- size : data . size ,
103- category : data . category || null ,
104- description : data . description ,
105- contentType : data . contentType ,
106- filePath : filePath
107- } )
108- } )
109- . then ( ( _newAttachment ) => {
110- newAttachment = _newAttachment . get ( { plain : true } )
111- req . log . debug ( 'New Attachment record: ' , newAttachment )
112- // retrieve download url for the response
113- req . log . debug ( 'retrieving download url' )
114- return httpClient . post ( fileServiceUrl + 'downloadurl' , {
115- param : {
116- filePath : filePath
117- }
77+ . then ( ( resp ) => {
78+ req . log . debug ( 'Presigned Url resp: ' , JSON . stringify ( resp . data , null , 2 ) )
79+ if ( resp . status !== 200 || resp . data . result . status !== 200 ) {
80+ return Promise . reject ( new Error ( resp . data . result . message ) )
81+ }
82+ // store deistination path & url
83+ const destinationUri = `s3://${ config . get ( 'attachmentsS3Bucket' ) } /${ filePath } `
84+ const sourceUri = `s3://${ data . s3Bucket } /${ data . filePath } `
85+ req . log . debug ( 'Moving s3 file' )
86+ return util . s3FileTransfer ( req , sourceUri , destinationUri )
87+ } )
88+ . then ( ( ) => {
89+ // file copied to final destination, create DB record
90+ req . log . debug ( 'creating db record' )
91+ return models . ProjectAttachment
92+ . create ( {
93+ projectId : projectId ,
94+ createdBy : req . authUser . userId ,
95+ updatedBy : req . authUser . userId ,
96+ title : data . title ,
97+ size : data . size ,
98+ category : data . category || null ,
99+ description : data . description ,
100+ contentType : data . contentType ,
101+ filePath : filePath
118102 } )
119- } )
120- . then ( ( resp ) => {
121- req . log . debug ( 'Retreiving Presigned Url resp: ' , JSON . stringify ( resp . data , null , 2 ) )
122- if ( resp . status !== 200 || resp . data . result . status !== 200 ) {
123- return Promise . reject ( new Error ( "Unable to fetch pre-signed url" ) )
124- }
125- let response = _ . cloneDeep ( newAttachment )
126- response = _ . omit ( response , [ 'filePath' , 'deletedAt' ] )
103+ } )
104+ . then ( ( _newAttachment ) => {
105+ newAttachment = _newAttachment . get ( { plain : true } )
106+ req . log . debug ( 'New Attachment record: ' , newAttachment )
107+ // retrieve download url for the response
108+ req . log . debug ( 'retrieving download url' )
109+ return httpClient . post ( fileServiceUrl + 'downloadurl' , {
110+ param : {
111+ filePath : filePath
112+ }
113+ } )
114+ } )
115+ . then ( ( resp ) => {
116+ req . log . debug ( 'Retreiving Presigned Url resp: ' , JSON . stringify ( resp . data , null , 2 ) )
117+ if ( resp . status !== 200 || resp . data . result . status !== 200 ) {
118+ return Promise . reject ( new Error ( "Unable to fetch pre-signed url" ) )
119+ }
120+ let response = _ . cloneDeep ( newAttachment )
121+ response = _ . omit ( response , [ 'filePath' , 'deletedAt' ] )
127122
128- response . url = resp . data . result . content . preSignedURL
129- res . status ( 201 ) . json ( util . wrapResponse ( req . id , response ) )
130- } )
131- . catch ( function ( err ) {
132- req . log . error ( "Error adding attachment" , err )
133- err . status = err . status || 500
134- next ( err )
135- } )
123+ response . url = resp . data . result . content . preSignedURL
124+ res . status ( 201 ) . json ( util . wrapResponse ( req . id , response ) )
125+ } )
126+ . catch ( function ( err ) {
127+ req . log . error ( "Error adding attachment" , err )
128+ err . status = err . status || 500
129+ next ( err )
130+ } )
136131
137132 }
138133]
0 commit comments