@@ -7,7 +7,7 @@ const HTTPError = require('./http-error')
7
7
8
8
/*
9
9
* 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
11
11
* https://www.w3.org/DesignIssues/HTTPFilenameMapping.html
12
12
*
13
13
* This class implements this mapping in a single place
@@ -91,9 +91,10 @@ class ResourceMapper {
91
91
if ( filePath . indexOf ( '/..' ) >= 0 ) {
92
92
throw new Error ( 'Disallowed /.. segment in URL' )
93
93
}
94
+ let isFolder = filePath . endsWith ( '/' )
94
95
let isIndex = searchIndex && filePath . endsWith ( '/' )
95
96
96
- // Create the path for a new file
97
+ // Create the path for a new ressource
97
98
let path
98
99
if ( createIfNotExists ) {
99
100
path = filePath
@@ -105,7 +106,7 @@ class ResourceMapper {
105
106
path += this . _indexFilename
106
107
}
107
108
// 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 ) {
109
110
path += `$${ contentType in extensions ? `.${ extensions [ contentType ] [ 0 ] } ` : '.unknown' } `
110
111
}
111
112
// Determine the path of an existing file
@@ -116,13 +117,13 @@ class ResourceMapper {
116
117
117
118
// Find a file with the same name (minus the dollar extension)
118
119
let match = ''
119
- if ( searchIndex ) {
120
+ if ( match === '' ) { // always true to keep indentation
120
121
const files = await this . _readdir ( folder )
121
122
// Search for files with the same name (disregarding a dollar extension)
122
- if ( ! isIndex ) {
123
+ if ( ! isFolder ) {
123
124
match = files . find ( f => this . _removeDollarExtension ( f ) === filename )
124
125
// Check if the index file exists
125
- } else if ( files . includes ( this . _indexFilename ) ) {
126
+ } else if ( searchIndex && files . includes ( this . _indexFilename ) ) {
126
127
match = this . _indexFilename
127
128
}
128
129
}
@@ -137,7 +138,6 @@ class ResourceMapper {
137
138
path = `${ folder } ${ match } `
138
139
contentType = this . _getContentTypeByExtension ( match )
139
140
}
140
-
141
141
return { path, contentType : contentType || this . _defaultContentType }
142
142
}
143
143
0 commit comments