Add --all flag to allow all supported file types at once; add --text flag
This commit is contained in:
parent
a217573483
commit
3c21946351
|
@ -108,6 +108,7 @@ Usage:
|
|||
roulette <path> [path]... [flags]
|
||||
|
||||
Flags:
|
||||
--all enable all supported file types
|
||||
--audio enable support for audio files
|
||||
-b, --bind string address to bind to (default "0.0.0.0")
|
||||
-c, --cache generate directory cache at startup
|
||||
|
@ -127,6 +128,7 @@ Flags:
|
|||
-s, --sort enable sorting
|
||||
--stats expose stats endpoint
|
||||
--stats-file string path to optional persistent stats file
|
||||
--text enable support for text files (default true)
|
||||
-v, --verbose log accessed files to stdout
|
||||
-V, --version display version and exit
|
||||
--video enable support for video files
|
||||
|
|
|
@ -12,10 +12,11 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
Version string = "0.67.0"
|
||||
Version string = "0.68.0"
|
||||
)
|
||||
|
||||
var (
|
||||
all bool
|
||||
audio bool
|
||||
bind string
|
||||
cache bool
|
||||
|
@ -34,6 +35,7 @@ var (
|
|||
sorting bool
|
||||
statistics bool
|
||||
statisticsFile string
|
||||
text bool
|
||||
verbose bool
|
||||
version bool
|
||||
videos bool
|
||||
|
@ -73,6 +75,7 @@ func Execute() {
|
|||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.Flags().BoolVar(&all, "all", false, "enable all supported file types")
|
||||
rootCmd.Flags().BoolVar(&audio, "audio", false, "enable support for audio files")
|
||||
rootCmd.Flags().StringVarP(&bind, "bind", "b", "0.0.0.0", "address to bind to")
|
||||
rootCmd.Flags().BoolVarP(&cache, "cache", "c", false, "generate directory cache at startup")
|
||||
|
@ -91,6 +94,7 @@ func init() {
|
|||
rootCmd.Flags().BoolVarP(&sorting, "sort", "s", false, "enable sorting")
|
||||
rootCmd.Flags().BoolVar(&statistics, "stats", false, "expose stats endpoint")
|
||||
rootCmd.Flags().StringVar(&statisticsFile, "stats-file", "", "path to optional persistent stats file")
|
||||
rootCmd.Flags().BoolVar(&text, "text", true, "enable support for text files")
|
||||
rootCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "log accessed files to stdout")
|
||||
rootCmd.Flags().BoolVarP(&version, "version", "V", false, "display version and exit")
|
||||
rootCmd.Flags().BoolVar(&videos, "video", false, "enable support for video files")
|
||||
|
|
16
cmd/web.go
16
cmd/web.go
|
@ -263,9 +263,11 @@ func serveMedia(paths []string, Regexes *Regexes, index *Index, registeredFormat
|
|||
htmlBody.WriteString(`<!DOCTYPE html><html lang="en"><head>`)
|
||||
htmlBody.WriteString(FaviconHtml)
|
||||
htmlBody.WriteString(`<style>html,body{margin:0;padding:0;height:100%;}`)
|
||||
htmlBody.WriteString(`a{display:block;height:100%;width:100%;text-decoration:none;}`)
|
||||
htmlBody.WriteString(`a{color:inherit;display:block;height:100%;width:100%;text-decoration:none;}`)
|
||||
htmlBody.WriteString(`img{margin:auto;display:block;max-width:97%;max-height:97%;object-fit:scale-down;`)
|
||||
htmlBody.WriteString(`position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);}</style>`)
|
||||
htmlBody.WriteString(`position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);}`)
|
||||
htmlBody.WriteString((fileType.Css(queryParams, fileUri, filePath, fileName, mimeType)))
|
||||
htmlBody.WriteString(`</style>`)
|
||||
htmlBody.WriteString((fileType.Title(queryParams, fileUri, filePath, fileName, mimeType)))
|
||||
htmlBody.WriteString(`</head><body>`)
|
||||
if refreshInterval != "0ms" {
|
||||
|
@ -319,15 +321,19 @@ func ServePage(args []string) error {
|
|||
|
||||
registeredFormats := &formats.SupportedFormats{}
|
||||
|
||||
if audio {
|
||||
if audio || all {
|
||||
registeredFormats.Add(formats.RegisterAudioFormats())
|
||||
}
|
||||
|
||||
if images {
|
||||
if images || all {
|
||||
registeredFormats.Add(formats.RegisterImageFormats())
|
||||
}
|
||||
|
||||
if videos {
|
||||
if text || all {
|
||||
registeredFormats.Add(formats.RegisterTextFormats())
|
||||
}
|
||||
|
||||
if videos || all {
|
||||
registeredFormats.Add(formats.RegisterVideoFormats())
|
||||
}
|
||||
|
||||
|
|
|
@ -10,6 +10,9 @@ import (
|
|||
|
||||
func RegisterAudioFormats() *SupportedFormat {
|
||||
return &SupportedFormat{
|
||||
Css: func(queryParams, fileUri, filePath, fileName, mime string) string {
|
||||
return ``
|
||||
},
|
||||
Title: func(queryParams, fileUri, filePath, fileName, mime string) string {
|
||||
return fmt.Sprintf(`<title>%s</title>`, fileName)
|
||||
},
|
||||
|
|
|
@ -24,6 +24,9 @@ type Dimensions struct {
|
|||
|
||||
func RegisterImageFormats() *SupportedFormat {
|
||||
return &SupportedFormat{
|
||||
Css: func(queryParams, fileUri, filePath, fileName, mime string) string {
|
||||
return ``
|
||||
},
|
||||
Title: func(queryParams, fileUri, filePath, fileName, mime string) string {
|
||||
dimensions, err := ImageDimensions(filePath)
|
||||
if err != nil {
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
Copyright © 2023 Seednode <seednode@seedno.de>
|
||||
*/
|
||||
|
||||
package formats
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func RegisterTextFormats() *SupportedFormat {
|
||||
return &SupportedFormat{
|
||||
Css: func(queryParams, fileUri, filePath, fileName, mime string) string {
|
||||
return `pre{margin:.5rem;}`
|
||||
},
|
||||
Title: func(queryParams, fileUri, filePath, fileName, mime string) string {
|
||||
return fmt.Sprintf(`<title>%s</title>`, fileName)
|
||||
},
|
||||
Body: func(queryParams, fileUri, filePath, fileName, mime string) string {
|
||||
body, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
body = []byte{}
|
||||
}
|
||||
|
||||
if !utf8.Valid(body) {
|
||||
body = []byte(`Unable to parse binary file as text.`)
|
||||
}
|
||||
|
||||
return fmt.Sprintf(`<a href="/%s"><pre>%s</pre></a>`,
|
||||
queryParams,
|
||||
body)
|
||||
},
|
||||
Extensions: []string{
|
||||
`.css`,
|
||||
`.csv`,
|
||||
`.html`,
|
||||
`.js`,
|
||||
`.json`,
|
||||
`.md`,
|
||||
`.txt`,
|
||||
`.xml`,
|
||||
},
|
||||
MimeTypes: []string{
|
||||
`application/json`,
|
||||
`application/octet-stream`,
|
||||
`application/xml`,
|
||||
`text/css`,
|
||||
`text/csv`,
|
||||
`text/javascript`,
|
||||
`text/plain`,
|
||||
`text/plain; charset=utf-8`,
|
||||
},
|
||||
}
|
||||
}
|
|
@ -13,6 +13,7 @@ import (
|
|||
type FormatFunction func(queryParams, fileUri, filePath, fileName, mime string) string
|
||||
|
||||
type SupportedFormat struct {
|
||||
Css FormatFunction
|
||||
Title FormatFunction
|
||||
Body FormatFunction
|
||||
Extensions []string
|
||||
|
|
|
@ -10,6 +10,9 @@ import (
|
|||
|
||||
func RegisterVideoFormats() *SupportedFormat {
|
||||
return &SupportedFormat{
|
||||
Css: func(queryParams, fileUri, filePath, fileName, mime string) string {
|
||||
return ``
|
||||
},
|
||||
Title: func(queryParams, fileUri, filePath, fileName, mime string) string {
|
||||
return fmt.Sprintf(`<title>%s</title>`, fileName)
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue