Add new --fun flag, add comments to Type interface

This commit is contained in:
Seednode 2023-10-04 17:33:03 -05:00
parent 9ae0aa60d3
commit 9844f4a2e1
10 changed files with 59 additions and 15 deletions

View File

@ -116,6 +116,7 @@ Flags:
--fallback serve files as application/octet-stream if no matching format is registered
-f, --filter enable filtering
--flash enable support for shockwave flash files (via ruffle.rs)
--fun adds a bit of excitement to your day
--handlers display registered handlers (for debugging)
-h, --help help for roulette
--images enable support for image files
@ -123,7 +124,7 @@ Flags:
--index-file string path to optional persistent index file
-i, --info expose informational endpoints
--max-file-count int skip directories with file counts above this value (default 2147483647)
--min-file-count int skip directories with file counts below this value (default 1)
--min-file-count int skip directories with file counts below this value
--page-length int pagination length for info pages
-p, --port int port to listen on (default 8080)
--prefix string root path for http handlers (for reverse proxying) (default "/")

View File

@ -12,7 +12,7 @@ import (
)
const (
ReleaseVersion string = "2.5.1"
ReleaseVersion string = "2.6.0"
)
var (
@ -26,6 +26,7 @@ var (
Fallback bool
Filtering bool
Flash bool
Fun bool
Handlers bool
Images bool
Index bool
@ -91,6 +92,7 @@ func init() {
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().BoolVar(&Flash, "flash", false, "enable support for shockwave flash files (via ruffle.rs)")
rootCmd.Flags().BoolVar(&Fun, "fun", false, "adds a bit of excitement to your day")
rootCmd.Flags().BoolVar(&Handlers, "handlers", false, "display registered handlers (for debugging)")
rootCmd.Flags().BoolVar(&Images, "images", false, "enable support for image files")
rootCmd.Flags().BoolVar(&Index, "index", false, "generate index of supported file paths at startup")

View File

@ -436,29 +436,29 @@ func ServePage(args []string) error {
formats := make(types.Types)
if Audio || All {
formats.Add(audio.Format{})
formats.Add(audio.Format{Fun: Fun})
}
if Code || All {
formats.Add(code.Format{Theme: CodeTheme})
formats.Add(code.Format{Fun: Fun, Theme: CodeTheme})
}
if Flash || All {
formats.Add(flash.Format{})
formats.Add(flash.Format{Fun: Fun})
}
if Text || All {
formats.Add(text.Format{})
formats.Add(text.Format{Fun: Fun})
}
if Videos || All {
formats.Add(video.Format{})
formats.Add(video.Format{Fun: Fun})
}
// enable image support if no other flags are passed, to retain backwards compatibility
// to be replaced with rootCmd.MarkFlagsOneRequired on next spf13/cobra update
if Images || All || len(formats) == 0 {
formats.Add(images.Format{})
formats.Add(images.Format{Fun: Fun})
}
paths, err := validatePaths(args, formats)

View File

@ -11,7 +11,9 @@ import (
"seedno.de/seednode/roulette/types"
)
type Format struct{}
type Format struct {
Fun bool
}
func (t Format) Css() string {
var css strings.Builder

View File

@ -20,6 +20,7 @@ import (
)
type Format struct {
Fun bool
Theme string
}
@ -56,6 +57,9 @@ func (t Format) Css() string {
css.WriteString("html{height:100%;width:100%;}\n")
css.WriteString("a{bottom:0;left:0;position:absolute;right:0;top:0;margin:1rem;padding:0;height:99%;width:99%;color:inherit;text-decoration:none;}\n")
if t.Fun {
css.WriteString("body{font-family: \"Comic Sans MS\", cursive, \"Brush Script MT\", sans-serif;}\n")
}
return css.String()
}

View File

@ -11,7 +11,9 @@ import (
"seedno.de/seednode/roulette/types"
)
type Format struct{}
type Format struct {
Fun bool
}
func (t Format) Css() string {
var css strings.Builder

View File

@ -11,6 +11,7 @@ import (
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"math/rand"
"os"
"strings"
@ -24,7 +25,9 @@ type dimensions struct {
height int
}
type Format struct{}
type Format struct {
Fun bool
}
func (t Format) Css() string {
var css strings.Builder
@ -32,7 +35,17 @@ func (t Format) Css() string {
css.WriteString(`html,body{margin:0;padding:0;height:100%;}`)
css.WriteString(`a{color:inherit;display:block;height:100%;width:100%;text-decoration:none;}`)
css.WriteString(`img{margin:auto;display:block;max-width:97%;max-height:97%;`)
css.WriteString(`object-fit:scale-down;position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);}`)
css.WriteString(`object-fit:scale-down;position:absolute;top:50%;left:50%;transform:translate(-50%,-50%)`)
if t.Fun {
rotate := rand.Intn(360)
css.WriteString(fmt.Sprintf(" rotate(%ddeg);", rotate))
css.WriteString(fmt.Sprintf("-ms-transform:rotate(%ddeg);", rotate))
css.WriteString(fmt.Sprintf("-webkit-transform:rotate(%ddeg);", rotate))
css.WriteString(fmt.Sprintf("-moz-transform:rotate(%ddeg);", rotate))
css.WriteString(fmt.Sprintf("-o-transform:rotate(%ddeg)", rotate))
}
css.WriteString(`;}`)
return css.String()
}

View File

@ -14,7 +14,9 @@ import (
"seedno.de/seednode/roulette/types"
)
type Format struct{}
type Format struct {
Fun bool
}
func (t Format) Css() string {
var css strings.Builder

View File

@ -13,12 +13,28 @@ import (
var SupportedFormats = make(Types)
type Type interface {
// Returns either "inline" or "embed", depending on whether the file
// should be displayed inline (e.g. code) or embedded (e.g. images)
Type() string
// Returns a CSS string used to format the corresponding page
Css() string
// Returns an HTML <title> element for the specified file
Title(rootUrl, fileUri, filePath, fileName, prefix, mime string) (string, error)
// Returns an HTML <body> element used to display the specified file
Body(rootUrl, fileUri, filePath, fileName, prefix, mime string) (string, error)
// Returns a map of file extensions to MIME type strings.
Extensions() map[string]string
MimeType(string) string
// Given a file extension, returns the corresponding MIME type,
// if one exists. Otherwise, returns an empty string.
MimeType(extension string) string
// Optional function used to validate whether a given file matches this format.
// If no validation checks are needed, this function should always return true.
Validate(filePath string) bool
}

View File

@ -12,7 +12,9 @@ import (
"seedno.de/seednode/roulette/types"
)
type Format struct{}
type Format struct {
Fun bool
}
func (t Format) Css() string {
var css strings.Builder