1- const path = require ( 'path' )
1+ const { join , dirname } = require ( 'path' )
22const url = require ( 'url' )
33const fs = require ( 'fs' )
44const $rdf = require ( 'rdflib' )
@@ -90,18 +90,14 @@ class LDP {
9090 } )
9191 }
9292
93- createReadStream ( filename , start , end ) {
94- if ( start && end ) {
95- return fs . createReadStream ( filename , { 'start' : start , 'end' : end } )
96- } else {
97- return fs . createReadStream ( filename )
98- }
93+ createReadStream ( path , start , end ) {
94+ return fs . createReadStream ( path , start && end && { start, end} )
9995 }
10096
10197 async readResource ( url ) {
10298 try {
10399 const { path } = await this . resourceMapper . mapUrlToFile ( { url } )
104- return await promisify ( fs . readFile ) ( path , { ' encoding' : 'utf8' } )
100+ return await promisify ( fs . readFile ) ( path , { encoding : 'utf8' } )
105101 } catch ( err ) {
106102 throw error ( err . status , err . message )
107103 }
@@ -114,7 +110,7 @@ class LDP {
114110 return this . readResource ( url + this . suffixMeta )
115111 }
116112
117- async listContainer ( filename , reqUri , containerData , hostname ) {
113+ async listContainer ( container , reqUri , containerData , hostname ) {
118114 const resourceGraph = $rdf . graph ( )
119115
120116 try {
@@ -126,14 +122,14 @@ class LDP {
126122
127123 try {
128124 // add container stats
129- await ldpContainer . addContainerStats ( this , reqUri , filename , resourceGraph )
125+ await ldpContainer . addContainerStats ( this , reqUri , container , resourceGraph )
130126 // read directory
131- const files = await ldpContainer . readdir ( filename )
127+ const files = await ldpContainer . readdir ( container )
132128 // iterate through all the files
133129 await Promise . all ( files . map ( async file => {
134130 const { url : fileUri } = await this . resourceMapper . mapFileToUrl (
135- { path : path . join ( filename , file ) , hostname } )
136- return await ldpContainer . addFile ( this , resourceGraph , reqUri , fileUri , filename , file )
131+ { path : join ( container , file ) , hostname } )
132+ return await ldpContainer . addFile ( this , resourceGraph , reqUri , fileUri , container , file )
137133 } ) )
138134 } catch ( err ) {
139135 throw error ( 500 , "Can't list container" )
@@ -169,7 +165,7 @@ class LDP {
169165 let originalPath = resourcePath
170166 if ( container ) {
171167 // Create directory by an LDP PUT to the container's .meta resource
172- resourcePath = path . join ( originalPath , ldp . suffixMeta )
168+ resourcePath = join ( originalPath , ldp . suffixMeta )
173169 if ( originalPath && ! originalPath . endsWith ( '/' ) ) {
174170 originalPath += '/'
175171 }
@@ -241,8 +237,8 @@ class LDP {
241237 }
242238
243239 // Second, create the enclosing directory, if necessary
244- const { path : filePath } = await this . resourceMapper . mapUrlToFile ( { url, contentType, createIfNotExists : true } )
245- const dirName = path . dirname ( filePath )
240+ const { path } = await this . resourceMapper . mapUrlToFile ( { url, contentType, createIfNotExists : true } )
241+ const dirName = dirname ( path )
246242 try {
247243 await promisify ( mkdirp ) ( dirName )
248244 } catch ( err ) {
@@ -253,12 +249,12 @@ class LDP {
253249
254250 // Directory created, now write the file
255251 return await new Promise ( ( resolve , reject ) => {
256- const file = stream . pipe ( fs . createWriteStream ( filePath ) )
252+ const file = stream . pipe ( fs . createWriteStream ( path ) )
257253 file . on ( 'error' , function ( ) {
258254 reject ( error ( 500 , 'Error writing data' ) )
259255 } )
260256 file . on ( 'finish' , function ( ) {
261- debug . handlers ( 'PUT -- Wrote data to: ' + filePath )
257+ debug . handlers ( 'PUT -- Wrote data to: ' + path )
262258 resolve ( )
263259 } )
264260 } )
@@ -335,61 +331,58 @@ class LDP {
335331 }
336332
337333 async get ( options , searchIndex = true ) {
338- let filename , contentType , stats
334+ let path , contentType , stats
339335 try {
340- ( { path : filename , contentType } = await this . resourceMapper . mapUrlToFile ( { url : options , searchIndex } ) )
341- stats = await this . stat ( filename )
336+ ( { path, contentType } = await this . resourceMapper . mapUrlToFile ( { url : options , searchIndex } ) )
337+ stats = await this . stat ( path )
342338 } catch ( err ) {
343339 throw error ( 404 , 'Can\'t find file requested: ' + options )
344340 }
345341
346342 // Just return, since resource exists
347343 if ( ! options . includeBody ) {
348- return { ' stream' : stats , ' contentType' : contentType , ' container' : stats . isDirectory ( ) }
344+ return { stream : stats , contentType, container : stats . isDirectory ( ) }
349345 }
350346
351347 // Found a container
352348 if ( stats . isDirectory ( ) ) {
353349 const { url : absContainerUri } = await this . resourceMapper
354- . mapFileToUrl ( { path : filename , hostname : options . hostname } )
350+ . mapFileToUrl ( { path, hostname : options . hostname } )
355351 const metaFile = await this . readContainerMeta ( absContainerUri )
356352 . catch ( ( ) => '' ) // Default to an empty meta file if it is missing
357353 let data
358354 try {
359- data = await this . listContainer ( filename , absContainerUri , metaFile , options . hostname )
355+ data = await this . listContainer ( path , absContainerUri , metaFile , options . hostname )
360356 } catch ( err ) {
361357 debug . handlers ( 'GET container -- Read error:' + err . message )
362358 throw err
363359 }
364360 const stream = stringToStream ( data )
365361 // TODO 'text/turtle' is fixed, should be contentType instead
366362 // This forces one more translation turtle -> desired
367- return { ' stream' : stream , ' contentType' : 'text/turtle' , ' container' : true }
363+ return { stream, contentType : 'text/turtle' , container : true }
368364 } else {
369365 let stream
370- let chunksize
371- let contentRange
366+ let chunksize , contentRange , start , end
372367 if ( options . range ) {
373- const total = fs . statSync ( filename ) . size
368+ const total = fs . statSync ( path ) . size
374369 const parts = options . range . replace ( / b y t e s = / , '' ) . split ( '-' )
375370 const partialstart = parts [ 0 ]
376371 const partialend = parts [ 1 ]
377- const start = parseInt ( partialstart , 10 )
378- const end = partialend ? parseInt ( partialend , 10 ) : total - 1
372+ start = parseInt ( partialstart , 10 )
373+ end = partialend ? parseInt ( partialend , 10 ) : total - 1
379374 chunksize = ( end - start ) + 1
380375 contentRange = 'bytes ' + start + '-' + end + '/' + total
381- stream = this . createReadStream ( filename , start , end )
382- } else {
383- stream = this . createReadStream ( filename )
384376 }
377+ stream = this . createReadStream ( path )
385378 return new Promise ( ( resolve , reject ) => {
386379 stream
387380 . on ( 'error' , function ( err ) {
388- debug . handlers ( `GET -- error reading ${ filename } : ${ err . message } ` )
381+ debug . handlers ( `GET -- error reading ${ path } : ${ err . message } ` )
389382 return reject ( error ( err , "Can't read file " + err ) )
390383 } )
391384 . on ( 'open' , function ( ) {
392- debug . handlers ( `GET -- Reading ${ filename } ` )
385+ debug . handlers ( `GET -- Reading ${ path } ` )
393386 return resolve ( { stream, contentType, container : false , contentRange, chunksize } )
394387 } )
395388 } )
@@ -398,19 +391,19 @@ class LDP {
398391
399392 async delete ( url ) {
400393 // First check if the path points to a valid file
401- let filePath , stats
394+ let path , stats
402395 try {
403- ( { path : filePath } = await this . resourceMapper . mapUrlToFile ( { url } ) )
404- stats = await this . stat ( filePath )
396+ ( { path } = await this . resourceMapper . mapUrlToFile ( { url } ) )
397+ stats = await this . stat ( path )
405398 } catch ( err ) {
406399 throw error ( 404 , "Can't find " + err )
407400 }
408401
409402 // If so, delete the directory or file
410403 if ( stats . isDirectory ( ) ) {
411- return this . deleteContainer ( filePath )
404+ return this . deleteContainer ( path )
412405 } else {
413- return this . deleteResource ( filePath )
406+ return this . deleteResource ( path )
414407 }
415408 }
416409
@@ -440,28 +433,28 @@ class LDP {
440433 }
441434 }
442435
443- async deleteResource ( filename ) {
436+ async deleteResource ( path ) {
444437 try {
445- return promisify ( fs . unlink ) ( filename )
438+ return promisify ( fs . unlink ) ( path )
446439 } catch ( err ) {
447440 debug . container ( 'DELETE -- unlink() error: ' + err )
448441 throw error ( err , 'Failed to delete resource' )
449442 }
450443 }
451444
452445 getAvailablePath ( host , containerURI , { slug = uuid . v1 ( ) , extension } ) {
453- const filename = slug + extension
446+ const path = slug + extension
454447 function ensureNotExists ( self , newPath ) {
455448 // Verify whether the new path already exists
456449 return self . exists ( host , newPath ) . then (
457450 // If it does, generate another one
458451 ( ) => ensureNotExists ( self , URI . joinPaths ( containerURI ,
459- `${ uuid . v1 ( ) . split ( '-' ) [ 0 ] } -${ filename } ` ) . toString ( ) ) ,
452+ `${ uuid . v1 ( ) . split ( '-' ) [ 0 ] } -${ path } ` ) . toString ( ) ) ,
460453 // If not, we found an appropriate path
461454 ( ) => newPath
462455 )
463456 }
464- return ensureNotExists ( this , URI . joinPaths ( containerURI , filename ) . toString ( ) )
457+ return ensureNotExists ( this , URI . joinPaths ( containerURI , path ) . toString ( ) )
465458 }
466459
467460 getTrustedOrigins ( req ) {
0 commit comments