Add --all flag to allow all supported file types at once; add --text flag

This commit is contained in:
Seednode 2023-09-11 19:11:45 -05:00
parent a217573483
commit 3c21946351
8 changed files with 84 additions and 6 deletions

View File

@ -108,6 +108,7 @@ Usage:
roulette <path> [path]... [flags] roulette <path> [path]... [flags]
Flags: Flags:
--all enable all supported file types
--audio enable support for audio files --audio enable support for audio files
-b, --bind string address to bind to (default "0.0.0.0") -b, --bind string address to bind to (default "0.0.0.0")
-c, --cache generate directory cache at startup -c, --cache generate directory cache at startup
@ -127,6 +128,7 @@ Flags:
-s, --sort enable sorting -s, --sort enable sorting
--stats expose stats endpoint --stats expose stats endpoint
--stats-file string path to optional persistent stats file --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, --verbose log accessed files to stdout
-V, --version display version and exit -V, --version display version and exit
--video enable support for video files --video enable support for video files

View File

@ -12,10 +12,11 @@ import (
) )
const ( const (
Version string = "0.67.0" Version string = "0.68.0"
) )
var ( var (
all bool
audio bool audio bool
bind string bind string
cache bool cache bool
@ -34,6 +35,7 @@ var (
sorting bool sorting bool
statistics bool statistics bool
statisticsFile string statisticsFile string
text bool
verbose bool verbose bool
version bool version bool
videos bool videos bool
@ -73,6 +75,7 @@ func Execute() {
} }
func init() { 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().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().StringVarP(&bind, "bind", "b", "0.0.0.0", "address to bind to")
rootCmd.Flags().BoolVarP(&cache, "cache", "c", false, "generate directory cache at startup") 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().BoolVarP(&sorting, "sort", "s", false, "enable sorting")
rootCmd.Flags().BoolVar(&statistics, "stats", false, "expose stats endpoint") rootCmd.Flags().BoolVar(&statistics, "stats", false, "expose stats endpoint")
rootCmd.Flags().StringVar(&statisticsFile, "stats-file", "", "path to optional persistent stats file") 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(&verbose, "verbose", "v", false, "log accessed files to stdout")
rootCmd.Flags().BoolVarP(&version, "version", "V", false, "display version and exit") rootCmd.Flags().BoolVarP(&version, "version", "V", false, "display version and exit")
rootCmd.Flags().BoolVar(&videos, "video", false, "enable support for video files") rootCmd.Flags().BoolVar(&videos, "video", false, "enable support for video files")

View File

@ -263,9 +263,11 @@ func serveMedia(paths []string, Regexes *Regexes, index *Index, registeredFormat
htmlBody.WriteString(`<!DOCTYPE html><html lang="en"><head>`) htmlBody.WriteString(`<!DOCTYPE html><html lang="en"><head>`)
htmlBody.WriteString(FaviconHtml) htmlBody.WriteString(FaviconHtml)
htmlBody.WriteString(`<style>html,body{margin:0;padding:0;height:100%;}`) 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(`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((fileType.Title(queryParams, fileUri, filePath, fileName, mimeType)))
htmlBody.WriteString(`</head><body>`) htmlBody.WriteString(`</head><body>`)
if refreshInterval != "0ms" { if refreshInterval != "0ms" {
@ -319,15 +321,19 @@ func ServePage(args []string) error {
registeredFormats := &formats.SupportedFormats{} registeredFormats := &formats.SupportedFormats{}
if audio { if audio || all {
registeredFormats.Add(formats.RegisterAudioFormats()) registeredFormats.Add(formats.RegisterAudioFormats())
} }
if images { if images || all {
registeredFormats.Add(formats.RegisterImageFormats()) registeredFormats.Add(formats.RegisterImageFormats())
} }
if videos { if text || all {
registeredFormats.Add(formats.RegisterTextFormats())
}
if videos || all {
registeredFormats.Add(formats.RegisterVideoFormats()) registeredFormats.Add(formats.RegisterVideoFormats())
} }

View File

@ -10,6 +10,9 @@ import (
func RegisterAudioFormats() *SupportedFormat { func RegisterAudioFormats() *SupportedFormat {
return &SupportedFormat{ return &SupportedFormat{
Css: func(queryParams, fileUri, filePath, fileName, mime string) string {
return ``
},
Title: func(queryParams, fileUri, filePath, fileName, mime string) string { Title: func(queryParams, fileUri, filePath, fileName, mime string) string {
return fmt.Sprintf(`<title>%s</title>`, fileName) return fmt.Sprintf(`<title>%s</title>`, fileName)
}, },

View File

@ -24,6 +24,9 @@ type Dimensions struct {
func RegisterImageFormats() *SupportedFormat { func RegisterImageFormats() *SupportedFormat {
return &SupportedFormat{ return &SupportedFormat{
Css: func(queryParams, fileUri, filePath, fileName, mime string) string {
return ``
},
Title: func(queryParams, fileUri, filePath, fileName, mime string) string { Title: func(queryParams, fileUri, filePath, fileName, mime string) string {
dimensions, err := ImageDimensions(filePath) dimensions, err := ImageDimensions(filePath)
if err != nil { if err != nil {

56
formats/text.go Normal file
View File

@ -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`,
},
}
}

View File

@ -13,6 +13,7 @@ import (
type FormatFunction func(queryParams, fileUri, filePath, fileName, mime string) string type FormatFunction func(queryParams, fileUri, filePath, fileName, mime string) string
type SupportedFormat struct { type SupportedFormat struct {
Css FormatFunction
Title FormatFunction Title FormatFunction
Body FormatFunction Body FormatFunction
Extensions []string Extensions []string

View File

@ -10,6 +10,9 @@ import (
func RegisterVideoFormats() *SupportedFormat { func RegisterVideoFormats() *SupportedFormat {
return &SupportedFormat{ return &SupportedFormat{
Css: func(queryParams, fileUri, filePath, fileName, mime string) string {
return ``
},
Title: func(queryParams, fileUri, filePath, fileName, mime string) string { Title: func(queryParams, fileUri, filePath, fileName, mime string) string {
return fmt.Sprintf(`<title>%s</title>`, fileName) return fmt.Sprintf(`<title>%s</title>`, fileName)
}, },