Add --fallback option to serve every file, even if no corresponding format is registered

This commit is contained in:
Seednode 2023-10-03 09:44:50 -05:00
parent 13440f6a4e
commit e67752470d
4 changed files with 34 additions and 12 deletions

View File

@ -113,6 +113,7 @@ Flags:
--code enable support for source code files --code enable support for source code files
--code-theme string theme for source code syntax highlighting (default "solarized-dark256") --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 --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 -f, --filter enable filtering
--flash enable support for shockwave flash files (via ruffle.rs) --flash enable support for shockwave flash files (via ruffle.rs)
--handlers display registered handlers (for debugging) --handlers display registered handlers (for debugging)

View File

@ -291,15 +291,15 @@ func walkPath(path string, fileChannel chan<- string, stats *scanStatsChannels,
return return
} }
if !formats.Validate(path) { if formats.Validate(path) || Fallback {
stats.filesSkipped <- 1 fileChannel <- path
stats.filesMatched <- 1
return return
} }
fileChannel <- path stats.filesSkipped <- 1
stats.filesMatched <- 1
}() }()
} }
} }

View File

@ -12,7 +12,7 @@ import (
) )
const ( const (
ReleaseVersion string = "2.2.1" ReleaseVersion string = "2.3.0"
) )
var ( var (
@ -23,6 +23,7 @@ var (
Code bool Code bool
CodeTheme string CodeTheme string
ExitOnError bool ExitOnError bool
Fallback bool
Filtering bool Filtering bool
Flash bool Flash bool
Handlers bool Handlers bool
@ -87,6 +88,7 @@ func init() {
rootCmd.Flags().BoolVar(&Code, "code", false, "enable support for source code files") 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().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(&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().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(&Flash, "flash", false, "enable support for shockwave flash files (via ruffle.rs)")
rootCmd.Flags().BoolVar(&Handlers, "handlers", false, "display registered handlers (for debugging)") rootCmd.Flags().BoolVar(&Handlers, "handlers", false, "display registered handlers (for debugging)")

View File

@ -41,12 +41,12 @@ const (
timeout time.Duration = 10 * time.Second timeout time.Duration = 10 * time.Second
) )
func preparePath(path string) string { func preparePath(prefix, path string) string {
if runtime.GOOS == "windows" { 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 { 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", newUrl := fmt.Sprintf("http://%s%s%s%s",
r.Host, r.Host,
Prefix, Prefix,
preparePath(path), preparePath(mediaPrefix, path),
queryParams, queryParams,
) )
http.Redirect(w, r, newUrl, redirectStatusCode) http.Redirect(w, r, newUrl, redirectStatusCode)
@ -266,9 +266,28 @@ func serveMedia(paths []string, regexes *regexes, index *fileIndex, formats *typ
format := formats.FileType(path) format := formats.FileType(path)
if format == nil { if format == nil {
serverError(w, r, nil) if Fallback {
w.Header().Add("Content-Type", "application/octet-stream")
_, 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 return
} else {
notFound(w, r, path)
return
}
} }
if !format.Validate(path) { if !format.Validate(path) {