Add --fallback option to serve every file, even if no corresponding format is registered
This commit is contained in:
parent
13440f6a4e
commit
e67752470d
|
@ -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)
|
||||||
|
|
10
cmd/files.go
10
cmd/files.go
|
@ -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
|
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)")
|
||||||
|
|
31
cmd/web.go
31
cmd/web.go
|
@ -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")
|
||||||
|
|
||||||
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) {
|
if !format.Validate(path) {
|
||||||
|
|
Loading…
Reference in New Issue