Skip to content

Commit 7182901

Browse files
committed
Refactor to use urls in case of encoding errors
1 parent 7ff0edd commit 7182901

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

lib/ldp.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ const parse = require('./utils').parse
1818
const fetch = require('node-fetch')
1919
const { promisify } = require('util')
2020
const URL = require('url')
21-
const URI = require('urijs')
2221
const withLock = require('./lock')
22+
const utilPath = require('path')
2323

2424
const 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

Comments
 (0)