Skip to content

Commit

Permalink
only for containers
Browse files Browse the repository at this point in the history
  • Loading branch information
bourgeoa committed Mar 23, 2024
1 parent 51163e9 commit d56cdf0
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 362 deletions.
33 changes: 12 additions & 21 deletions lib/ldp.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,23 +145,24 @@ class LDP {

const ldp = this
debug.handlers('POST -- On parent: ' + containerPath)
// prepare slug
if (container) {
// Containers should not receive an extension
extension = ''
}
// pepare slug
if (slug) {
if (this.isAuxResource(slug, extension)) throw error(403, 'POST is not allowed for auxiliary resources')
if (this._containsInvalidSuffixes(slug)) {
throw error(400, `${slug} is an invalid file path`)
}
slug = decodeURIComponent(slug)

if (container) {
// the name of a container cannot be a valid auxiliary resource document
if (this._containsInvalidSuffixes(slug + '/')) slug = slug.split('.')[0]
} else if (this.isAuxResource(slug, extension)) throw error(403, 'POST is not allowed for auxiliary resources')

if (slug.match(/\/|\||:/)) {
throw error(400, 'The name of new file POSTed may not contain : | or /')
}
}

// Containers should not receive an extension
if (container) {
extension = ''
}

// always return a valid URL.
const resourceUrl = await ldp.getAvailableUrl(hostname, containerPath, { slug, extension, container })
debug.handlers('POST -- Will create at: ' + resourceUrl)
Expand Down Expand Up @@ -338,17 +339,7 @@ class LDP {
* @returns {boolean} true is fail - if the path contains reserved suffixes
*/
_containsInvalidSuffixes (path) {
// if it is a container, no suffix so remove last slash
if (path.endsWith('/')) {
path = path.slice(0, -1)
} else {
// this is a resource, so it either ends with an extension, or just text
const lastFullStop = path.lastIndexOf('.')
if (lastFullStop !== -1) { // contains at least one full stop
path = path.slice(0, lastFullStop)
}
}
return AUXILIARY_RESOURCES.some(suffix => path.includes(suffix))
return AUXILIARY_RESOURCES.some(suffix => path.endsWith(suffix + '/'))
}

// check whether a document (or container) has the same name as another document (or container)
Expand Down
Loading

0 comments on commit d56cdf0

Please sign in to comment.