File tree Expand file tree Collapse file tree 3 files changed +48
-0
lines changed Expand file tree Collapse file tree 3 files changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -19,5 +19,6 @@ module.exports = () => {
19
19
Authorizer . setPolicy ( 'project.addAttachment' , projectEdit ) ;
20
20
Authorizer . setPolicy ( 'project.updateAttachment' , projectEdit ) ;
21
21
Authorizer . setPolicy ( 'project.removeAttachment' , projectEdit ) ;
22
+ Authorizer . setPolicy ( 'project.downloadAttachment' , projectView ) ;
22
23
Authorizer . setPolicy ( 'project.updateMember' , projectEdit ) ;
23
24
} ;
Original file line number Diff line number Diff line change
1
+
2
+ import _ from 'lodash' ;
3
+ import { middleware as tcMiddleware } from 'tc-core-library-js' ;
4
+ import models from '../../models' ;
5
+ import util from '../../util' ;
6
+
7
+ /**
8
+ * API to download a project attachment.
9
+ *
10
+ */
11
+
12
+ const permissions = tcMiddleware . permissions ;
13
+
14
+ module . exports = [
15
+ permissions ( 'project.downloadAttachment' ) ,
16
+ ( req , res , next ) => {
17
+ const projectId = _ . parseInt ( req . params . projectId ) ;
18
+ const attachmentId = _ . parseInt ( req . params . id ) ;
19
+
20
+ models . ProjectAttachment . findOne (
21
+ {
22
+ where : {
23
+ id : attachmentId ,
24
+ projectId,
25
+ } ,
26
+ } )
27
+ . then ( ( attachment ) => {
28
+ if ( ! attachment ) {
29
+ const err = new Error ( 'Record not found' ) ;
30
+ err . status = 404 ;
31
+ return Promise . reject ( err ) ;
32
+ }
33
+ return util . getFileDownloadUrl ( req , attachment . filePath ) ;
34
+ } )
35
+ . then ( ( result ) => {
36
+ const url = result [ 1 ] ;
37
+ res . status ( 200 ) . json ( util . wrapResponse ( req . id , { url } ) ) ;
38
+ } )
39
+ . catch ( ( error ) => {
40
+ req . log . error ( 'Error fetching attachment' , error ) ;
41
+ const rerr = error ;
42
+ rerr . status = rerr . status || 500 ;
43
+ next ( rerr ) ;
44
+ } ) ;
45
+ } ,
46
+ ] ;
Original file line number Diff line number Diff line change @@ -48,6 +48,7 @@ router.route('/v4/projects/:projectId(\\d+)/members/:id(\\d+)')
48
48
router . route ( '/v4/projects/:projectId(\\d+)/attachments' )
49
49
. post ( require ( './attachments/create' ) ) ;
50
50
router . route ( '/v4/projects/:projectId(\\d+)/attachments/:id(\\d+)' )
51
+ . get ( require ( './attachments/download' ) )
51
52
. patch ( require ( './attachments/update' ) )
52
53
. delete ( require ( './attachments/delete' ) ) ;
53
54
You can’t perform that action at this time.
0 commit comments