@@ -43,10 +43,10 @@ const {
43
43
44
44
const respondNumber = result => typeof result === 'number' ? result : - 1 ;
45
45
const respondBoolean = result => typeof result === 'boolean' ? result : ! ! result ;
46
- const requestPath = req => ( [ sanitize ( req . fields . path ) ] ) ;
47
- const requestSearch = req => ( [ sanitize ( req . fields . root ) , req . fields . pattern ] ) ;
48
- const requestCross = req => ( [ sanitize ( req . fields . from ) , sanitize ( req . fields . to ) ] ) ;
49
- const requestFile = req => ( [ sanitize ( req . fields . path ) , streamFromRequest ( req ) ] ) ;
46
+ const requestPath = req => [ sanitize ( req . fields . path ) ] ;
47
+ const requestSearch = req => [ sanitize ( req . fields . root ) , req . fields . pattern ] ;
48
+ const requestFile = req => [ sanitize ( req . fields . path ) , ( ) => streamFromRequest ( req ) ] ;
49
+ const requestCross = req => [ sanitize ( req . fields . from ) , sanitize ( req . fields . to ) ] ;
50
50
51
51
/*
52
52
* Parses the range request headers
@@ -65,7 +65,10 @@ const onDone = (req, res) => {
65
65
if ( req . files ) {
66
66
for ( let fieldname in req . files ) {
67
67
try {
68
- fs . removeSync ( req . files [ fieldname ] . path ) ;
68
+ const n = req . files [ fieldname ] . path ;
69
+ if ( fs . existsSync ( n ) ) {
70
+ fs . removeSync ( n ) ;
71
+ }
69
72
} catch ( e ) {
70
73
console . warn ( 'Failed to unlink temporary file' , e ) ;
71
74
}
@@ -77,26 +80,18 @@ const onDone = (req, res) => {
77
80
* Wraps a vfs adapter request
78
81
*/
79
82
const wrapper = fn => ( req , res , next ) => fn ( req , res )
80
- . then ( result => {
83
+ . then ( result => new Promise ( ( resolve , reject ) => {
81
84
if ( result instanceof Stream ) {
82
- result . on ( 'error' , error => {
83
- next ( error ) ;
84
- } ) ;
85
-
86
- result . on ( 'end' , ( ) => {
87
- onDone ( req , res ) ;
88
- } ) ;
89
-
85
+ result . once ( 'error' , reject ) ;
86
+ result . once ( 'end' , resolve ) ;
90
87
result . pipe ( res ) ;
91
88
} else {
92
89
res . json ( result ) ;
93
- onDone ( req , res ) ;
90
+ resolve ( ) ;
94
91
}
95
- } )
96
- . catch ( error => {
97
- next ( error ) ;
98
- onDone ( req , res ) ;
99
- } ) ;
92
+ } ) )
93
+ . catch ( error => next ( error ) )
94
+ . finally ( ( ) => onDone ( req , res ) ) ;
100
95
101
96
/**
102
97
* Creates the middleware
@@ -147,27 +142,31 @@ const createOptions = req => {
147
142
// Standard request with only a target
148
143
const createRequestFactory = findMountpoint => ( getter , method , readOnly , respond ) => async ( req , res ) => {
149
144
const options = createOptions ( req ) ;
150
- const args = [ ...getter ( req , res ) , options ] ;
145
+ const [ target , ...rest ] = getter ( req , res ) ;
146
+
147
+ const found = await findMountpoint ( target ) ;
148
+ const attributes = found . mount . attributes || { } ;
149
+ const strict = attributes . strictGroups !== false ;
151
150
152
- const found = await findMountpoint ( args [ 0 ] ) ;
153
151
if ( method === 'search' ) {
154
- if ( found . mount . attributes && found . mount . attributes . searchable === false ) {
152
+ if ( attributes . searchable === false ) {
155
153
return [ ] ;
156
154
}
157
155
}
158
156
159
- const { attributes} = found . mount ;
160
- const strict = attributes . strictGroups !== false ;
161
- const ranges = ( ! attributes . adapter || attributes . adapter === 'system' ) || attributes . ranges === true ;
162
- const vfsMethodWrapper = m => found . adapter [ m ]
163
- ? found . adapter [ m ] ( found ) ( ...args )
164
- : Promise . reject ( new Error ( `Adapter does not support ${ m } ` ) ) ;
165
- const readstat = ( ) => vfsMethodWrapper ( 'stat' ) . catch ( ( ) => ( { } ) ) ;
166
157
await checkMountpointPermission ( req , res , method , readOnly , strict ) ( found ) ;
167
158
159
+ const vfsMethodWrapper = m => {
160
+ const args = [ target , ...rest . map ( r => typeof r === 'function' ? r ( ) : r ) , options ] ;
161
+ return found . adapter [ m ]
162
+ ? found . adapter [ m ] ( found ) ( ...args )
163
+ : Promise . reject ( new Error ( `Adapter does not support ${ m } ` ) ) ;
164
+ } ;
165
+
168
166
const result = await vfsMethodWrapper ( method ) ;
169
167
if ( method === 'readfile' ) {
170
- const stat = await readstat ( ) ;
168
+ const ranges = ( ! attributes . adapter || attributes . adapter === 'system' ) || attributes . ranges === true ;
169
+ const stat = await vfsMethodWrapper ( 'stat' ) . catch ( ( ) => ( { } ) ) ;
171
170
172
171
if ( ranges && options . range ) {
173
172
try {
@@ -192,7 +191,7 @@ const createRequestFactory = findMountpoint => (getter, method, readOnly, respon
192
191
}
193
192
194
193
if ( options . download ) {
195
- const filename = encodeURIComponent ( path . basename ( args [ 0 ] ) ) ;
194
+ const filename = encodeURIComponent ( path . basename ( target ) ) ;
196
195
res . append ( 'Content-Disposition' , `attachment; filename*=utf-8''${ filename } ` ) ;
197
196
}
198
197
}
0 commit comments