@@ -18,8 +18,8 @@ const parse = require('./utils').parse
1818const fetch = require ( 'node-fetch' )
1919const { promisify } = require ( 'util' )
2020const URL = require ( 'url' )
21- const URI = require ( 'urijs' )
2221const withLock = require ( './lock' )
22+ const utilPath = require ( 'path' )
2323
2424const RDF_MIME_TYPES = new Set ( [
2525 'text/turtle' , // .ttl
@@ -433,19 +433,16 @@ class LDP {
433433 }
434434
435435 async getAvailableUrl ( hostname , containerURI , { slug = uuid . v1 ( ) , extension } ) {
436- const fileName = slug + extension
437- function ensureNotExists ( self , newPath ) {
438- // Verify whether the new path already exists
439- return self . exists ( hostname , newPath ) . then (
440- // If it does, generate another one
441- ( ) => ensureNotExists ( self , URI . joinPaths ( containerURI ,
442- `${ uuid . v1 ( ) . split ( '-' ) [ 0 ] } -${ fileName } ` ) . toString ( ) ) ,
443- // If not, we found an appropriate path
444- ( ) => newPath
445- )
436+ let requestUrl = this . resourceMapper . resolveUrl ( hostname , containerURI )
437+ requestUrl . replace ( / \/ * $ / , '/' )
438+
439+ const { path : containerFilePath } = await this . resourceMapper . mapUrlToFile ( { url : requestUrl } )
440+ let fileName = slug + extension
441+ if ( fs . existsSync ( utilPath . join ( containerFilePath , fileName ) ) ) {
442+ fileName = `${ uuid . v1 ( ) } -${ fileName } `
446443 }
447- const relativePath = await ensureNotExists ( this , URI . joinPaths ( containerURI , fileName ) . toString ( ) )
448- return this . resourceMapper . resolveUrl ( hostname , relativePath )
444+
445+ return requestUrl + fileName
449446 }
450447
451448 getTrustedOrigins ( req ) {
0 commit comments