@@ -14,7 +14,7 @@ class ResourceMapper {
1414 rootPath,
1515 includeHost = false ,
1616 defaultContentType = 'application/octet-stream' ,
17- indexFilename = 'index' ,
17+ indexFilename = 'index.html ' ,
1818 overrideTypes = { acl : 'text/turtle' , meta : 'text/turtle' } ,
1919 fileSuffixes = [ '.acl' , '.meta' ]
2020 } ) {
@@ -23,8 +23,9 @@ class ResourceMapper {
2323 this . _includeHost = includeHost
2424 this . _readdir = readdir
2525 this . _defaultContentType = defaultContentType
26- this . _indexFilename = indexFilename
2726 this . _types = { ...types , ...overrideTypes }
27+ this . _indexFilename = indexFilename
28+ this . _indexContentType = this . _getContentTypeByExtension ( indexFilename )
2829 this . _isControlFile = new RegExp ( `(?:${ fileSuffixes . map ( fs => fs . replace ( '.' , '\\.' ) ) . join ( '|' ) } )$` )
2930
3031 // If the host needs to be replaced on every call, pre-split the root URL
@@ -41,15 +42,17 @@ class ResourceMapper {
4142 }
4243
4344 // Maps the request for a given resource and representation format to a server file
44- // When the URL ends with a '/', then files with the prefix 'index.' will be matched,
45- // such as 'index.html' and 'index.ttl', unless searchIndex is false.
45+ // When searchIndex is true and the URL ends with a '/', indexFilename will be matched.
4646 async mapUrlToFile ( { url, contentType, createIfNotExists, searchIndex = true } ) {
4747 let fullPath = this . getFullPath ( url )
4848 let isIndex = searchIndex && fullPath . endsWith ( '/' )
4949 let path
5050
5151 // Append index filename if the URL ends with a '/'
5252 if ( isIndex ) {
53+ if ( createIfNotExists && contentType !== this . _indexContentType ) {
54+ throw new Error ( `Index file needs to have ${ this . _indexContentType } as content type` )
55+ }
5356 fullPath += this . _indexFilename
5457 }
5558
@@ -58,12 +61,7 @@ class ResourceMapper {
5861 path = fullPath
5962 // If the extension is not correct for the content type, append the correct extension
6063 if ( searchIndex && this . _getContentTypeByExtension ( path ) !== contentType ) {
61- // Append a '$', unless we map for the index
62- // Because the index must _always_ have a proper extension when it is being created
63- if ( ! isIndex ) {
64- path += '$'
65- }
66- path += contentType in extensions ? `.${ extensions [ contentType ] [ 0 ] } ` : '.unknown'
64+ path += `$${ contentType in extensions ? `.${ extensions [ contentType ] [ 0 ] } ` : '.unknown' } `
6765 }
6866 // Determine the path of an existing file
6967 } else {
@@ -92,13 +90,13 @@ class ResourceMapper {
9290
9391 async _getMatchingFile ( folder , filename , isIndex ) {
9492 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 )
93+ // Search for files with the same name (disregarding a dollar extension)
94+ if ( ! isIndex ) {
95+ return files . find ( f => this . _removeDollarExtension ( f ) === filename )
96+ // Check if the index file exists
97+ } else if ( files . includes ( this . _indexFilename ) ) {
98+ return this . _indexFilename
10099 }
101- return files . find ( f => hasSameName ( f ) || isIndexFile ( f ) )
102100 }
103101
104102 async getRepresentationUrlForResource ( resourceUrl ) {
0 commit comments