MCPcopy Index your code
hub / github.com/nodeSolidServer/node-solid-server / checkItemName

Method checkItemName

lib/ldp.mjs:329–357  ·  view source on GitHub ↗
(url)

Source from the content-addressed store, hash-verified

327
328 // check whether a document (or container) has the same name as another document (or container)
329 async checkItemName (url) {
330 let testName, testPath
331 const { hostname, pathname } = this.resourceMapper._parseUrl(url) // (url.url || url)
332 let itemUrl = this.resourceMapper.resolveUrl(hostname, pathname)
333 // make sure the resource being created does not attempt invalid resource creation
334 if (this._containsInvalidSuffixes(itemUrl)) {
335 throw error(400, `${itemUrl} contained reserved suffixes in path`)
336 }
337 const container = itemUrl.endsWith('/')
338 try {
339 const testUrl = container ? itemUrl.slice(0, -1) : itemUrl + '/'
340 const { path: testPath } = await this.resourceMapper.mapUrlToFile({ url: testUrl })
341 testName = container ? fs.lstatSync(testPath).isFile() : fs.lstatSync(testPath).isDirectory()
342 } catch (err) {
343 testName = false
344
345 // item does not exist, check one level up the tree
346 if (itemUrl.endsWith('/')) itemUrl = itemUrl.substring(0, itemUrl.length - 1)
347 itemUrl = itemUrl.substring(0, itemUrl.lastIndexOf('/') + 1)
348 const { pathname } = this.resourceMapper._parseUrl(itemUrl) // (url.url || url)
349 // check not at root
350 if (pathname !== '/') {
351 return await this.checkItemName(itemUrl)
352 }
353 }
354 if (testName) {
355 throw error(409, `${testPath}: Container and resource cannot have the same name in URI`)
356 }
357 }
358
359 async exists (hostname, path, searchIndex = true) {
360 const options = { hostname, path, includeBody: false, searchIndex }

Callers 2

patchHandlerFunction · 0.80
handlerFunction · 0.80

Calls 4

_parseUrlMethod · 0.80
resolveUrlMethod · 0.80
mapUrlToFileMethod · 0.80

Tested by

no test coverage detected