@@ -80,14 +80,14 @@ const segments = {
8080
8181 username : {
8282 dynamic : true ,
83- fn : ( core , req ) => req . session . user . username
83+ fn : ( core , session ) => session . user . username
8484 }
8585} ;
8686
8787/*
8888 * Gets a segment value
8989 */
90- const getSegment = ( core , req , seg ) => segments [ seg ] ? segments [ seg ] . fn ( core , req ) : '' ;
90+ const getSegment = ( core , session , seg ) => segments [ seg ] ? segments [ seg ] . fn ( core , session ) : '' ;
9191
9292/*
9393 * Matches a string for segments
@@ -97,16 +97,16 @@ const matchSegments = str => (str.match(/(\{\w+\})/g) || []);
9797/*
9898 * Resolves a string with segments
9999 */
100- const resolveSegments = ( core , req , str ) => matchSegments ( str )
101- . reduce ( ( result , current ) => result . replace ( current , getSegment ( core , req , current . replace ( / ( \{ | \} ) / g, '' ) ) ) , str ) ;
100+ const resolveSegments = ( core , session , str ) => matchSegments ( str )
101+ . reduce ( ( result , current ) => result . replace ( current , getSegment ( core , session , current . replace ( / ( \{ | \} ) / g, '' ) ) ) , str ) ;
102102
103103/*
104104 * Resolves a given file path based on a request
105105 * Will take out segments from the resulting string
106106 * and replace them with a list of defined variables
107107 */
108- const getRealPath = ( core , req , mount , file ) => {
109- const root = resolveSegments ( core , req , mount . attributes . root ) ;
108+ const getRealPath = ( core , session , mount , file ) => {
109+ const root = resolveSegments ( core , session , mount . attributes . root ) ;
110110 const str = file . substr ( mount . root . length - 1 ) ;
111111 return path . join ( root , str ) ;
112112} ;
@@ -118,7 +118,7 @@ const getRealPath = (core, req, mount, file) => {
118118 */
119119module . exports = ( core ) => {
120120 const wrapper = ( method , cb , ...args ) => vfs => ( file , options = { } ) => {
121- const promise = Promise . resolve ( getRealPath ( core , vfs . req , vfs . mount , file ) )
121+ const promise = Promise . resolve ( getRealPath ( core , options . session , vfs . mount , file ) )
122122 . then ( realPath => fs [ method ] ( realPath , ...args ) ) ;
123123
124124 return typeof cb === 'function'
@@ -127,19 +127,17 @@ module.exports = (core) => {
127127 } ;
128128
129129 const crossWrapper = method => ( srcVfs , destVfs ) => ( src , dest , options = { } ) => Promise . resolve ( {
130- realSource : getRealPath ( core , srcVfs . req , srcVfs . mount , src ) ,
131- realDest : getRealPath ( core , destVfs . req , destVfs . mount , dest )
130+ realSource : getRealPath ( core , options . session , srcVfs . mount , src ) ,
131+ realDest : getRealPath ( core , options . session , destVfs . mount , dest )
132132 } )
133133 . then ( ( { realSource, realDest} ) => fs [ method ] ( realSource , realDest ) )
134134 . then ( ( ) => true ) ;
135135
136136 return {
137137 watch : ( mount , callback ) => {
138138 const dest = resolveSegments ( core , {
139- session : {
140- user : {
141- username : '**'
142- }
139+ user : {
140+ username : '**'
143141 }
144142 } , mount . attributes . root ) ;
145143
@@ -168,6 +166,7 @@ module.exports = (core) => {
168166 /**
169167 * Checks if file exists
170168 * @param {String } file The file path from client
169+ * @param {Object } [options={}] Options
171170 * @return {Promise<boolean, Error> }
172171 */
173172 exists : wrapper ( 'access' , promise => {
@@ -178,10 +177,11 @@ module.exports = (core) => {
178177 /**
179178 * Get file statistics
180179 * @param {String } file The file path from client
180+ * @param {Object } [options={}] Options
181181 * @return {Object }
182182 */
183- stat : vfs => file =>
184- Promise . resolve ( getRealPath ( core , vfs . req , vfs . mount , file ) )
183+ stat : vfs => ( file , options = { } ) =>
184+ Promise . resolve ( getRealPath ( core , options . session , vfs . mount , file ) )
185185 . then ( realPath => {
186186 return fs . access ( realPath , fs . F_OK )
187187 . then ( ( ) => createFileIter ( core , path . dirname ( realPath ) , realPath ) ) ;
@@ -190,10 +190,11 @@ module.exports = (core) => {
190190 /**
191191 * Reads directory
192192 * @param {String } root The file path from client
193+ * @param {Object } [options={}] Options
193194 * @return {Object[] }
194195 */
195- readdir : vfs => root =>
196- Promise . resolve ( getRealPath ( core , vfs . req , vfs . mount , root ) )
196+ readdir : vfs => ( root , options ) =>
197+ Promise . resolve ( getRealPath ( core , options . session , vfs . mount , root ) )
197198 . then ( realPath => fs . readdir ( realPath ) . then ( files => ( { realPath, files} ) ) )
198199 . then ( ( { realPath, files} ) => {
199200 const promises = files . map ( f => createFileIter ( core , realPath , root . replace ( / \/ ? $ / , '/' ) + f ) ) ;
@@ -203,10 +204,11 @@ module.exports = (core) => {
203204 /**
204205 * Reads file stream
205206 * @param {String } file The file path from client
207+ * @param {Object } [options={}] Options
206208 * @return {stream.Readable }
207209 */
208210 readfile : vfs => ( file , options = { } ) =>
209- Promise . resolve ( getRealPath ( core , vfs . req , vfs . mount , file ) )
211+ Promise . resolve ( getRealPath ( core , options . session , vfs . mount , file ) )
210212 . then ( realPath => fs . stat ( realPath ) . then ( stat => ( { realPath, stat} ) ) )
211213 . then ( ( { realPath, stat} ) => {
212214 if ( ! stat . isFile ( ) ) {
@@ -224,9 +226,10 @@ module.exports = (core) => {
224226 /**
225227 * Creates directory
226228 * @param {String } file The file path from client
229+ * @param {Object } [options={}] Options
227230 * @return {boolean }
228231 */
229- mkdir : wrapper ( 'mkdir' , ( promise , options ) => {
232+ mkdir : wrapper ( 'mkdir' , ( promise , options = { } ) => {
230233 return promise
231234 . then ( ( ) => true )
232235 . catch ( e => {
@@ -242,13 +245,14 @@ module.exports = (core) => {
242245 * Writes file stream
243246 * @param {String } file The file path from client
244247 * @param {stream.Readable } data The stream
248+ * @param {Object } [options={}] Options
245249 * @return {Promise<boolean, Error> }
246250 */
247- writefile : vfs => ( file , data ) => new Promise ( ( resolve , reject ) => {
251+ writefile : vfs => ( file , data , options = { } ) => new Promise ( ( resolve , reject ) => {
248252 // FIXME: Currently this actually copies the file because
249253 // formidable will put this in a temporary directory.
250254 // It would probably be better to do a "rename()" on local filesystems
251- const realPath = getRealPath ( core , vfs . req , vfs . mount , file ) ;
255+ const realPath = getRealPath ( core , options . session , vfs . mount , file ) ;
252256
253257 const write = ( ) => {
254258 const stream = fs . createWriteStream ( realPath ) ;
@@ -270,6 +274,7 @@ module.exports = (core) => {
270274 * Renames given file or directory
271275 * @param {String } src The source file path from client
272276 * @param {String } dest The destination file path from client
277+ * @param {Object } [options={}] Options
273278 * @return {boolean }
274279 */
275280 rename : crossWrapper ( 'rename' ) ,
@@ -278,24 +283,27 @@ module.exports = (core) => {
278283 * Copies given file or directory
279284 * @param {String } src The source file path from client
280285 * @param {String } dest The destination file path from client
286+ * @param {Object } [options={}] Options
281287 * @return {boolean }
282288 */
283289 copy : crossWrapper ( 'copy' ) ,
284290
285291 /**
286292 * Removes given file or directory
287293 * @param {String } file The file path from client
294+ * @param {Object } [options={}] Options
288295 * @return {boolean }
289296 */
290297 unlink : wrapper ( 'remove' ) ,
291298
292299 /**
293300 * Searches for files and folders
294301 * @param {String } file The file path from client
302+ * @param {Object } [options={}] Options
295303 * @return {boolean }
296304 */
297- search : vfs => ( root , pattern ) =>
298- Promise . resolve ( getRealPath ( core , vfs . req , vfs . mount , root ) )
305+ search : vfs => ( root , pattern , options = { } ) =>
306+ Promise . resolve ( getRealPath ( core , options . session , vfs . mount , root ) )
299307 . then ( realPath => {
300308 return fh . create ( )
301309 . paths ( realPath )
@@ -323,16 +331,18 @@ module.exports = (core) => {
323331 /**
324332 * Touches a file
325333 * @param {String } file The file path from client
334+ * @param {Object } [options={}] Options
326335 * @return {boolean }
327336 */
328337 touch : wrapper ( 'ensureFile' ) ,
329338
330339 /**
331340 * Gets the real filesystem path (internal only)
332341 * @param {String } file The file path from client
342+ * @param {Object } [options={}] Options
333343 * @return {string }
334344 */
335- realpath : vfs => file =>
336- Promise . resolve ( getRealPath ( core , vfs . req , vfs . mount , file ) )
345+ realpath : vfs => ( file , options = { } ) =>
346+ Promise . resolve ( getRealPath ( core , options . session , vfs . mount , file ) )
337347 } ;
338348} ;
0 commit comments