Skip to content

Commit 567b042

Browse files
authored
Merge pull request #1109 from solid/fix/exclude-acl-in-url-mapping
Removes acl files from list of files to match when mapping url to file
2 parents 5cbfb79 + 08684fb commit 567b042

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

lib/create-app.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ function createApp (argv = {}) {
4848
rootUrl: argv.serverUri,
4949
rootPath: path.resolve(argv.root || process.cwd()),
5050
includeHost: argv.multiuser,
51-
defaultContentType: argv.defaultContentType
51+
defaultContentType: argv.defaultContentType,
52+
fileSuffixes: [
53+
argv.suffixAcl || '.acl',
54+
argv.suffixMeta || '.meta'
55+
]
5256
})
5357

5458
const configPath = config.initConfigDir(argv)

lib/resource-mapper.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class ResourceMapper {
1515
includeHost = false,
1616
defaultContentType = 'application/octet-stream',
1717
indexFilename = 'index',
18-
overrideTypes = { acl: 'text/turtle', meta: 'text/turtle' }
18+
overrideTypes = { acl: 'text/turtle', meta: 'text/turtle' },
19+
fileSuffixes = ['.acl', '.meta']
1920
}) {
2021
this._rootUrl = this._removeTrailingSlash(rootUrl)
2122
this._rootPath = this._removeTrailingSlash(rootPath)
@@ -24,6 +25,7 @@ class ResourceMapper {
2425
this._defaultContentType = defaultContentType
2526
this._indexFilename = indexFilename
2627
this._types = { ...types, ...overrideTypes }
28+
this._isControlFile = new RegExp(`(?:${fileSuffixes.map(fs => fs.replace('.', '\\.')).join('|')})$`)
2729

2830
// If the host needs to be replaced on every call, pre-split the root URL
2931
if (includeHost) {
@@ -68,11 +70,9 @@ class ResourceMapper {
6870
// Read all files in the corresponding folder
6971
const filename = fullPath.substr(fullPath.lastIndexOf('/') + 1)
7072
const folder = fullPath.substr(0, fullPath.length - filename.length)
71-
const files = await this._readdir(folder)
7273

7374
// Find a file with the same name (minus the dollar extension)
74-
let match = !searchIndex ? '' : (files.find(f => this._removeDollarExtension(f) === filename ||
75-
(isIndex && f.startsWith(this._indexFilename + '.'))))
75+
let match = searchIndex ? await this._getMatchingFile(folder, filename, isIndex) : ''
7676
if (match === undefined) {
7777
// Error if no match was found,
7878
// unless the URL ends with a '/',
@@ -90,6 +90,17 @@ class ResourceMapper {
9090
return { path, contentType: contentType || this._defaultContentType }
9191
}
9292

93+
async _getMatchingFile (folder, filename, isIndex) {
94+
const files = await this._readdir(folder)
95+
const hasSameName = (f) => {
96+
return this._removeDollarExtension(f) === filename
97+
}
98+
const isIndexFile = (f) => {
99+
return isIndex && f.startsWith(this._indexFilename + '.') && !this._isControlFile.test(f)
100+
}
101+
return files.find(f => hasSameName(f) || isIndexFile(f))
102+
}
103+
93104
async getRepresentationUrlForResource (resourceUrl) {
94105
let fullPath = this.getFullPath(resourceUrl)
95106
let isIndex = fullPath.endsWith('/')

test/unit/resource-mapper-test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,18 @@ describe('ResourceMapper', () => {
286286
contentType: 'application/octet-stream'
287287
})
288288

289+
itMapsUrl(mapper, 'a URL of that has an accompanying acl file, but no actual file',
290+
{
291+
url: 'http://localhost/space/'
292+
},
293+
[
294+
`${rootPath}space/index.acl`
295+
],
296+
{
297+
path: `${rootPath}space/`,
298+
contentType: 'application/octet-stream'
299+
})
300+
289301
itMapsUrl(mapper, 'a URL ending with a slash to an index file for text/html when index.html not is available',
290302
{
291303
url: 'http://localhost/space/',
@@ -297,6 +309,20 @@ describe('ResourceMapper', () => {
297309
contentType: 'text/html'
298310
})
299311

312+
itMapsUrl(mapper, 'a URL of that has an accompanying meta file, but no actual file',
313+
{
314+
url: 'http://localhost/space/',
315+
contentType: 'text/html',
316+
createIfNotExists: true
317+
},
318+
[
319+
`${rootPath}space/index.meta`
320+
],
321+
{
322+
path: `${rootPath}space/index.html`,
323+
contentType: 'text/html'
324+
})
325+
300326
itMapsUrl(mapper, 'a URL ending with a slash to a folder when index is skipped',
301327
{
302328
url: 'http://localhost/space/',

0 commit comments

Comments
 (0)