Skip to content

Commit

Permalink
feat: Allow extensionless file downloads. #1627
Browse files Browse the repository at this point in the history
  • Loading branch information
mturoci committed Nov 28, 2022
1 parent 2498fb9 commit 61215c5
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions file_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,16 @@ func (fs *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

if path.Ext(r.URL.Path) == "" { // ignore requests for directories and ext-less files
trimmedPrefix := strings.TrimPrefix(r.URL.Path, fs.baseURL)
// Ignore requests for directories and non-existent / unaccessible files.
if fileInfo, err := os.Stat(filepath.Join(fs.dir, trimmedPrefix)); err != nil || fileInfo.IsDir() {
echo(Log{"t": "file_download", "path": r.URL.Path, "error": "not found"})
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}

echo(Log{"t": "file_download", "path": r.URL.Path})
r.URL.Path = strings.TrimPrefix(r.URL.Path, fs.baseURL) // public
r.URL.Path = trimmedPrefix // public
fs.handler.ServeHTTP(w, r)

case http.MethodPost:
Expand Down

0 comments on commit 61215c5

Please sign in to comment.