File tree 4 files changed +52
-0
lines changed
4 files changed +52
-0
lines changed Original file line number Diff line number Diff line change 14
14
export class FilesAdapter {
15
15
createFile ( config , filename , data ) { }
16
16
17
+ deleteFile ( config , filename ) { }
18
+
17
19
getFileData ( config , filename ) { }
18
20
19
21
getFileLocation ( config , filename ) { }
Original file line number Diff line number Diff line change @@ -20,6 +20,17 @@ export class GridStoreAdapter extends FilesAdapter {
20
20
} ) ;
21
21
}
22
22
23
+ deleteFile ( config , filename ) {
24
+ return config . database . connect ( ) . then ( ( ) => {
25
+ let gridStore = new GridStore ( config . database . db , filename , 'r' ) ;
26
+ return gridStore . open ( ) ;
27
+ } ) . then ( ( gridStore ) => {
28
+ return gridStore . unlink ( ) ;
29
+ } ) . then ( ( gridStore ) => {
30
+ return gridStore . close ( ) ;
31
+ } ) ;
32
+ }
33
+
23
34
getFileData ( config , filename ) {
24
35
return config . database . connect ( ) . then ( ( ) => {
25
36
return GridStore . exist ( config . database . db , filename ) ;
Original file line number Diff line number Diff line change @@ -56,6 +56,20 @@ export class S3Adapter extends FilesAdapter {
56
56
} ) ;
57
57
}
58
58
59
+ deleteFile ( config , filename ) {
60
+ return new Promise ( ( resolve , reject ) => {
61
+ let params = {
62
+ Key : this . _bucketPrefix + filename ,
63
+ } ;
64
+ this . _s3Client . deleteObject ( params , ( err , data ) => {
65
+ if ( err !== null ) {
66
+ return reject ( err ) ;
67
+ }
68
+ resolve ( data ) ;
69
+ } ) ;
70
+ } ) ;
71
+ }
72
+
59
73
// Search for and return a file if found by filename
60
74
// Returns a promise that succeeds with the buffer result from S3
61
75
getFileData ( config , filename ) {
Original file line number Diff line number Diff line change @@ -74,6 +74,25 @@ export class FilesController {
74
74
} ;
75
75
}
76
76
77
+ deleteHandler ( ) {
78
+ return ( req , res , next ) => {
79
+ if ( ! req . auth . isMaster ) {
80
+ next ( new Parse . Error ( Parse . Error . OPERATION_FORBIDDEN ,
81
+ 'Master key required for file deletion.' ) ) ;
82
+ return ;
83
+ }
84
+
85
+ this . _filesAdapter . deleteFile ( req . config , req . params . filename ) . then ( ( ) => {
86
+ res . status ( 200 ) ;
87
+ // TODO: return useful JSON here?
88
+ res . end ( ) ;
89
+ } ) . catch ( ( error ) => {
90
+ next ( new Parse . Error ( Parse . Error . FILE_DELETE_ERROR ,
91
+ 'Could not delete file.' ) ) ;
92
+ } ) ;
93
+ } ;
94
+ }
95
+
77
96
/**
78
97
* Find file references in REST-format object and adds the url key
79
98
* with the current mount point and app id.
@@ -119,6 +138,12 @@ export class FilesController {
119
138
this . createHandler ( )
120
139
) ;
121
140
141
+ router . delete ( '/files/:filename' ,
142
+ Middlewares . allowCrossDomain ,
143
+ Middlewares . handleParseHeaders ,
144
+ this . deleteHandler ( )
145
+ ) ;
146
+
122
147
return router ;
123
148
}
124
149
}
You can’t perform that action at this time.
0 commit comments