diff --git a/README.md b/README.md index 7ba5b9f..c6da61b 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,7 @@ Flags: --code enable support for source code files --code-theme string theme for source code syntax highlighting (default "solarized-dark256") --exit-on-error shut down webserver on error, instead of just printing the error + --fallback serve files as application/octet-stream if no matching format is registered -f, --filter enable filtering --flash enable support for shockwave flash files (via ruffle.rs) --handlers display registered handlers (for debugging) diff --git a/cmd/files.go b/cmd/files.go index 80670dd..ed99fed 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -291,15 +291,15 @@ func walkPath(path string, fileChannel chan<- string, stats *scanStatsChannels, return } - if !formats.Validate(path) { - stats.filesSkipped <- 1 + if formats.Validate(path) || Fallback { + fileChannel <- path + + stats.filesMatched <- 1 return } - fileChannel <- path - - stats.filesMatched <- 1 + stats.filesSkipped <- 1 }() } } diff --git a/cmd/root.go b/cmd/root.go index e148128..07498e5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -12,7 +12,7 @@ import ( ) const ( - ReleaseVersion string = "2.2.1" + ReleaseVersion string = "2.3.0" ) var ( @@ -23,6 +23,7 @@ var ( Code bool CodeTheme string ExitOnError bool + Fallback bool Filtering bool Flash bool Handlers bool @@ -87,6 +88,7 @@ func init() { rootCmd.Flags().BoolVar(&Code, "code", false, "enable support for source code files") rootCmd.Flags().StringVar(&CodeTheme, "code-theme", "solarized-dark256", "theme for source code syntax highlighting") rootCmd.Flags().BoolVar(&ExitOnError, "exit-on-error", false, "shut down webserver on error, instead of just printing the error") + rootCmd.Flags().BoolVar(&Fallback, "fallback", false, "serve files as application/octet-stream if no matching format is registered") rootCmd.Flags().BoolVarP(&Filtering, "filter", "f", false, "enable filtering") rootCmd.Flags().BoolVar(&Flash, "flash", false, "enable support for shockwave flash files (via ruffle.rs)") rootCmd.Flags().BoolVar(&Handlers, "handlers", false, "display registered handlers (for debugging)") diff --git a/cmd/web.go b/cmd/web.go index b7cc893..20c6f68 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -41,12 +41,12 @@ const ( timeout time.Duration = 10 * time.Second ) -func preparePath(path string) string { +func preparePath(prefix, path string) string { if runtime.GOOS == "windows" { - return fmt.Sprintf("%s/%s", mediaPrefix, filepath.ToSlash(path)) + return fmt.Sprintf("%s/%s", prefix, filepath.ToSlash(path)) } - return mediaPrefix + path + return prefix + path } func serveStaticFile(paths []string, index *fileIndex, errorChannel chan<- error) httprouter.Handle { @@ -228,7 +228,7 @@ func serveRoot(paths []string, regexes *regexes, index *fileIndex, formats *type newUrl := fmt.Sprintf("http://%s%s%s%s", r.Host, Prefix, - preparePath(path), + preparePath(mediaPrefix, path), queryParams, ) http.Redirect(w, r, newUrl, redirectStatusCode) @@ -266,9 +266,28 @@ func serveMedia(paths []string, regexes *regexes, index *fileIndex, formats *typ format := formats.FileType(path) if format == nil { - serverError(w, r, nil) + if Fallback { + w.Header().Add("Content-Type", "application/octet-stream") - return + _, refreshInterval := refreshInterval(r) + + // redirect to static url for file + newUrl := fmt.Sprintf("http://%s%s%s%s", + r.Host, + Prefix, + preparePath(sourcePrefix, path), + generateQueryParams(filters, sortOrder, refreshInterval), + ) + + http.Redirect(w, r, newUrl, redirectStatusCode) + + return + } else { + notFound(w, r, path) + + return + + } } if !format.Validate(path) {