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 (
ReleaseVersion string = "0.73.1"
ReleaseVersion string = "0.74.0"
)
var (

View File

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

View File

@ -9,41 +9,48 @@ import (
"strings"
)
func RegisterAudio() *Type {
return &Type{
Css: func() string {
type Audio struct{}
func (t Audio) Css() string {
var css strings.Builder
css.WriteString(`html,body{margin:0;padding:0;height:100%;}`)
css.WriteString(`a{color:inherit;display:block;height:100%;width:100%;text-decoration:none;}`)
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)
},
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>`,
queryParams,
fileUri,
mime,
fileName)
},
Extensions: map[string]string{
}
func (t Audio) Extensions() map[string]string {
return map[string]string{
`.mp3`: `audio/mpeg`,
`.ogg`: `audio/ogg`,
`.oga`: `audio/ogg`,
},
MimeTypes: []string{
}
}
func (t Audio) MimeTypes() []string {
return []string{
`application/ogg`,
`audio/mp3`,
`audio/mpeg`,
`audio/mpeg3`,
`audio/ogg`,
`audio/x-mpeg-3`,
},
Validate: func(filePath string) bool {
}
}
func (t Audio) Validate(filePath string) bool {
return true
},
}
}

View File

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

View File

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

View File

@ -12,9 +12,9 @@ import (
"unicode/utf8"
)
func RegisterText() *Type {
return &Type{
Css: func() string {
type Text struct{}
func (t Text) Css() string {
var css strings.Builder
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;}`)
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)
},
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)
if err != nil {
body = []byte{}
@ -36,8 +38,10 @@ func RegisterText() *Type {
return fmt.Sprintf(`<a href="/%s"><textarea autofocus readonly>%s</textarea></a>`,
queryParams,
body)
},
Extensions: map[string]string{
}
func (t Text) Extensions() map[string]string {
return map[string]string{
`.css`: `text/css`,
`.csv`: `text/csv`,
`.html`: `text/html`,
@ -46,8 +50,11 @@ func RegisterText() *Type {
`.md`: `text/markdown`,
`.txt`: `text/plain`,
`.xml`: `application/xml`,
},
MimeTypes: []string{
}
}
func (t Text) MimeTypes() []string {
return []string{
`application/json`,
`application/xml`,
`text/css`,
@ -55,9 +62,11 @@ func RegisterText() *Type {
`text/javascript`,
`text/plain`,
`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 {
case errors.Is(err, os.ErrNotExist):
return false
@ -70,6 +79,4 @@ func RegisterText() *Type {
file.Read(head)
return utf8.Valid(head)
},
}
}

View File

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

View File

@ -9,9 +9,9 @@ import (
"strings"
)
func RegisterVideos() *Type {
return &Type{
Css: func() string {
type Video struct{}
func (t Video) Css() string {
var css strings.Builder
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%);}`)
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)
},
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>`,
queryParams,
fileUri,
mime,
fileName)
},
Extensions: map[string]string{
}
func (t Video) Extensions() map[string]string {
return map[string]string{
`.mp4`: `video/mp4`,
`.ogm`: `video/ogg`,
`.ogv`: `video/ogg`,
`.webm`: `video/webm`,
},
MimeTypes: []string{
}
}
func (t Video) MimeTypes() []string {
return []string{
`video/mp4`,
`video/ogg`,
`video/webm`,
},
Validate: func(filePath string) bool {
}
}
func (t Video) Validate(filePath string) bool {
return true
},
}
}