Skip to content

Commit 8afcb23

Browse files
authored
Merge pull request #1292 from bourgeoa/master
PR # 1282 with minimized rewrite on ressource-mapper.js
2 parents 17f2b68 + 927dd53 commit 8afcb23

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

lib/handlers/get.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ async function handler (req, res, next) {
4545

4646
let ret
4747
try {
48-
ret = await ldp.get(options)
48+
ret = await ldp.get(options, req.accepts('html'))
4949
} catch (err) {
5050
// use globHandler if magic is detected
5151
if (err.status === 404 && glob.hasMagic(path)) {

lib/ldp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ class LDP {
437437
requestUrl = requestUrl.replace(/\/*$/, '/')
438438

439439
const { path: containerFilePath } = await this.resourceMapper.mapUrlToFile({ url: requestUrl })
440-
let fileName = slug + extension
440+
let fileName = slug.endsWith(extension) || slug.endsWith(this.suffixAcl) || slug.endsWith(this.suffixMeta) ? slug : slug + extension
441441
if (await promisify(fs.exists)(utilPath.join(containerFilePath, fileName))) {
442442
fileName = `${uuid.v1()}-${fileName}`
443443
}

lib/resource-mapper.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const HTTPError = require('./http-error')
77

88
/*
99
* A ResourceMapper maintains the mapping between HTTP URLs and server filenames,
10-
* following the principles of the sweet spot discussed in
10+
* following the principles of the "sweet spot" discussed in
1111
* https://www.w3.org/DesignIssues/HTTPFilenameMapping.html
1212
*
1313
* This class implements this mapping in a single place
@@ -91,9 +91,10 @@ class ResourceMapper {
9191
if (filePath.indexOf('/..') >= 0) {
9292
throw new Error('Disallowed /.. segment in URL')
9393
}
94+
let isFolder = filePath.endsWith('/')
9495
let isIndex = searchIndex && filePath.endsWith('/')
9596

96-
// Create the path for a new file
97+
// Create the path for a new ressource
9798
let path
9899
if (createIfNotExists) {
99100
path = filePath
@@ -105,7 +106,7 @@ class ResourceMapper {
105106
path += this._indexFilename
106107
}
107108
// If the extension is not correct for the content type, append the correct extension
108-
if (searchIndex && this._getContentTypeByExtension(path) !== contentType) {
109+
if (!isFolder && this._getContentTypeByExtension(path) !== contentType) {
109110
path += `$${contentType in extensions ? `.${extensions[contentType][0]}` : '.unknown'}`
110111
}
111112
// Determine the path of an existing file
@@ -116,13 +117,13 @@ class ResourceMapper {
116117

117118
// Find a file with the same name (minus the dollar extension)
118119
let match = ''
119-
if (searchIndex) {
120+
if (match === '') { // always true to keep indentation
120121
const files = await this._readdir(folder)
121122
// Search for files with the same name (disregarding a dollar extension)
122-
if (!isIndex) {
123+
if (!isFolder) {
123124
match = files.find(f => this._removeDollarExtension(f) === filename)
124125
// Check if the index file exists
125-
} else if (files.includes(this._indexFilename)) {
126+
} else if (searchIndex && files.includes(this._indexFilename)) {
126127
match = this._indexFilename
127128
}
128129
}
@@ -137,7 +138,6 @@ class ResourceMapper {
137138
path = `${folder}${match}`
138139
contentType = this._getContentTypeByExtension(match)
139140
}
140-
141141
return { path, contentType: contentType || this._defaultContentType }
142142
}
143143

0 commit comments

Comments
 (0)