@@ -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 ( '/' )
0 commit comments