From 3c2194635112a6935cbacbd1057d656f53b9d1a4 Mon Sep 17 00:00:00 2001 From: Seednode Date: Mon, 11 Sep 2023 19:11:45 -0500 Subject: [PATCH] Add --all flag to allow all supported file types at once; add --text flag --- README.md | 2 ++ cmd/root.go | 6 ++++- cmd/web.go | 16 +++++++++----- formats/audio.go | 3 +++ formats/images.go | 3 +++ formats/text.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++ formats/types.go | 1 + formats/video.go | 3 +++ 8 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 formats/text.go diff --git a/README.md b/README.md index 2de6637..51c4856 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,7 @@ Usage: roulette [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 diff --git a/cmd/root.go b/cmd/root.go index 61bcbdb..f807860 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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") diff --git a/cmd/web.go b/cmd/web.go index 5c78a31..900d19e 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -263,9 +263,11 @@ func serveMedia(paths []string, Regexes *Regexes, index *Index, registeredFormat htmlBody.WriteString(``) htmlBody.WriteString(FaviconHtml) htmlBody.WriteString(``) + htmlBody.WriteString(`position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);}`) + htmlBody.WriteString((fileType.Css(queryParams, fileUri, filePath, fileName, mimeType))) + htmlBody.WriteString(``) htmlBody.WriteString((fileType.Title(queryParams, fileUri, filePath, fileName, mimeType))) htmlBody.WriteString(``) 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()) } diff --git a/formats/audio.go b/formats/audio.go index 3909cf4..5ed2bdd 100644 --- a/formats/audio.go +++ b/formats/audio.go @@ -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(`%s`, fileName) }, diff --git a/formats/images.go b/formats/images.go index c56757e..bed3713 100644 --- a/formats/images.go +++ b/formats/images.go @@ -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 { diff --git a/formats/text.go b/formats/text.go new file mode 100644 index 0000000..6d9c474 --- /dev/null +++ b/formats/text.go @@ -0,0 +1,56 @@ +/* +Copyright © 2023 Seednode +*/ + +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(`%s`, 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(`
%s
`, + 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`, + }, + } +} diff --git a/formats/types.go b/formats/types.go index c84d07c..b09b659 100644 --- a/formats/types.go +++ b/formats/types.go @@ -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 diff --git a/formats/video.go b/formats/video.go index 75c3a37..04b3207 100644 --- a/formats/video.go +++ b/formats/video.go @@ -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(`%s`, fileName) },