@@ -11,6 +11,7 @@ import Path from 'path';
11
11
import { middleware as tcMiddleware } from 'tc-core-library-js' ;
12
12
import models from '../../models' ;
13
13
import util from '../../util' ;
14
+ import { getDownloadUrl } from '../../services/fileService' ;
14
15
import { EVENT , RESOURCES , ATTACHMENT_TYPES } from '../../constants' ;
15
16
16
17
const permissions = tcMiddleware . permissions ;
@@ -60,14 +61,6 @@ module.exports = [
60
61
] , '/' ) ;
61
62
let newAttachment = null ;
62
63
63
- const httpClient = util . getHttpClient ( req ) ;
64
- // get presigned Url
65
- httpClient . defaults . headers . common . Authorization = req . headers . authorization ;
66
- let fileServiceUrl = config . get ( 'fileServiceEndpoint' ) ;
67
- if ( fileServiceUrl . substr ( - 1 ) !== '/' ) {
68
- fileServiceUrl += '/' ;
69
- }
70
-
71
64
const sourceBucket = data . s3Bucket ;
72
65
const sourceKey = data . path ;
73
66
const destBucket = config . get ( 'attachmentsS3Bucket' ) ;
@@ -137,35 +130,29 @@ module.exports = [
137
130
if ( process . env . NODE_ENV !== 'development' || config . get ( 'enableFileUpload' ) === 'true' ) {
138
131
// retrieve download url for the response
139
132
req . log . debug ( 'retrieving download url' ) ;
140
- return httpClient . post ( `${ fileServiceUrl } downloadurl` , {
141
- param : {
142
- filePath : path ,
143
- } ,
144
- } ) ;
133
+ return getDownloadUrl ( destBucket , path ) ;
145
134
}
146
135
return Promise . resolve ( ) ;
147
- } ) . then ( ( resp ) => {
148
- if ( process . env . NODE_ENV !== 'development' || config . get ( 'enableFileUpload' ) === 'true' ) {
149
- req . log . debug ( 'Retreiving Presigned Url resp: ' , JSON . stringify ( resp . data ) ) ;
150
- return new Promise ( ( accept , reject ) => {
151
- if ( resp . status !== 200 || resp . data . result . status !== 200 ) {
152
- reject ( new Error ( 'Unable to fetch pre-signed url' ) ) ;
153
- } else {
154
- let response = _ . cloneDeep ( newAttachment ) ;
155
- response = _ . omit ( response , [ 'path' , 'deletedAt' ] ) ;
136
+ } ) . then ( ( url ) => {
137
+ if (
138
+ process . env . NODE_ENV !== 'development' ||
139
+ config . get ( 'enableFileUpload' ) === 'true'
140
+ ) {
141
+ req . log . debug ( 'Retreiving Presigned Url resp: ' , url ) ;
142
+ let response = _ . cloneDeep ( newAttachment ) ;
143
+ response = _ . omit ( response , [ 'path' , 'deletedAt' ] ) ;
156
144
157
- response . downloadUrl = resp . data . result . content . preSignedURL ;
145
+ response . downloadUrl = url ;
158
146
159
- // emit the event
160
- util . sendResourceToKafkaBus (
161
- req ,
162
- EVENT . ROUTING_KEY . PROJECT_ATTACHMENT_ADDED ,
163
- RESOURCES . ATTACHMENT ,
164
- newAttachment ) ;
165
- res . status ( 201 ) . json ( response ) ;
166
- accept ( ) ;
167
- }
168
- } ) ;
147
+ // emit the event
148
+ util . sendResourceToKafkaBus (
149
+ req ,
150
+ EVENT . ROUTING_KEY . PROJECT_ATTACHMENT_ADDED ,
151
+ RESOURCES . ATTACHMENT ,
152
+ newAttachment ,
153
+ ) ;
154
+ res . status ( 201 ) . json ( response ) ;
155
+ return Promise . resolve ( ) ;
169
156
}
170
157
let response = _ . cloneDeep ( newAttachment ) ;
171
158
response = _ . omit ( response , [ 'path' , 'deletedAt' ] ) ;
0 commit comments