Rename formats package to types, replace Css string literal in type struct with func() string

This commit is contained in:
Seednode 2023-09-12 16:32:19 -05:00
parent 6fea978459
commit 732d944036
10 changed files with 128 additions and 87 deletions

View File

@ -20,7 +20,7 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"seedno.de/seednode/roulette/formats" "seedno.de/seednode/roulette/types"
) )
type maxConcurrency int type maxConcurrency int
@ -108,9 +108,9 @@ func preparePath(path string) string {
return MediaPrefix + path return MediaPrefix + path
} }
func appendPath(directory, path string, files *Files, stats *ScanStats, registeredFormats *formats.SupportedFormats, shouldCache bool) error { func appendPath(directory, path string, files *Files, stats *ScanStats, formats *types.Types, shouldCache bool) error {
if shouldCache { if shouldCache {
registered, _, _, err := formats.FileType(path, registeredFormats) registered, _, _, err := types.FileType(path, formats)
if err != nil { if err != nil {
return err return err
} }
@ -127,7 +127,7 @@ func appendPath(directory, path string, files *Files, stats *ScanStats, register
return nil return nil
} }
func appendPaths(path string, files *Files, filters *Filters, stats *ScanStats, types *formats.SupportedFormats) error { func appendPaths(path string, files *Files, filters *Filters, stats *ScanStats, formats *types.Types) error {
shouldCache := Cache && filters.IsEmpty() shouldCache := Cache && filters.IsEmpty()
absolutePath, err := filepath.Abs(path) absolutePath, err := filepath.Abs(path)
@ -158,7 +158,7 @@ func appendPaths(path string, files *Files, filters *Filters, stats *ScanStats,
filename, filename,
filters.includes[i], filters.includes[i],
) { ) {
err := appendPath(directory, path, files, stats, types, shouldCache) err := appendPath(directory, path, files, stats, formats, shouldCache)
if err != nil { if err != nil {
return err return err
} }
@ -172,7 +172,7 @@ func appendPaths(path string, files *Files, filters *Filters, stats *ScanStats,
return nil return nil
} }
err = appendPath(directory, path, files, stats, types, shouldCache) err = appendPath(directory, path, files, stats, formats, shouldCache)
if err != nil { if err != nil {
return err return err
} }
@ -180,8 +180,8 @@ func appendPaths(path string, files *Files, filters *Filters, stats *ScanStats,
return nil return nil
} }
func newFile(paths []string, filters *Filters, sortOrder string, Regexes *Regexes, index *FileIndex, registeredFormats *formats.SupportedFormats) (string, error) { func newFile(paths []string, filters *Filters, sortOrder string, Regexes *Regexes, index *FileIndex, formats *types.Types) (string, error) {
filePath, err := pickFile(paths, filters, sortOrder, index, registeredFormats) filePath, err := pickFile(paths, filters, sortOrder, index, formats)
if err != nil { if err != nil {
return "", nil return "", nil
} }
@ -195,7 +195,7 @@ func newFile(paths []string, filters *Filters, sortOrder string, Regexes *Regexe
switch { switch {
case sortOrder == "asc": case sortOrder == "asc":
filePath, err = tryExtensions(path, registeredFormats) filePath, err = tryExtensions(path, formats)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -203,7 +203,7 @@ func newFile(paths []string, filters *Filters, sortOrder string, Regexes *Regexe
for { for {
path.increment() path.increment()
filePath, err = tryExtensions(path, registeredFormats) filePath, err = tryExtensions(path, formats)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -211,7 +211,7 @@ func newFile(paths []string, filters *Filters, sortOrder string, Regexes *Regexe
if filePath == "" { if filePath == "" {
path.decrement() path.decrement()
filePath, err = tryExtensions(path, registeredFormats) filePath, err = tryExtensions(path, formats)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -224,7 +224,7 @@ func newFile(paths []string, filters *Filters, sortOrder string, Regexes *Regexe
return filePath, nil return filePath, nil
} }
func nextFile(filePath, sortOrder string, Regexes *Regexes, registeredFormats *formats.SupportedFormats) (string, error) { func nextFile(filePath, sortOrder string, Regexes *Regexes, formats *types.Types) (string, error) {
path, err := splitPath(filePath, Regexes) path, err := splitPath(filePath, Regexes)
if err != nil { if err != nil {
return "", err return "", err
@ -239,7 +239,7 @@ func nextFile(filePath, sortOrder string, Regexes *Regexes, registeredFormats *f
return "", nil return "", nil
} }
fileName, err := tryExtensions(path, registeredFormats) fileName, err := tryExtensions(path, formats)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -269,10 +269,10 @@ func splitPath(path string, Regexes *Regexes) (*Path, error) {
return &p, nil return &p, nil
} }
func tryExtensions(p *Path, registeredFormats *formats.SupportedFormats) (string, error) { func tryExtensions(p *Path, formats *types.Types) (string, error) {
var fileName string var fileName string
for _, format := range registeredFormats.Extensions { for _, format := range formats.Extensions {
for _, extension := range format.Extensions { for _, extension := range format.Extensions {
fileName = fmt.Sprintf("%s%.3d%s", p.base, p.number, extension) fileName = fmt.Sprintf("%s%.3d%s", p.base, p.number, extension)
} }
@ -326,7 +326,7 @@ func pathIsValid(filePath string, paths []string) bool {
} }
} }
func pathHasSupportedFiles(path string, registeredFormats *formats.SupportedFormats) (bool, error) { func pathHasSupportedFiles(path string, formats *types.Types) (bool, error) {
hasRegisteredFiles := make(chan bool, 1) hasRegisteredFiles := make(chan bool, 1)
err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error { err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error {
@ -338,7 +338,7 @@ func pathHasSupportedFiles(path string, registeredFormats *formats.SupportedForm
case !Recursive && info.IsDir() && p != path: case !Recursive && info.IsDir() && p != path:
return filepath.SkipDir return filepath.SkipDir
case !info.IsDir(): case !info.IsDir():
registered, _, _, err := formats.FileType(p, registeredFormats) registered, _, _, err := types.FileType(p, formats)
if err != nil { if err != nil {
return err return err
} }
@ -383,7 +383,7 @@ func pathCount(path string) (uint64, uint64, error) {
return files, directories, nil return files, directories, nil
} }
func scanPath(path string, files *Files, filters *Filters, stats *ScanStats, concurrency *Concurrency, types *formats.SupportedFormats) error { func scanPath(path string, files *Files, filters *Filters, stats *ScanStats, concurrency *Concurrency, formats *types.Types) error {
var wg sync.WaitGroup var wg sync.WaitGroup
err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error { err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error {
@ -410,7 +410,7 @@ func scanPath(path string, files *Files, filters *Filters, stats *ScanStats, con
fmt.Println(err) fmt.Println(err)
} }
err = appendPaths(path, files, filters, stats, types) err = appendPaths(path, files, filters, stats, formats)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
@ -444,7 +444,7 @@ func scanPath(path string, files *Files, filters *Filters, stats *ScanStats, con
return nil return nil
} }
func fileList(paths []string, filters *Filters, sort string, index *FileIndex, types *formats.SupportedFormats) ([]string, bool) { func fileList(paths []string, filters *Filters, sort string, index *FileIndex, formats *types.Types) ([]string, bool) {
if Cache && filters.IsEmpty() && !index.IsEmpty() { if Cache && filters.IsEmpty() && !index.IsEmpty() {
return index.Index(), true return index.Index(), true
} }
@ -483,7 +483,7 @@ func fileList(paths []string, filters *Filters, sort string, index *FileIndex, t
wg.Done() wg.Done()
}() }()
err := scanPath(paths[i], files, filters, stats, concurrency, types) err := scanPath(paths[i], files, filters, stats, concurrency, formats)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
@ -558,8 +558,8 @@ func prepareDirectories(files *Files, sort string) []string {
return directories return directories
} }
func pickFile(args []string, filters *Filters, sort string, index *FileIndex, registeredFormats *formats.SupportedFormats) (string, error) { func pickFile(args []string, filters *Filters, sort string, index *FileIndex, formats *types.Types) (string, error) {
fileList, fromCache := fileList(args, filters, sort, index, registeredFormats) fileList, fromCache := fileList(args, filters, sort, index, formats)
fileCount := len(fileList) fileCount := len(fileList)
if fileCount < 1 { if fileCount < 1 {
@ -586,7 +586,7 @@ func pickFile(args []string, filters *Filters, sort string, index *FileIndex, re
filePath := fileList[val] filePath := fileList[val]
if !fromCache { if !fromCache {
registered, _, _, err := formats.FileType(filePath, registeredFormats) registered, _, _, err := types.FileType(filePath, formats)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -618,7 +618,7 @@ func normalizePath(path string) (string, error) {
return absolutePath, nil return absolutePath, nil
} }
func normalizePaths(args []string, types *formats.SupportedFormats) ([]string, error) { func normalizePaths(args []string, formats *types.Types) ([]string, error) {
var paths []string var paths []string
var pathList strings.Builder var pathList strings.Builder
@ -632,7 +632,7 @@ func normalizePaths(args []string, types *formats.SupportedFormats) ([]string, e
pathMatches := (args[i] == path) pathMatches := (args[i] == path)
hasSupportedFiles, err := pathHasSupportedFiles(path, types) hasSupportedFiles, err := pathHasSupportedFiles(path, formats)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -20,7 +20,7 @@ import (
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
"github.com/klauspost/compress/zstd" "github.com/klauspost/compress/zstd"
"github.com/yosssi/gohtml" "github.com/yosssi/gohtml"
"seedno.de/seednode/roulette/formats" "seedno.de/seednode/roulette/types"
) )
type FileIndex struct { type FileIndex struct {
@ -66,12 +66,12 @@ func (i *FileIndex) setIndex(val []string) {
i.mutex.Unlock() i.mutex.Unlock()
} }
func (i *FileIndex) generateCache(args []string, registeredFormats *formats.SupportedFormats) { func (i *FileIndex) generateCache(args []string, formats *types.Types) {
i.mutex.Lock() i.mutex.Lock()
i.list = []string{} i.list = []string{}
i.mutex.Unlock() i.mutex.Unlock()
fileList(args, &Filters{}, "", i, registeredFormats) fileList(args, &Filters{}, "", i, formats)
if Cache && CacheFile != "" { if Cache && CacheFile != "" {
i.Export(CacheFile) i.Export(CacheFile)
@ -138,9 +138,9 @@ func (i *FileIndex) Import(path string) error {
return nil return nil
} }
func serveCacheClear(args []string, index *FileIndex, registeredFormats *formats.SupportedFormats) httprouter.Handle { func serveCacheClear(args []string, index *FileIndex, formats *types.Types) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
index.generateCache(args, registeredFormats) index.generateCache(args, formats)
w.Header().Set("Content-Type", "text/plain") w.Header().Set("Content-Type", "text/plain")

View File

@ -12,7 +12,7 @@ import (
) )
const ( const (
ReleaseVersion string = "0.70.3" ReleaseVersion string = "0.71.0"
) )
var ( var (
@ -45,7 +45,7 @@ var (
Use: "roulette <path> [path]...", Use: "roulette <path> [path]...",
Short: "Serves random media from the specified directories.", Short: "Serves random media from the specified directories.",
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) { PreRunE: func(cmd *cobra.Command, args []string) error {
// enable image support if no other flags are passed, to retain backwards compatibility // enable image support if no other flags are passed, to retain backwards compatibility
// to be replaced with MarkFlagsOneRequired on next spf13/cobra update // to be replaced with MarkFlagsOneRequired on next spf13/cobra update
if !(All || Audio || Flash || Images || Text || Videos) { if !(All || Audio || Flash || Images || Text || Videos) {
@ -59,9 +59,11 @@ var (
if RefreshInterval != "" { if RefreshInterval != "" {
interval, err := time.ParseDuration(RefreshInterval) interval, err := time.ParseDuration(RefreshInterval)
if err != nil || interval < 500*time.Millisecond { if err != nil || interval < 500*time.Millisecond {
log.Fatal(ErrIncorrectRefreshInterval) return ErrIncorrectRefreshInterval
} }
} }
return nil
}, },
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
err := ServePage(args) err := ServePage(args)

View File

@ -26,7 +26,7 @@ import (
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
"github.com/yosssi/gohtml" "github.com/yosssi/gohtml"
"seedno.de/seednode/roulette/formats" "seedno.de/seednode/roulette/types"
) )
const ( const (
@ -127,7 +127,7 @@ func serveStaticFile(paths []string, stats *ServeStats, index *FileIndex) httpro
} }
} }
func serveRoot(paths []string, Regexes *Regexes, index *FileIndex, registeredFormats *formats.SupportedFormats) httprouter.Handle { func serveRoot(paths []string, Regexes *Regexes, index *FileIndex, formats *types.Types) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
refererUri, err := stripQueryParams(refererToUri(r.Referer())) refererUri, err := stripQueryParams(refererToUri(r.Referer()))
if err != nil { if err != nil {
@ -152,7 +152,7 @@ func serveRoot(paths []string, Regexes *Regexes, index *FileIndex, registeredFor
var filePath string var filePath string
if refererUri != "" { if refererUri != "" {
filePath, err = nextFile(strippedRefererUri, sortOrder, Regexes, registeredFormats) filePath, err = nextFile(strippedRefererUri, sortOrder, Regexes, formats)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -174,7 +174,7 @@ func serveRoot(paths []string, Regexes *Regexes, index *FileIndex, registeredFor
break loop break loop
} }
filePath, err = newFile(paths, filters, sortOrder, Regexes, index, registeredFormats) filePath, err = newFile(paths, filters, sortOrder, Regexes, index, formats)
switch { switch {
case err != nil && err == ErrNoMediaFound: case err != nil && err == ErrNoMediaFound:
notFound(w, r, filePath) notFound(w, r, filePath)
@ -200,7 +200,7 @@ func serveRoot(paths []string, Regexes *Regexes, index *FileIndex, registeredFor
} }
} }
func serveMedia(paths []string, Regexes *Regexes, index *FileIndex, registeredFormats *formats.SupportedFormats) httprouter.Handle { func serveMedia(paths []string, Regexes *Regexes, index *FileIndex, formats *types.Types) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
filters := &Filters{ filters := &Filters{
includes: splitQueryParams(r.URL.Query().Get("include"), Regexes), includes: splitQueryParams(r.URL.Query().Get("include"), Regexes),
@ -229,7 +229,7 @@ func serveMedia(paths []string, Regexes *Regexes, index *FileIndex, registeredFo
return return
} }
registered, fileType, mimeType, err := formats.FileType(filePath, registeredFormats) registered, fileType, mimeType, err := types.FileType(filePath, formats)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -257,12 +257,7 @@ func serveMedia(paths []string, Regexes *Regexes, index *FileIndex, registeredFo
var htmlBody strings.Builder var htmlBody strings.Builder
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(fmt.Sprintf(`<style>%s</style>`, fileType.Css()))
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(`position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);}`)
htmlBody.WriteString(fileType.Css)
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" {
@ -316,32 +311,32 @@ func ServePage(args []string) error {
mux := httprouter.New() mux := httprouter.New()
registeredFormats := &formats.SupportedFormats{ formats := &types.Types{
Extensions: make(map[string]*formats.SupportedFormat), Extensions: make(map[string]*types.Type),
MimeTypes: make(map[string]*formats.SupportedFormat), MimeTypes: make(map[string]*types.Type),
} }
if Audio || All { if Audio || All {
registeredFormats.Add(formats.RegisterAudioFormats()) formats.Add(types.RegisterAudio())
} }
if Flash || All { if Flash || All {
registeredFormats.Add(formats.RegisterFlashFormats()) formats.Add(types.RegisterFlash())
} }
if Images || All { if Images || All {
registeredFormats.Add(formats.RegisterImageFormats()) formats.Add(types.RegisterImages())
} }
if Text || All { if Text || All {
registeredFormats.Add(formats.RegisterTextFormats()) formats.Add(types.RegisterText())
} }
if Videos || All { if Videos || All {
registeredFormats.Add(formats.RegisterVideoFormats()) formats.Add(types.RegisterVideos())
} }
paths, err := normalizePaths(args, registeredFormats) paths, err := normalizePaths(args, formats)
if err != nil { if err != nil {
return err return err
} }
@ -382,13 +377,13 @@ func ServePage(args []string) error {
mux.PanicHandler = serverErrorHandler() mux.PanicHandler = serverErrorHandler()
mux.GET("/", serveRoot(paths, regexes, index, registeredFormats)) mux.GET("/", serveRoot(paths, regexes, index, formats))
mux.GET("/favicons/*favicon", serveFavicons()) mux.GET("/favicons/*favicon", serveFavicons())
mux.GET("/favicon.ico", serveFavicons()) mux.GET("/favicon.ico", serveFavicons())
mux.GET(MediaPrefix+"/*media", serveMedia(paths, regexes, index, registeredFormats)) mux.GET(MediaPrefix+"/*media", serveMedia(paths, regexes, index, formats))
mux.GET(SourcePrefix+"/*static", serveStaticFile(paths, stats, index)) mux.GET(SourcePrefix+"/*static", serveStaticFile(paths, stats, index))
@ -405,10 +400,10 @@ func ServePage(args []string) error {
} }
if !skipIndex { if !skipIndex {
index.generateCache(args, registeredFormats) index.generateCache(args, formats)
} }
mux.GET("/clear_cache", serveCacheClear(args, index, registeredFormats)) mux.GET("/clear_cache", serveCacheClear(args, index, formats))
} }
if Index { if Index {

View File

@ -2,15 +2,23 @@
Copyright © 2023 Seednode <seednode@seedno.de> Copyright © 2023 Seednode <seednode@seedno.de>
*/ */
package formats package types
import ( import (
"fmt" "fmt"
"strings"
) )
func RegisterAudioFormats() *SupportedFormat { func RegisterAudio() *Type {
return &SupportedFormat{ return &Type{
Css: ``, Css: func() 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 { 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

@ -2,16 +2,23 @@
Copyright © 2023 Seednode <seednode@seedno.de> Copyright © 2023 Seednode <seednode@seedno.de>
*/ */
package formats package types
import ( import (
"fmt" "fmt"
"strings" "strings"
) )
func RegisterFlashFormats() *SupportedFormat { func RegisterFlash() *Type {
return &SupportedFormat{ return &Type{
Css: ``, Css: func() 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 { 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

@ -2,7 +2,7 @@
Copyright © 2023 Seednode <seednode@seedno.de> Copyright © 2023 Seednode <seednode@seedno.de>
*/ */
package formats package types
import ( import (
"errors" "errors"
@ -12,6 +12,7 @@ import (
_ "image/jpeg" _ "image/jpeg"
_ "image/png" _ "image/png"
"os" "os"
"strings"
_ "golang.org/x/image/bmp" _ "golang.org/x/image/bmp"
_ "golang.org/x/image/webp" _ "golang.org/x/image/webp"
@ -22,9 +23,18 @@ type Dimensions struct {
Height int Height int
} }
func RegisterImageFormats() *SupportedFormat { func RegisterImages() *Type {
return &SupportedFormat{ return &Type{
Css: ``, Css: func() 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;}`)
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%);}`)
return css.String()
},
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 {

View File

@ -2,18 +2,27 @@
Copyright © 2023 Seednode <seednode@seedno.de> Copyright © 2023 Seednode <seednode@seedno.de>
*/ */
package formats package types
import ( import (
"errors" "errors"
"fmt" "fmt"
"os" "os"
"strings"
"unicode/utf8" "unicode/utf8"
) )
func RegisterTextFormats() *SupportedFormat { func RegisterText() *Type {
return &SupportedFormat{ return &Type{
Css: `pre{margin:.5rem;}`, Css: func() 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;}`)
css.WriteString(`pre{margin:.5rem;}`)
return css.String()
},
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

@ -2,7 +2,7 @@
Copyright © 2023 Seednode <seednode@seedno.de> Copyright © 2023 Seednode <seednode@seedno.de>
*/ */
package formats package types
import ( import (
"errors" "errors"
@ -11,8 +11,8 @@ import (
"path/filepath" "path/filepath"
) )
type SupportedFormat struct { type Type struct {
Css string Css func() string
Title func(queryParams, fileUri, filePath, fileName, mime string) string Title func(queryParams, fileUri, filePath, fileName, mime string) string
Body func(queryParams, fileUri, filePath, fileName, mime string) string Body func(queryParams, fileUri, filePath, fileName, mime string) string
Extensions []string Extensions []string
@ -20,12 +20,12 @@ type SupportedFormat struct {
Validate func(filePath string) bool Validate func(filePath string) bool
} }
type SupportedFormats struct { type Types struct {
Extensions map[string]*SupportedFormat Extensions map[string]*Type
MimeTypes map[string]*SupportedFormat MimeTypes map[string]*Type
} }
func (s *SupportedFormats) Add(t *SupportedFormat) { func (s *Types) Add(t *Type) {
for _, v := range t.Extensions { for _, v := range t.Extensions {
_, exists := s.Extensions[v] _, exists := s.Extensions[v]
if !exists { if !exists {
@ -41,7 +41,7 @@ func (s *SupportedFormats) Add(t *SupportedFormat) {
} }
} }
func FileType(path string, registeredFormats *SupportedFormats) (bool, *SupportedFormat, 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

@ -2,15 +2,25 @@
Copyright © 2023 Seednode <seednode@seedno.de> Copyright © 2023 Seednode <seednode@seedno.de>
*/ */
package formats package types
import ( import (
"fmt" "fmt"
"strings"
) )
func RegisterVideoFormats() *SupportedFormat { func RegisterVideos() *Type {
return &SupportedFormat{ return &Type{
Css: ``, Css: func() 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;}`)
css.WriteString(`video{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%);}`)
return css.String()
},
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)
}, },