Gonna be honest, guys, I don't think I know what an interface is

This commit is contained in:
Seednode 2023-09-12 20:56:39 -05:00
parent 62a8bde8ea
commit c77a151a24
8 changed files with 262 additions and 227 deletions

View File

@ -12,7 +12,7 @@ import (
) )
const ( const (
ReleaseVersion string = "0.73.1" ReleaseVersion string = "0.74.0"
) )
var ( var (

View File

@ -313,27 +313,27 @@ func ServePage(args []string) error {
formats := &types.Types{ formats := &types.Types{
Extensions: make(map[string]string), Extensions: make(map[string]string),
MimeTypes: make(map[string]*types.Type), MimeTypes: make(map[string]types.Type),
} }
if Audio || All { if Audio || All {
formats.Add(types.RegisterAudio()) formats.Add(types.Audio{})
} }
if Flash || All { if Flash || All {
formats.Add(types.RegisterFlash()) formats.Add(types.Flash{})
} }
if Images || All { if Images || All {
formats.Add(types.RegisterImages()) formats.Add(types.Images{})
} }
if Text || All { if Text || All {
formats.Add(types.RegisterText()) formats.Add(types.Text{})
} }
if Videos || All { if Videos || All {
formats.Add(types.RegisterVideos()) formats.Add(types.Video{})
} }
paths, err := normalizePaths(args, formats) paths, err := normalizePaths(args, formats)

View File

@ -9,41 +9,48 @@ import (
"strings" "strings"
) )
func RegisterAudio() *Type { type Audio struct{}
return &Type{
Css: func() string { func (t Audio) Css() string {
var css strings.Builder var css strings.Builder
css.WriteString(`html,body{margin:0;padding:0;height:100%;}`) 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(`a{color:inherit;display:block;height:100%;width:100%;text-decoration:none;}`)
return css.String() return css.String()
}, }
Title: func(queryParams, fileUri, filePath, fileName, mime string) string {
func (t Audio) Title(queryParams, fileUri, filePath, fileName, mime string) string {
return fmt.Sprintf(`<title>%s</title>`, fileName) return fmt.Sprintf(`<title>%s</title>`, fileName)
}, }
Body: func(queryParams, fileUri, filePath, fileName, mime string) string {
func (t Audio) Body(queryParams, fileUri, filePath, fileName, mime string) string {
return fmt.Sprintf(`<a href="/%s"><audio controls autoplay loop preload="auto"><source src="%s" type="%s" alt="Roulette selected: %s">Your browser does not support the audio tag.</audio></a>`, return fmt.Sprintf(`<a href="/%s"><audio controls autoplay loop preload="auto"><source src="%s" type="%s" alt="Roulette selected: %s">Your browser does not support the audio tag.</audio></a>`,
queryParams, queryParams,
fileUri, fileUri,
mime, mime,
fileName) fileName)
}, }
Extensions: map[string]string{
func (t Audio) Extensions() map[string]string {
return map[string]string{
`.mp3`: `audio/mpeg`, `.mp3`: `audio/mpeg`,
`.ogg`: `audio/ogg`, `.ogg`: `audio/ogg`,
`.oga`: `audio/ogg`, `.oga`: `audio/ogg`,
}, }
MimeTypes: []string{ }
func (t Audio) MimeTypes() []string {
return []string{
`application/ogg`, `application/ogg`,
`audio/mp3`, `audio/mp3`,
`audio/mpeg`, `audio/mpeg`,
`audio/mpeg3`, `audio/mpeg3`,
`audio/ogg`, `audio/ogg`,
`audio/x-mpeg-3`, `audio/x-mpeg-3`,
},
Validate: func(filePath string) bool {
return true
},
} }
} }
func (t Audio) Validate(filePath string) bool {
return true
}

View File

@ -9,35 +9,42 @@ import (
"strings" "strings"
) )
func RegisterFlash() *Type { type Flash struct{}
return &Type{
Css: func() string { func (t Flash) Css() string {
var css strings.Builder var css strings.Builder
css.WriteString(`html,body{margin:0;padding:0;height:100%;}`) 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(`a{color:inherit;display:block;height:100%;width:100%;text-decoration:none;}`)
return css.String() return css.String()
}, }
Title: func(queryParams, fileUri, filePath, fileName, mime string) string {
func (t Flash) Title(queryParams, fileUri, filePath, fileName, mime string) string {
return fmt.Sprintf(`<title>%s</title>`, fileName) return fmt.Sprintf(`<title>%s</title>`, fileName)
}, }
Body: func(queryParams, fileUri, filePath, fileName, mime string) string {
func (t Flash) Body(queryParams, fileUri, filePath, fileName, mime string) string {
var html strings.Builder var html strings.Builder
html.WriteString(fmt.Sprintf(`<script src="https://unpkg.com/@ruffle-rs/ruffle"></script><script>window.RufflePlayer.config = {autoplay:"on"};</script><embed src="%s"></embed>`, fileUri)) html.WriteString(fmt.Sprintf(`<script src="https://unpkg.com/@ruffle-rs/ruffle"></script><script>window.RufflePlayer.config = {autoplay:"on"};</script><embed src="%s"></embed>`, fileUri))
html.WriteString(fmt.Sprintf(`<br /><button onclick="window.location.href = '/%s';">Next</button>`, queryParams)) html.WriteString(fmt.Sprintf(`<br /><button onclick="window.location.href = '/%s';">Next</button>`, queryParams))
return html.String() return html.String()
}, }
Extensions: map[string]string{
func (t Flash) Extensions() map[string]string {
return map[string]string{
`.swf`: `application/x-shockwave-flash`, `.swf`: `application/x-shockwave-flash`,
},
MimeTypes: []string{
`application/x-shockwave-flash`,
},
Validate: func(filePath string) bool {
return true
},
} }
} }
func (t Flash) MimeTypes() []string {
return []string{
`application/x-shockwave-flash`,
}
}
func (t Flash) Validate(filePath string) bool {
return true
}

View File

@ -23,9 +23,9 @@ type Dimensions struct {
Height int Height int
} }
func RegisterImages() *Type { type Images struct{}
return &Type{
Css: func() string { func (t Images) Css() string {
var css strings.Builder var css strings.Builder
css.WriteString(`html,body{margin:0;padding:0;height:100%;}`) css.WriteString(`html,body{margin:0;padding:0;height:100%;}`)
@ -34,8 +34,9 @@ func RegisterImages() *Type {
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%);}`)
return css.String() return css.String()
}, }
Title: func(queryParams, fileUri, filePath, fileName, mime string) string {
func (t Images) Title(queryParams, fileUri, filePath, fileName, mime string) string {
dimensions, err := ImageDimensions(filePath) dimensions, err := ImageDimensions(filePath)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -45,8 +46,9 @@ func RegisterImages() *Type {
fileName, fileName,
dimensions.Width, dimensions.Width,
dimensions.Height) dimensions.Height)
}, }
Body: func(queryParams, fileUri, filePath, fileName, mime string) string {
func (t Images) Body(queryParams, fileUri, filePath, fileName, mime string) string {
dimensions, err := ImageDimensions(filePath) dimensions, err := ImageDimensions(filePath)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -59,8 +61,10 @@ func RegisterImages() *Type {
dimensions.Height, dimensions.Height,
mime, mime,
fileName) fileName)
}, }
Extensions: map[string]string{
func (t Images) Extensions() map[string]string {
return map[string]string{
`.apng`: `image/apng`, `.apng`: `image/apng`,
`.avif`: `image/avif`, `.avif`: `image/avif`,
`.bmp`: `image/bmp`, `.bmp`: `image/bmp`,
@ -73,8 +77,11 @@ func RegisterImages() *Type {
`.png`: `image/png`, `.png`: `image/png`,
`.svg`: `image/svg+xml`, `.svg`: `image/svg+xml`,
`.webp`: `image/webp`, `.webp`: `image/webp`,
}, }
MimeTypes: []string{ }
func (t Images) MimeTypes() []string {
return []string{
`image/apng`, `image/apng`,
`image/avif`, `image/avif`,
`image/bmp`, `image/bmp`,
@ -83,13 +90,13 @@ func RegisterImages() *Type {
`image/png`, `image/png`,
`image/svg+xml`, `image/svg+xml`,
`image/webp`, `image/webp`,
},
Validate: func(filePath string) bool {
return true
},
} }
} }
func (t Images) Validate(filePath string) bool {
return true
}
func ImageDimensions(path string) (*Dimensions, error) { func ImageDimensions(path string) (*Dimensions, error) {
file, err := os.Open(path) file, err := os.Open(path)
switch { switch {

View File

@ -12,9 +12,9 @@ import (
"unicode/utf8" "unicode/utf8"
) )
func RegisterText() *Type { type Text struct{}
return &Type{
Css: func() string { func (t Text) Css() string {
var css strings.Builder var css strings.Builder
css.WriteString(`html,body{margin:0;padding:0;height:100%;}`) css.WriteString(`html,body{margin:0;padding:0;height:100%;}`)
@ -23,11 +23,13 @@ func RegisterText() *Type {
css.WriteString(`height:99%;width:99%;white-space:pre;overflow:auto;}`) css.WriteString(`height:99%;width:99%;white-space:pre;overflow:auto;}`)
return css.String() return css.String()
}, }
Title: func(queryParams, fileUri, filePath, fileName, mime string) string {
func (t Text) Title(queryParams, fileUri, filePath, fileName, mime string) string {
return fmt.Sprintf(`<title>%s</title>`, fileName) return fmt.Sprintf(`<title>%s</title>`, fileName)
}, }
Body: func(queryParams, fileUri, filePath, fileName, mime string) string {
func (t Text) Body(queryParams, fileUri, filePath, fileName, mime string) string {
body, err := os.ReadFile(filePath) body, err := os.ReadFile(filePath)
if err != nil { if err != nil {
body = []byte{} body = []byte{}
@ -36,8 +38,10 @@ func RegisterText() *Type {
return fmt.Sprintf(`<a href="/%s"><textarea autofocus readonly>%s</textarea></a>`, return fmt.Sprintf(`<a href="/%s"><textarea autofocus readonly>%s</textarea></a>`,
queryParams, queryParams,
body) body)
}, }
Extensions: map[string]string{
func (t Text) Extensions() map[string]string {
return map[string]string{
`.css`: `text/css`, `.css`: `text/css`,
`.csv`: `text/csv`, `.csv`: `text/csv`,
`.html`: `text/html`, `.html`: `text/html`,
@ -46,8 +50,11 @@ func RegisterText() *Type {
`.md`: `text/markdown`, `.md`: `text/markdown`,
`.txt`: `text/plain`, `.txt`: `text/plain`,
`.xml`: `application/xml`, `.xml`: `application/xml`,
}, }
MimeTypes: []string{ }
func (t Text) MimeTypes() []string {
return []string{
`application/json`, `application/json`,
`application/xml`, `application/xml`,
`text/css`, `text/css`,
@ -55,9 +62,11 @@ func RegisterText() *Type {
`text/javascript`, `text/javascript`,
`text/plain`, `text/plain`,
`text/plain; charset=utf-8`, `text/plain; charset=utf-8`,
}, }
Validate: func(path string) bool { }
file, err := os.Open(path)
func (t Text) Validate(filePath string) bool {
file, err := os.Open(filePath)
switch { switch {
case errors.Is(err, os.ErrNotExist): case errors.Is(err, os.ErrNotExist):
return false return false
@ -70,6 +79,4 @@ func RegisterText() *Type {
file.Read(head) file.Read(head)
return utf8.Valid(head) return utf8.Valid(head)
},
}
} }

View File

@ -11,29 +11,29 @@ import (
"path/filepath" "path/filepath"
) )
type Type struct { type Type interface {
Css func() string Css() string
Title func(queryParams, fileUri, filePath, fileName, mime string) string Title(queryParams, fileUri, filePath, fileName, mime string) string
Body func(queryParams, fileUri, filePath, fileName, mime string) string Body(queryParams, fileUri, filePath, fileName, mime string) string
Extensions map[string]string Extensions() map[string]string
MimeTypes []string MimeTypes() []string
Validate func(filePath string) bool Validate(filePath string) bool
} }
type Types struct { type Types struct {
Extensions map[string]string Extensions map[string]string
MimeTypes map[string]*Type MimeTypes map[string]Type
} }
func (s *Types) Add(t *Type) { func (s *Types) Add(t Type) {
for k, v := range t.Extensions { for k, v := range t.Extensions() {
_, exists := s.Extensions[k] _, exists := s.Extensions[k]
if !exists { if !exists {
s.Extensions[k] = v s.Extensions[k] = v
} }
} }
for _, v := range t.MimeTypes { for _, v := range t.MimeTypes() {
_, exists := s.Extensions[v] _, exists := s.Extensions[v]
if !exists { if !exists {
s.MimeTypes[v] = t s.MimeTypes[v] = t
@ -41,7 +41,7 @@ func (s *Types) Add(t *Type) {
} }
} }
func FileType(path string, registeredFormats *Types) (bool, *Type, string, error) { func FileType(path string, registeredFormats *Types) (bool, Type, string, error) {
file, err := os.Open(path) file, err := os.Open(path)
switch { switch {
case errors.Is(err, os.ErrNotExist): case errors.Is(err, os.ErrNotExist):

View File

@ -9,9 +9,9 @@ import (
"strings" "strings"
) )
func RegisterVideos() *Type { type Video struct{}
return &Type{
Css: func() string { func (t Video) Css() string {
var css strings.Builder var css strings.Builder
css.WriteString(`html,body{margin:0;padding:0;height:100%;}`) css.WriteString(`html,body{margin:0;padding:0;height:100%;}`)
@ -20,30 +20,37 @@ func RegisterVideos() *Type {
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%);}`)
return css.String() return css.String()
}, }
Title: func(queryParams, fileUri, filePath, fileName, mime string) string {
func (t Video) Title(queryParams, fileUri, filePath, fileName, mime string) string {
return fmt.Sprintf(`<title>%s</title>`, fileName) return fmt.Sprintf(`<title>%s</title>`, fileName)
}, }
Body: func(queryParams, fileUri, filePath, fileName, mime string) string {
func (t Video) Body(queryParams, fileUri, filePath, fileName, mime string) string {
return fmt.Sprintf(`<a href="/%s"><video controls autoplay loop preload="auto"><source src="%s" type="%s" alt="Roulette selected: %s">Your browser does not support the video tag.</video></a>`, return fmt.Sprintf(`<a href="/%s"><video controls autoplay loop preload="auto"><source src="%s" type="%s" alt="Roulette selected: %s">Your browser does not support the video tag.</video></a>`,
queryParams, queryParams,
fileUri, fileUri,
mime, mime,
fileName) fileName)
}, }
Extensions: map[string]string{
func (t Video) Extensions() map[string]string {
return map[string]string{
`.mp4`: `video/mp4`, `.mp4`: `video/mp4`,
`.ogm`: `video/ogg`, `.ogm`: `video/ogg`,
`.ogv`: `video/ogg`, `.ogv`: `video/ogg`,
`.webm`: `video/webm`, `.webm`: `video/webm`,
}, }
MimeTypes: []string{ }
func (t Video) MimeTypes() []string {
return []string{
`video/mp4`, `video/mp4`,
`video/ogg`, `video/ogg`,
`video/webm`, `video/webm`,
},
Validate: func(filePath string) bool {
return true
},
} }
} }
func (t Video) Validate(filePath string) bool {
return true
}