@@ -18,8 +18,8 @@ const parse = require('./utils').parse
18
18
const fetch = require ( 'node-fetch' )
19
19
const { promisify } = require ( 'util' )
20
20
const URL = require ( 'url' )
21
- const URI = require ( 'urijs' )
22
21
const withLock = require ( './lock' )
22
+ const utilPath = require ( 'path' )
23
23
24
24
const RDF_MIME_TYPES = new Set ( [
25
25
'text/turtle' , // .ttl
@@ -433,19 +433,16 @@ class LDP {
433
433
}
434
434
435
435
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 } `
446
443
}
447
- const relativePath = await ensureNotExists ( this , URI . joinPaths ( containerURI , fileName ) . toString ( ) )
448
- return this . resourceMapper . resolveUrl ( hostname , relativePath )
444
+
445
+ return requestUrl + fileName
449
446
}
450
447
451
448
getTrustedOrigins ( req ) {
0 commit comments