Compare commits
No commits in common. "cbf721845300a1e4869d6c82d8a1edfb899632d6" and "6fea978459faf555c03c18099502055b4b1b85cb" have entirely different histories.
cbf7218453
...
6fea978459
18
README.md
18
README.md
|
@ -78,13 +78,9 @@ The cache can be regenerated at any time by accessing the `/clear_cache` endpoin
|
|||
|
||||
If `--cache-file` is set, the cache will be loaded from the specified file on start, and written to the file whenever it is re-generated.
|
||||
|
||||
If the `-i|--index` flag is passed, four additional endpoints are registered.
|
||||
If the `-i|--index` flag is passed, two additional endpoints—`/html` and `/json`—are registered.
|
||||
|
||||
The first of these—`/html` and `/json`—return the contents of the index, in HTML and JSON formats respectively.
|
||||
|
||||
This can prove useful when confirming whether the index is generated successfully, or whether a given file is in the index.
|
||||
|
||||
The other two endpoints—`/extensions` and `/mime_types`—return the registered file types.
|
||||
When accessed, these endpoints return the contents of the index, in HTML and JSON formats respectively. This can prove useful when confirming whether the index is generated successfully, or whether a given file is in the index.
|
||||
|
||||
## Statistics
|
||||
|
||||
|
@ -117,13 +113,13 @@ Flags:
|
|||
-c, --cache generate directory cache at startup
|
||||
--cache-file string path to optional persistent cache file
|
||||
-f, --filter enable filtering
|
||||
--flash enable support for shockwave flash files (via ruffle.rs)
|
||||
--flash enable support for shockwave flash files (via ruffle.rs) (default true)
|
||||
-h, --help help for roulette
|
||||
--images enable support for image files
|
||||
--images enable support for image files (default true)
|
||||
-i, --index expose index endpoints
|
||||
--maximum-files uint32 skip directories with file counts above this value (default 4294967295)
|
||||
--minimum-files uint32 skip directories with file counts below this value (default 1)
|
||||
--page-length uint32 pagination length for statistics and debug pages
|
||||
--maximum-files uint skip directories with file counts above this value (default 18446744073709551615)
|
||||
--minimum-files uint skip directories with file counts below this value (default 1)
|
||||
--page-length uint pagination length for statistics and debug pages
|
||||
-p, --port uint16 port to listen on (default 8080)
|
||||
--profile register net/http/pprof handlers
|
||||
-r, --recursive recurse into subdirectories
|
||||
|
|
78
cmd/files.go
78
cmd/files.go
|
@ -20,7 +20,7 @@ import (
|
|||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"seedno.de/seednode/roulette/types"
|
||||
"seedno.de/seednode/roulette/formats"
|
||||
)
|
||||
|
||||
type maxConcurrency int
|
||||
|
@ -53,10 +53,10 @@ func (f *Files) Append(directory, path string) {
|
|||
}
|
||||
|
||||
type ScanStats struct {
|
||||
filesMatched atomic.Uint32
|
||||
filesSkipped atomic.Uint32
|
||||
directoriesMatched atomic.Uint32
|
||||
directoriesSkipped atomic.Uint32
|
||||
filesMatched atomic.Uint64
|
||||
filesSkipped atomic.Uint64
|
||||
directoriesMatched atomic.Uint64
|
||||
directoriesSkipped atomic.Uint64
|
||||
}
|
||||
|
||||
type Path struct {
|
||||
|
@ -108,9 +108,9 @@ func preparePath(path string) string {
|
|||
return MediaPrefix + path
|
||||
}
|
||||
|
||||
func appendPath(directory, path string, files *Files, stats *ScanStats, formats *types.Types, shouldCache bool) error {
|
||||
func appendPath(directory, path string, files *Files, stats *ScanStats, registeredFormats *formats.SupportedFormats, shouldCache bool) error {
|
||||
if shouldCache {
|
||||
registered, _, _, err := types.FileType(path, formats)
|
||||
registered, _, _, err := formats.FileType(path, registeredFormats)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -127,7 +127,7 @@ func appendPath(directory, path string, files *Files, stats *ScanStats, formats
|
|||
return nil
|
||||
}
|
||||
|
||||
func appendPaths(path string, files *Files, filters *Filters, stats *ScanStats, formats *types.Types) error {
|
||||
func appendPaths(path string, files *Files, filters *Filters, stats *ScanStats, types *formats.SupportedFormats) error {
|
||||
shouldCache := Cache && filters.IsEmpty()
|
||||
|
||||
absolutePath, err := filepath.Abs(path)
|
||||
|
@ -158,7 +158,7 @@ func appendPaths(path string, files *Files, filters *Filters, stats *ScanStats,
|
|||
filename,
|
||||
filters.includes[i],
|
||||
) {
|
||||
err := appendPath(directory, path, files, stats, formats, shouldCache)
|
||||
err := appendPath(directory, path, files, stats, types, shouldCache)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -172,7 +172,7 @@ func appendPaths(path string, files *Files, filters *Filters, stats *ScanStats,
|
|||
return nil
|
||||
}
|
||||
|
||||
err = appendPath(directory, path, files, stats, formats, shouldCache)
|
||||
err = appendPath(directory, path, files, stats, types, shouldCache)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -180,8 +180,8 @@ func appendPaths(path string, files *Files, filters *Filters, stats *ScanStats,
|
|||
return nil
|
||||
}
|
||||
|
||||
func newFile(paths []string, filters *Filters, sortOrder string, Regexes *Regexes, index *FileIndex, formats *types.Types) (string, error) {
|
||||
filePath, err := pickFile(paths, filters, sortOrder, index, formats)
|
||||
func newFile(paths []string, filters *Filters, sortOrder string, Regexes *Regexes, index *FileIndex, registeredFormats *formats.SupportedFormats) (string, error) {
|
||||
filePath, err := pickFile(paths, filters, sortOrder, index, registeredFormats)
|
||||
if err != nil {
|
||||
return "", nil
|
||||
}
|
||||
|
@ -195,7 +195,7 @@ func newFile(paths []string, filters *Filters, sortOrder string, Regexes *Regexe
|
|||
|
||||
switch {
|
||||
case sortOrder == "asc":
|
||||
filePath, err = tryExtensions(path, formats)
|
||||
filePath, err = tryExtensions(path, registeredFormats)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -203,7 +203,7 @@ func newFile(paths []string, filters *Filters, sortOrder string, Regexes *Regexe
|
|||
for {
|
||||
path.increment()
|
||||
|
||||
filePath, err = tryExtensions(path, formats)
|
||||
filePath, err = tryExtensions(path, registeredFormats)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -211,7 +211,7 @@ func newFile(paths []string, filters *Filters, sortOrder string, Regexes *Regexe
|
|||
if filePath == "" {
|
||||
path.decrement()
|
||||
|
||||
filePath, err = tryExtensions(path, formats)
|
||||
filePath, err = tryExtensions(path, registeredFormats)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -224,7 +224,7 @@ func newFile(paths []string, filters *Filters, sortOrder string, Regexes *Regexe
|
|||
return filePath, nil
|
||||
}
|
||||
|
||||
func nextFile(filePath, sortOrder string, Regexes *Regexes, formats *types.Types) (string, error) {
|
||||
func nextFile(filePath, sortOrder string, Regexes *Regexes, registeredFormats *formats.SupportedFormats) (string, error) {
|
||||
path, err := splitPath(filePath, Regexes)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
@ -239,7 +239,7 @@ func nextFile(filePath, sortOrder string, Regexes *Regexes, formats *types.Types
|
|||
return "", nil
|
||||
}
|
||||
|
||||
fileName, err := tryExtensions(path, formats)
|
||||
fileName, err := tryExtensions(path, registeredFormats)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -269,11 +269,13 @@ func splitPath(path string, Regexes *Regexes) (*Path, error) {
|
|||
return &p, nil
|
||||
}
|
||||
|
||||
func tryExtensions(p *Path, formats *types.Types) (string, error) {
|
||||
func tryExtensions(p *Path, registeredFormats *formats.SupportedFormats) (string, error) {
|
||||
var fileName string
|
||||
|
||||
for extension := range formats.Extensions {
|
||||
fileName = fmt.Sprintf("%s%.3d%s", p.base, p.number, extension)
|
||||
for _, format := range registeredFormats.Extensions {
|
||||
for _, extension := range format.Extensions {
|
||||
fileName = fmt.Sprintf("%s%.3d%s", p.base, p.number, extension)
|
||||
}
|
||||
|
||||
exists, err := fileExists(fileName)
|
||||
if err != nil {
|
||||
|
@ -324,7 +326,7 @@ func pathIsValid(filePath string, paths []string) bool {
|
|||
}
|
||||
}
|
||||
|
||||
func pathHasSupportedFiles(path string, formats *types.Types) (bool, error) {
|
||||
func pathHasSupportedFiles(path string, registeredFormats *formats.SupportedFormats) (bool, error) {
|
||||
hasRegisteredFiles := make(chan bool, 1)
|
||||
|
||||
err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error {
|
||||
|
@ -336,7 +338,7 @@ func pathHasSupportedFiles(path string, formats *types.Types) (bool, error) {
|
|||
case !Recursive && info.IsDir() && p != path:
|
||||
return filepath.SkipDir
|
||||
case !info.IsDir():
|
||||
registered, _, _, err := types.FileType(p, formats)
|
||||
registered, _, _, err := formats.FileType(p, registeredFormats)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -361,9 +363,9 @@ func pathHasSupportedFiles(path string, formats *types.Types) (bool, error) {
|
|||
}
|
||||
}
|
||||
|
||||
func pathCount(path string) (uint32, uint32, error) {
|
||||
var directories uint32 = 0
|
||||
var files uint32 = 0
|
||||
func pathCount(path string) (uint64, uint64, error) {
|
||||
var directories uint64 = 0
|
||||
var files uint64 = 0
|
||||
|
||||
nodes, err := os.ReadDir(path)
|
||||
if err != nil {
|
||||
|
@ -381,7 +383,7 @@ func pathCount(path string) (uint32, uint32, error) {
|
|||
return files, directories, nil
|
||||
}
|
||||
|
||||
func scanPath(path string, files *Files, filters *Filters, stats *ScanStats, concurrency *Concurrency, formats *types.Types) error {
|
||||
func scanPath(path string, files *Files, filters *Filters, stats *ScanStats, concurrency *Concurrency, types *formats.SupportedFormats) error {
|
||||
var wg sync.WaitGroup
|
||||
|
||||
err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error {
|
||||
|
@ -408,7 +410,7 @@ func scanPath(path string, files *Files, filters *Filters, stats *ScanStats, con
|
|||
fmt.Println(err)
|
||||
}
|
||||
|
||||
err = appendPaths(path, files, filters, stats, formats)
|
||||
err = appendPaths(path, files, filters, stats, types)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
@ -442,7 +444,7 @@ func scanPath(path string, files *Files, filters *Filters, stats *ScanStats, con
|
|||
return nil
|
||||
}
|
||||
|
||||
func fileList(paths []string, filters *Filters, sort string, index *FileIndex, formats *types.Types) ([]string, bool) {
|
||||
func fileList(paths []string, filters *Filters, sort string, index *FileIndex, types *formats.SupportedFormats) ([]string, bool) {
|
||||
if Cache && filters.IsEmpty() && !index.IsEmpty() {
|
||||
return index.Index(), true
|
||||
}
|
||||
|
@ -455,10 +457,10 @@ func fileList(paths []string, filters *Filters, sort string, index *FileIndex, f
|
|||
}
|
||||
|
||||
stats := &ScanStats{
|
||||
filesMatched: atomic.Uint32{},
|
||||
filesSkipped: atomic.Uint32{},
|
||||
directoriesMatched: atomic.Uint32{},
|
||||
directoriesSkipped: atomic.Uint32{},
|
||||
filesMatched: atomic.Uint64{},
|
||||
filesSkipped: atomic.Uint64{},
|
||||
directoriesMatched: atomic.Uint64{},
|
||||
directoriesSkipped: atomic.Uint64{},
|
||||
}
|
||||
|
||||
concurrency := &Concurrency{
|
||||
|
@ -481,7 +483,7 @@ func fileList(paths []string, filters *Filters, sort string, index *FileIndex, f
|
|||
wg.Done()
|
||||
}()
|
||||
|
||||
err := scanPath(paths[i], files, filters, stats, concurrency, formats)
|
||||
err := scanPath(paths[i], files, filters, stats, concurrency, types)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
@ -556,8 +558,8 @@ func prepareDirectories(files *Files, sort string) []string {
|
|||
return directories
|
||||
}
|
||||
|
||||
func pickFile(args []string, filters *Filters, sort string, index *FileIndex, formats *types.Types) (string, error) {
|
||||
fileList, fromCache := fileList(args, filters, sort, index, formats)
|
||||
func pickFile(args []string, filters *Filters, sort string, index *FileIndex, registeredFormats *formats.SupportedFormats) (string, error) {
|
||||
fileList, fromCache := fileList(args, filters, sort, index, registeredFormats)
|
||||
|
||||
fileCount := len(fileList)
|
||||
if fileCount < 1 {
|
||||
|
@ -584,7 +586,7 @@ func pickFile(args []string, filters *Filters, sort string, index *FileIndex, fo
|
|||
filePath := fileList[val]
|
||||
|
||||
if !fromCache {
|
||||
registered, _, _, err := types.FileType(filePath, formats)
|
||||
registered, _, _, err := formats.FileType(filePath, registeredFormats)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -616,7 +618,7 @@ func normalizePath(path string) (string, error) {
|
|||
return absolutePath, nil
|
||||
}
|
||||
|
||||
func normalizePaths(args []string, formats *types.Types) ([]string, error) {
|
||||
func normalizePaths(args []string, types *formats.SupportedFormats) ([]string, error) {
|
||||
var paths []string
|
||||
|
||||
var pathList strings.Builder
|
||||
|
@ -630,7 +632,7 @@ func normalizePaths(args []string, formats *types.Types) ([]string, error) {
|
|||
|
||||
pathMatches := (args[i] == path)
|
||||
|
||||
hasSupportedFiles, err := pathHasSupportedFiles(path, formats)
|
||||
hasSupportedFiles, err := pathHasSupportedFiles(path, types)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
87
cmd/index.go
87
cmd/index.go
|
@ -11,7 +11,6 @@ import (
|
|||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"slices"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -21,7 +20,7 @@ import (
|
|||
"github.com/julienschmidt/httprouter"
|
||||
"github.com/klauspost/compress/zstd"
|
||||
"github.com/yosssi/gohtml"
|
||||
"seedno.de/seednode/roulette/types"
|
||||
"seedno.de/seednode/roulette/formats"
|
||||
)
|
||||
|
||||
type FileIndex struct {
|
||||
|
@ -67,12 +66,12 @@ func (i *FileIndex) setIndex(val []string) {
|
|||
i.mutex.Unlock()
|
||||
}
|
||||
|
||||
func (i *FileIndex) generateCache(args []string, formats *types.Types) {
|
||||
func (i *FileIndex) generateCache(args []string, registeredFormats *formats.SupportedFormats) {
|
||||
i.mutex.Lock()
|
||||
i.list = []string{}
|
||||
i.mutex.Unlock()
|
||||
|
||||
fileList(args, &Filters{}, "", i, formats)
|
||||
fileList(args, &Filters{}, "", i, registeredFormats)
|
||||
|
||||
if Cache && CacheFile != "" {
|
||||
i.Export(CacheFile)
|
||||
|
@ -139,9 +138,9 @@ func (i *FileIndex) Import(path string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func serveCacheClear(args []string, index *FileIndex, formats *types.Types) httprouter.Handle {
|
||||
func serveCacheClear(args []string, index *FileIndex, registeredFormats *formats.SupportedFormats) httprouter.Handle {
|
||||
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
index.generateCache(args, formats)
|
||||
index.generateCache(args, registeredFormats)
|
||||
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
|
||||
|
@ -317,79 +316,3 @@ func serveIndexJson(args []string, index *FileIndex) httprouter.Handle {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func serveExtensions(formats *types.Types) httprouter.Handle {
|
||||
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
|
||||
startTime := time.Now()
|
||||
|
||||
var output strings.Builder
|
||||
|
||||
extensions := make([]string, len(formats.Extensions))
|
||||
|
||||
i := 0
|
||||
|
||||
for k := range formats.Extensions {
|
||||
extensions[i] = k
|
||||
i++
|
||||
}
|
||||
|
||||
slices.Sort(extensions)
|
||||
|
||||
for _, v := range extensions {
|
||||
output.WriteString(v + "\n")
|
||||
}
|
||||
|
||||
response := []byte(output.String())
|
||||
|
||||
w.Write(response)
|
||||
|
||||
if Verbose {
|
||||
fmt.Printf("%s | Served registered extensions list (%s) to %s in %s\n",
|
||||
startTime.Format(LogDate),
|
||||
humanReadableSize(len(response)),
|
||||
realIP(r),
|
||||
time.Since(startTime).Round(time.Microsecond),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func serveMimeTypes(formats *types.Types) httprouter.Handle {
|
||||
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
|
||||
startTime := time.Now()
|
||||
|
||||
var output strings.Builder
|
||||
|
||||
mimeTypes := make([]string, len(formats.MimeTypes))
|
||||
|
||||
i := 0
|
||||
|
||||
for k := range formats.MimeTypes {
|
||||
mimeTypes[i] = k
|
||||
i++
|
||||
}
|
||||
|
||||
slices.Sort(mimeTypes)
|
||||
|
||||
for _, v := range mimeTypes {
|
||||
output.WriteString(v + "\n")
|
||||
}
|
||||
|
||||
response := []byte(output.String())
|
||||
|
||||
w.Write(response)
|
||||
|
||||
if Verbose {
|
||||
fmt.Printf("%s | Served registered MIME types list (%s) to %s in %s\n",
|
||||
startTime.Format(LogDate),
|
||||
humanReadableSize(len(response)),
|
||||
realIP(r),
|
||||
time.Since(startTime).Round(time.Microsecond),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
26
cmd/root.go
26
cmd/root.go
|
@ -12,7 +12,7 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
ReleaseVersion string = "0.75.0"
|
||||
ReleaseVersion string = "0.70.3"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -25,9 +25,9 @@ var (
|
|||
Flash bool
|
||||
Images bool
|
||||
Index bool
|
||||
MaximumFileCount uint32
|
||||
MinimumFileCount uint32
|
||||
PageLength uint32
|
||||
MaximumFileCount uint64
|
||||
MinimumFileCount uint64
|
||||
PageLength uint64
|
||||
Port uint16
|
||||
Profile bool
|
||||
Recursive bool
|
||||
|
@ -45,7 +45,13 @@ var (
|
|||
Use: "roulette <path> [path]...",
|
||||
Short: "Serves random media from the specified directories.",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
// enable image support if no other flags are passed, to retain backwards compatibility
|
||||
// to be replaced with MarkFlagsOneRequired on next spf13/cobra update
|
||||
if !(All || Audio || Flash || Images || Text || Videos) {
|
||||
Images = true
|
||||
}
|
||||
|
||||
if Index {
|
||||
cmd.MarkFlagRequired("cache")
|
||||
}
|
||||
|
@ -53,11 +59,9 @@ var (
|
|||
if RefreshInterval != "" {
|
||||
interval, err := time.ParseDuration(RefreshInterval)
|
||||
if err != nil || interval < 500*time.Millisecond {
|
||||
return ErrIncorrectRefreshInterval
|
||||
log.Fatal(ErrIncorrectRefreshInterval)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
err := ServePage(args)
|
||||
|
@ -87,9 +91,9 @@ func init() {
|
|||
rootCmd.Flags().BoolVar(&Flash, "flash", false, "enable support for shockwave flash files (via ruffle.rs)")
|
||||
rootCmd.Flags().BoolVar(&Images, "images", false, "enable support for image files")
|
||||
rootCmd.Flags().BoolVarP(&Index, "index", "i", false, "expose index endpoints")
|
||||
rootCmd.Flags().Uint32Var(&MaximumFileCount, "maximum-files", 1<<32-1, "skip directories with file counts above this value")
|
||||
rootCmd.Flags().Uint32Var(&MinimumFileCount, "minimum-files", 1, "skip directories with file counts below this value")
|
||||
rootCmd.Flags().Uint32Var(&PageLength, "page-length", 0, "pagination length for statistics and debug pages")
|
||||
rootCmd.Flags().Uint64Var(&MaximumFileCount, "maximum-files", 1<<64-1, "skip directories with file counts above this value")
|
||||
rootCmd.Flags().Uint64Var(&MinimumFileCount, "minimum-files", 1, "skip directories with file counts below this value")
|
||||
rootCmd.Flags().Uint64Var(&PageLength, "page-length", 0, "pagination length for statistics and debug pages")
|
||||
rootCmd.Flags().Uint16VarP(&Port, "port", "p", 8080, "port to listen on")
|
||||
rootCmd.Flags().BoolVar(&Profile, "profile", false, "register net/http/pprof handlers")
|
||||
rootCmd.Flags().BoolVarP(&Recursive, "recursive", "r", false, "recurse into subdirectories")
|
||||
|
|
57
cmd/web.go
57
cmd/web.go
|
@ -26,7 +26,7 @@ import (
|
|||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
"github.com/yosssi/gohtml"
|
||||
"seedno.de/seednode/roulette/types"
|
||||
"seedno.de/seednode/roulette/formats"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -127,7 +127,7 @@ func serveStaticFile(paths []string, stats *ServeStats, index *FileIndex) httpro
|
|||
}
|
||||
}
|
||||
|
||||
func serveRoot(paths []string, Regexes *Regexes, index *FileIndex, formats *types.Types) httprouter.Handle {
|
||||
func serveRoot(paths []string, Regexes *Regexes, index *FileIndex, registeredFormats *formats.SupportedFormats) httprouter.Handle {
|
||||
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
refererUri, err := stripQueryParams(refererToUri(r.Referer()))
|
||||
if err != nil {
|
||||
|
@ -152,7 +152,7 @@ func serveRoot(paths []string, Regexes *Regexes, index *FileIndex, formats *type
|
|||
var filePath string
|
||||
|
||||
if refererUri != "" {
|
||||
filePath, err = nextFile(strippedRefererUri, sortOrder, Regexes, formats)
|
||||
filePath, err = nextFile(strippedRefererUri, sortOrder, Regexes, registeredFormats)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
||||
|
@ -174,7 +174,7 @@ func serveRoot(paths []string, Regexes *Regexes, index *FileIndex, formats *type
|
|||
break loop
|
||||
}
|
||||
|
||||
filePath, err = newFile(paths, filters, sortOrder, Regexes, index, formats)
|
||||
filePath, err = newFile(paths, filters, sortOrder, Regexes, index, registeredFormats)
|
||||
switch {
|
||||
case err != nil && err == ErrNoMediaFound:
|
||||
notFound(w, r, filePath)
|
||||
|
@ -200,7 +200,7 @@ func serveRoot(paths []string, Regexes *Regexes, index *FileIndex, formats *type
|
|||
}
|
||||
}
|
||||
|
||||
func serveMedia(paths []string, Regexes *Regexes, index *FileIndex, formats *types.Types) httprouter.Handle {
|
||||
func serveMedia(paths []string, Regexes *Regexes, index *FileIndex, registeredFormats *formats.SupportedFormats) httprouter.Handle {
|
||||
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
filters := &Filters{
|
||||
includes: splitQueryParams(r.URL.Query().Get("include"), Regexes),
|
||||
|
@ -229,7 +229,7 @@ func serveMedia(paths []string, Regexes *Regexes, index *FileIndex, formats *typ
|
|||
return
|
||||
}
|
||||
|
||||
registered, fileType, mimeType, err := types.FileType(filePath, formats)
|
||||
registered, fileType, mimeType, err := formats.FileType(filePath, registeredFormats)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
||||
|
@ -257,7 +257,12 @@ func serveMedia(paths []string, Regexes *Regexes, index *FileIndex, formats *typ
|
|||
var htmlBody strings.Builder
|
||||
htmlBody.WriteString(`<!DOCTYPE html><html lang="en"><head>`)
|
||||
htmlBody.WriteString(FaviconHtml)
|
||||
htmlBody.WriteString(fmt.Sprintf(`<style>%s</style>`, fileType.Css()))
|
||||
htmlBody.WriteString(`<style>html,body{margin:0;padding:0;height:100%;}`)
|
||||
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(`</head><body>`)
|
||||
if refreshInterval != "0ms" {
|
||||
|
@ -311,34 +316,32 @@ func ServePage(args []string) error {
|
|||
|
||||
mux := httprouter.New()
|
||||
|
||||
formats := &types.Types{
|
||||
Extensions: make(map[string]string),
|
||||
MimeTypes: make(map[string]types.Type),
|
||||
registeredFormats := &formats.SupportedFormats{
|
||||
Extensions: make(map[string]*formats.SupportedFormat),
|
||||
MimeTypes: make(map[string]*formats.SupportedFormat),
|
||||
}
|
||||
|
||||
if Audio || All {
|
||||
formats.Add(types.Audio{})
|
||||
registeredFormats.Add(formats.RegisterAudioFormats())
|
||||
}
|
||||
|
||||
if Flash || All {
|
||||
formats.Add(types.Flash{})
|
||||
registeredFormats.Add(formats.RegisterFlashFormats())
|
||||
}
|
||||
|
||||
if Images || All {
|
||||
registeredFormats.Add(formats.RegisterImageFormats())
|
||||
}
|
||||
|
||||
if Text || All {
|
||||
formats.Add(types.Text{})
|
||||
registeredFormats.Add(formats.RegisterTextFormats())
|
||||
}
|
||||
|
||||
if Videos || All {
|
||||
formats.Add(types.Video{})
|
||||
registeredFormats.Add(formats.RegisterVideoFormats())
|
||||
}
|
||||
|
||||
// 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.Extensions) == 0 {
|
||||
formats.Add(types.Images{})
|
||||
}
|
||||
|
||||
paths, err := normalizePaths(args, formats)
|
||||
paths, err := normalizePaths(args, registeredFormats)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -379,13 +382,13 @@ func ServePage(args []string) error {
|
|||
|
||||
mux.PanicHandler = serverErrorHandler()
|
||||
|
||||
mux.GET("/", serveRoot(paths, regexes, index, formats))
|
||||
mux.GET("/", serveRoot(paths, regexes, index, registeredFormats))
|
||||
|
||||
mux.GET("/favicons/*favicon", serveFavicons())
|
||||
|
||||
mux.GET("/favicon.ico", serveFavicons())
|
||||
|
||||
mux.GET(MediaPrefix+"/*media", serveMedia(paths, regexes, index, formats))
|
||||
mux.GET(MediaPrefix+"/*media", serveMedia(paths, regexes, index, registeredFormats))
|
||||
|
||||
mux.GET(SourcePrefix+"/*static", serveStaticFile(paths, stats, index))
|
||||
|
||||
|
@ -402,10 +405,10 @@ func ServePage(args []string) error {
|
|||
}
|
||||
|
||||
if !skipIndex {
|
||||
index.generateCache(args, formats)
|
||||
index.generateCache(args, registeredFormats)
|
||||
}
|
||||
|
||||
mux.GET("/clear_cache", serveCacheClear(args, index, formats))
|
||||
mux.GET("/clear_cache", serveCacheClear(args, index, registeredFormats))
|
||||
}
|
||||
|
||||
if Index {
|
||||
|
@ -418,10 +421,6 @@ func ServePage(args []string) error {
|
|||
if PageLength != 0 {
|
||||
mux.GET("/json/:page", serveIndexJson(args, index))
|
||||
}
|
||||
|
||||
mux.GET("/extensions", serveExtensions(formats))
|
||||
|
||||
mux.GET("/mime_types", serveMimeTypes(formats))
|
||||
}
|
||||
|
||||
if Profile {
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
Copyright © 2023 Seednode <seednode@seedno.de>
|
||||
*/
|
||||
|
||||
package formats
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func RegisterAudioFormats() *SupportedFormat {
|
||||
return &SupportedFormat{
|
||||
Css: ``,
|
||||
Title: func(queryParams, fileUri, filePath, fileName, mime string) string {
|
||||
return fmt.Sprintf(`<title>%s</title>`, fileName)
|
||||
},
|
||||
Body: func(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: []string{
|
||||
`.mp3`,
|
||||
`.ogg`,
|
||||
`.oga`,
|
||||
`.wav`,
|
||||
},
|
||||
MimeTypes: []string{
|
||||
`audio/mpeg`,
|
||||
`audio/ogg`,
|
||||
`audio/wav`,
|
||||
},
|
||||
Validate: func(filePath string) bool {
|
||||
return true
|
||||
},
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
Copyright © 2023 Seednode <seednode@seedno.de>
|
||||
*/
|
||||
|
||||
package formats
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func RegisterFlashFormats() *SupportedFormat {
|
||||
return &SupportedFormat{
|
||||
Css: ``,
|
||||
Title: func(queryParams, fileUri, filePath, fileName, mime string) string {
|
||||
return fmt.Sprintf(`<title>%s</title>`, fileName)
|
||||
},
|
||||
Body: func(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: []string{
|
||||
`.swf`,
|
||||
},
|
||||
MimeTypes: []string{
|
||||
`application/x-shockwave-flash`,
|
||||
},
|
||||
Validate: func(filePath string) bool {
|
||||
return true
|
||||
},
|
||||
}
|
||||
}
|
|
@ -0,0 +1,106 @@
|
|||
/*
|
||||
Copyright © 2023 Seednode <seednode@seedno.de>
|
||||
*/
|
||||
|
||||
package formats
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
_ "image/gif"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"os"
|
||||
|
||||
_ "golang.org/x/image/bmp"
|
||||
_ "golang.org/x/image/webp"
|
||||
)
|
||||
|
||||
type Dimensions struct {
|
||||
Width int
|
||||
Height int
|
||||
}
|
||||
|
||||
func RegisterImageFormats() *SupportedFormat {
|
||||
return &SupportedFormat{
|
||||
Css: ``,
|
||||
Title: func(queryParams, fileUri, filePath, fileName, mime string) string {
|
||||
dimensions, err := ImageDimensions(filePath)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf(`<title>%s (%dx%d)</title>`,
|
||||
fileName,
|
||||
dimensions.Width,
|
||||
dimensions.Height)
|
||||
},
|
||||
Body: func(queryParams, fileUri, filePath, fileName, mime string) string {
|
||||
dimensions, err := ImageDimensions(filePath)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf(`<a href="/%s"><img src="%s" width="%d" height="%d" type="%s" alt="Roulette selected: %s"></a>`,
|
||||
queryParams,
|
||||
fileUri,
|
||||
dimensions.Width,
|
||||
dimensions.Height,
|
||||
mime,
|
||||
fileName)
|
||||
},
|
||||
Extensions: []string{
|
||||
`.apng`,
|
||||
`.avif`,
|
||||
`.bmp`,
|
||||
`.gif`,
|
||||
`.jpg`,
|
||||
`.jpeg`,
|
||||
`.jfif`,
|
||||
`.pjp`,
|
||||
`.pjpeg`,
|
||||
`.png`,
|
||||
`.svg`,
|
||||
`.webp`,
|
||||
},
|
||||
MimeTypes: []string{
|
||||
`image/apng`,
|
||||
`image/avif`,
|
||||
`image/bmp`,
|
||||
`image/gif`,
|
||||
`image/jpeg`,
|
||||
`image/png`,
|
||||
`image/svg+xml`,
|
||||
`image/webp`,
|
||||
},
|
||||
Validate: func(filePath string) bool {
|
||||
return true
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func ImageDimensions(path string) (*Dimensions, error) {
|
||||
file, err := os.Open(path)
|
||||
switch {
|
||||
case errors.Is(err, os.ErrNotExist):
|
||||
fmt.Printf("File %s does not exist\n", path)
|
||||
return &Dimensions{}, nil
|
||||
case err != nil:
|
||||
fmt.Printf("File %s open returned error: %s\n", path, err)
|
||||
return &Dimensions{}, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
decodedConfig, _, err := image.DecodeConfig(file)
|
||||
switch {
|
||||
case errors.Is(err, image.ErrFormat):
|
||||
fmt.Printf("File %s has invalid image format\n", path)
|
||||
return &Dimensions{Width: 0, Height: 0}, nil
|
||||
case err != nil:
|
||||
fmt.Printf("File %s decode returned error: %s\n", path, err)
|
||||
return &Dimensions{}, err
|
||||
}
|
||||
|
||||
return &Dimensions{Width: decodedConfig.Width, Height: decodedConfig.Height}, nil
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
Copyright © 2023 Seednode <seednode@seedno.de>
|
||||
*/
|
||||
|
||||
package formats
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func RegisterTextFormats() *SupportedFormat {
|
||||
return &SupportedFormat{
|
||||
Css: `pre{margin:.5rem;}`,
|
||||
Title: func(queryParams, fileUri, filePath, fileName, mime string) string {
|
||||
return fmt.Sprintf(`<title>%s</title>`, fileName)
|
||||
},
|
||||
Body: func(queryParams, fileUri, filePath, fileName, mime string) string {
|
||||
body, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
body = []byte{}
|
||||
}
|
||||
|
||||
return fmt.Sprintf(`<a href="/%s"><pre>%s</pre></a>`,
|
||||
queryParams,
|
||||
body)
|
||||
},
|
||||
Extensions: []string{
|
||||
`.css`,
|
||||
`.csv`,
|
||||
`.html`,
|
||||
`.js`,
|
||||
`.json`,
|
||||
`.md`,
|
||||
`.txt`,
|
||||
`.xml`,
|
||||
},
|
||||
MimeTypes: []string{
|
||||
`application/json`,
|
||||
`application/xml`,
|
||||
`text/css`,
|
||||
`text/csv`,
|
||||
`text/javascript`,
|
||||
`text/plain`,
|
||||
`text/plain; charset=utf-8`,
|
||||
},
|
||||
Validate: func(path string) bool {
|
||||
file, err := os.Open(path)
|
||||
switch {
|
||||
case errors.Is(err, os.ErrNotExist):
|
||||
return false
|
||||
case err != nil:
|
||||
return false
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
head := make([]byte, 512)
|
||||
file.Read(head)
|
||||
|
||||
return utf8.Valid(head)
|
||||
},
|
||||
}
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
Copyright © 2023 Seednode <seednode@seedno.de>
|
||||
*/
|
||||
|
||||
package formats
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type SupportedFormat struct {
|
||||
Css string
|
||||
Title func(queryParams, fileUri, filePath, fileName, mime string) string
|
||||
Body func(queryParams, fileUri, filePath, fileName, mime string) string
|
||||
Extensions []string
|
||||
MimeTypes []string
|
||||
Validate func(filePath string) bool
|
||||
}
|
||||
|
||||
type SupportedFormats struct {
|
||||
Extensions map[string]*SupportedFormat
|
||||
MimeTypes map[string]*SupportedFormat
|
||||
}
|
||||
|
||||
func (s *SupportedFormats) Add(t *SupportedFormat) {
|
||||
for _, v := range t.Extensions {
|
||||
_, exists := s.Extensions[v]
|
||||
if !exists {
|
||||
s.Extensions[v] = t
|
||||
}
|
||||
}
|
||||
|
||||
for _, v := range t.MimeTypes {
|
||||
_, exists := s.Extensions[v]
|
||||
if !exists {
|
||||
s.MimeTypes[v] = t
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func FileType(path string, registeredFormats *SupportedFormats) (bool, *SupportedFormat, string, error) {
|
||||
file, err := os.Open(path)
|
||||
switch {
|
||||
case errors.Is(err, os.ErrNotExist):
|
||||
return false, nil, "", nil
|
||||
case err != nil:
|
||||
return false, nil, "", err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
head := make([]byte, 512)
|
||||
file.Read(head)
|
||||
|
||||
mimeType := http.DetectContentType(head)
|
||||
|
||||
// try identifying files by mime types first
|
||||
fileType, exists := registeredFormats.MimeTypes[mimeType]
|
||||
if exists {
|
||||
return fileType.Validate(path), fileType, mimeType, nil
|
||||
}
|
||||
|
||||
// if mime type detection fails, use the file extension
|
||||
fileType, exists = registeredFormats.Extensions[filepath.Ext(path)]
|
||||
if exists {
|
||||
return fileType.Validate(path), fileType, mimeType, nil
|
||||
}
|
||||
|
||||
return false, nil, "", nil
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
Copyright © 2023 Seednode <seednode@seedno.de>
|
||||
*/
|
||||
|
||||
package formats
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func RegisterVideoFormats() *SupportedFormat {
|
||||
return &SupportedFormat{
|
||||
Css: ``,
|
||||
Title: func(queryParams, fileUri, filePath, fileName, mime string) string {
|
||||
return fmt.Sprintf(`<title>%s</title>`, fileName)
|
||||
},
|
||||
Body: func(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: []string{
|
||||
`.mp4`,
|
||||
`.ogm`,
|
||||
`.ogv`,
|
||||
`.webm`,
|
||||
},
|
||||
MimeTypes: []string{
|
||||
`video/mp4`,
|
||||
`video/ogg`,
|
||||
`video/webm`,
|
||||
},
|
||||
Validate: func(filePath string) bool {
|
||||
return true
|
||||
},
|
||||
}
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
/*
|
||||
Copyright © 2023 Seednode <seednode@seedno.de>
|
||||
*/
|
||||
|
||||
package types
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
func (t Audio) Title(queryParams, fileUri, filePath, fileName, mime string) string {
|
||||
return fmt.Sprintf(`<title>%s</title>`, fileName)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func (t Audio) Extensions() map[string]string {
|
||||
return map[string]string{
|
||||
`.mp3`: `audio/mpeg`,
|
||||
`.ogg`: `audio/ogg`,
|
||||
`.oga`: `audio/ogg`,
|
||||
}
|
||||
}
|
||||
|
||||
func (t Audio) MimeTypes() []string {
|
||||
return []string{
|
||||
`application/ogg`,
|
||||
`audio/mp3`,
|
||||
`audio/mpeg`,
|
||||
`audio/mpeg3`,
|
||||
`audio/ogg`,
|
||||
`audio/x-mpeg-3`,
|
||||
}
|
||||
}
|
||||
|
||||
func (t Audio) Validate(filePath string) bool {
|
||||
return true
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
/*
|
||||
Copyright © 2023 Seednode <seednode@seedno.de>
|
||||
*/
|
||||
|
||||
package types
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
func (t Flash) Title(queryParams, fileUri, filePath, fileName, mime string) string {
|
||||
return fmt.Sprintf(`<title>%s</title>`, fileName)
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
func (t Flash) Extensions() map[string]string {
|
||||
return map[string]string{
|
||||
`.swf`: `application/x-shockwave-flash`,
|
||||
}
|
||||
}
|
||||
|
||||
func (t Flash) MimeTypes() []string {
|
||||
return []string{
|
||||
`application/x-shockwave-flash`,
|
||||
}
|
||||
}
|
||||
|
||||
func (t Flash) Validate(filePath string) bool {
|
||||
return true
|
||||
}
|
123
types/images.go
123
types/images.go
|
@ -1,123 +0,0 @@
|
|||
/*
|
||||
Copyright © 2023 Seednode <seednode@seedno.de>
|
||||
*/
|
||||
|
||||
package types
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
_ "image/gif"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
_ "golang.org/x/image/bmp"
|
||||
_ "golang.org/x/image/webp"
|
||||
)
|
||||
|
||||
type Dimensions struct {
|
||||
Width int
|
||||
Height int
|
||||
}
|
||||
|
||||
type Images struct{}
|
||||
|
||||
func (t Images) 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;}`)
|
||||
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()
|
||||
}
|
||||
|
||||
func (t Images) Title(queryParams, fileUri, filePath, fileName, mime string) string {
|
||||
dimensions, err := ImageDimensions(filePath)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf(`<title>%s (%dx%d)</title>`,
|
||||
fileName,
|
||||
dimensions.Width,
|
||||
dimensions.Height)
|
||||
}
|
||||
|
||||
func (t Images) Body(queryParams, fileUri, filePath, fileName, mime string) string {
|
||||
dimensions, err := ImageDimensions(filePath)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf(`<a href="/%s"><img src="%s" width="%d" height="%d" type="%s" alt="Roulette selected: %s"></a>`,
|
||||
queryParams,
|
||||
fileUri,
|
||||
dimensions.Width,
|
||||
dimensions.Height,
|
||||
mime,
|
||||
fileName)
|
||||
}
|
||||
|
||||
func (t Images) Extensions() map[string]string {
|
||||
return map[string]string{
|
||||
`.apng`: `image/apng`,
|
||||
`.avif`: `image/avif`,
|
||||
`.bmp`: `image/bmp`,
|
||||
`.gif`: `image/gif`,
|
||||
`.jpg`: `image/jpeg`,
|
||||
`.jpeg`: `image/jpeg`,
|
||||
`.jfif`: `image/jpeg`,
|
||||
`.pjp`: `image/jpeg`,
|
||||
`.pjpeg`: `image/jpeg`,
|
||||
`.png`: `image/png`,
|
||||
`.svg`: `image/svg+xml`,
|
||||
`.webp`: `image/webp`,
|
||||
}
|
||||
}
|
||||
|
||||
func (t Images) MimeTypes() []string {
|
||||
return []string{
|
||||
`image/apng`,
|
||||
`image/avif`,
|
||||
`image/bmp`,
|
||||
`image/gif`,
|
||||
`image/jpeg`,
|
||||
`image/png`,
|
||||
`image/svg+xml`,
|
||||
`image/webp`,
|
||||
}
|
||||
}
|
||||
|
||||
func (t Images) Validate(filePath string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func ImageDimensions(path string) (*Dimensions, error) {
|
||||
file, err := os.Open(path)
|
||||
switch {
|
||||
case errors.Is(err, os.ErrNotExist):
|
||||
fmt.Printf("File %s does not exist\n", path)
|
||||
return &Dimensions{}, nil
|
||||
case err != nil:
|
||||
fmt.Printf("File %s open returned error: %s\n", path, err)
|
||||
return &Dimensions{}, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
decodedConfig, _, err := image.DecodeConfig(file)
|
||||
switch {
|
||||
case errors.Is(err, image.ErrFormat):
|
||||
fmt.Printf("File %s has invalid image format\n", path)
|
||||
return &Dimensions{Width: 0, Height: 0}, nil
|
||||
case err != nil:
|
||||
fmt.Printf("File %s decode returned error: %s\n", path, err)
|
||||
return &Dimensions{}, err
|
||||
}
|
||||
|
||||
return &Dimensions{Width: decodedConfig.Width, Height: decodedConfig.Height}, nil
|
||||
}
|
|
@ -1,84 +0,0 @@
|
|||
/*
|
||||
Copyright © 2023 Seednode <seednode@seedno.de>
|
||||
*/
|
||||
|
||||
package types
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
type Text struct{}
|
||||
|
||||
func (t Text) 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;overflow:hidden;}`)
|
||||
css.WriteString(`textarea{border:none;caret-color:transparent;outline:none;margin:.5rem;`)
|
||||
css.WriteString(`height:99%;width:99%;white-space:pre;overflow:auto;}`)
|
||||
|
||||
return css.String()
|
||||
}
|
||||
|
||||
func (t Text) Title(queryParams, fileUri, filePath, fileName, mime string) string {
|
||||
return fmt.Sprintf(`<title>%s</title>`, fileName)
|
||||
}
|
||||
|
||||
func (t Text) Body(queryParams, fileUri, filePath, fileName, mime string) string {
|
||||
body, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
body = []byte{}
|
||||
}
|
||||
|
||||
return fmt.Sprintf(`<a href="/%s"><textarea autofocus readonly>%s</textarea></a>`,
|
||||
queryParams,
|
||||
body)
|
||||
}
|
||||
|
||||
func (t Text) Extensions() map[string]string {
|
||||
return map[string]string{
|
||||
`.css`: `text/css`,
|
||||
`.csv`: `text/csv`,
|
||||
`.htm`: `text/html`,
|
||||
`.html`: `text/html`,
|
||||
`.js`: `text/javascript`,
|
||||
`.json`: `application/json`,
|
||||
`.md`: `text/markdown`,
|
||||
`.txt`: `text/plain`,
|
||||
`.xml`: `application/xml`,
|
||||
}
|
||||
}
|
||||
|
||||
func (t Text) MimeTypes() []string {
|
||||
return []string{
|
||||
`application/json`,
|
||||
`application/xml`,
|
||||
`text/css`,
|
||||
`text/csv`,
|
||||
`text/html`,
|
||||
`text/javascript`,
|
||||
`text/plain`,
|
||||
`text/plain; charset=utf-8`,
|
||||
}
|
||||
}
|
||||
|
||||
func (t Text) Validate(filePath string) bool {
|
||||
file, err := os.Open(filePath)
|
||||
switch {
|
||||
case errors.Is(err, os.ErrNotExist):
|
||||
return false
|
||||
case err != nil:
|
||||
return false
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
head := make([]byte, 512)
|
||||
file.Read(head)
|
||||
|
||||
return utf8.Valid(head)
|
||||
}
|
|
@ -1,76 +0,0 @@
|
|||
/*
|
||||
Copyright © 2023 Seednode <seednode@seedno.de>
|
||||
*/
|
||||
|
||||
package types
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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() {
|
||||
_, exists := s.Extensions[v]
|
||||
if !exists {
|
||||
s.MimeTypes[v] = t
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func FileType(path string, registeredFormats *Types) (bool, Type, string, error) {
|
||||
file, err := os.Open(path)
|
||||
switch {
|
||||
case errors.Is(err, os.ErrNotExist):
|
||||
return false, nil, "", nil
|
||||
case err != nil:
|
||||
return false, nil, "", err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
head := make([]byte, 512)
|
||||
file.Read(head)
|
||||
|
||||
mimeType := http.DetectContentType(head)
|
||||
|
||||
// try identifying files by mime types first
|
||||
fileType, exists := registeredFormats.MimeTypes[mimeType]
|
||||
if exists {
|
||||
return fileType.Validate(path), fileType, mimeType, nil
|
||||
}
|
||||
|
||||
// if mime type detection fails, use the file extension
|
||||
mimeType, exists = registeredFormats.Extensions[filepath.Ext(path)]
|
||||
if exists {
|
||||
fileType, exists := registeredFormats.MimeTypes[mimeType]
|
||||
|
||||
if exists {
|
||||
return fileType.Validate(path), fileType, mimeType, nil
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil, "", nil
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
/*
|
||||
Copyright © 2023 Seednode <seednode@seedno.de>
|
||||
*/
|
||||
|
||||
package types
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Video struct{}
|
||||
|
||||
func (t Video) 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;}`)
|
||||
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()
|
||||
}
|
||||
|
||||
func (t Video) Title(queryParams, fileUri, filePath, fileName, mime string) string {
|
||||
return fmt.Sprintf(`<title>%s</title>`, fileName)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func (t Video) Extensions() map[string]string {
|
||||
return map[string]string{
|
||||
`.mp4`: `video/mp4`,
|
||||
`.ogm`: `video/ogg`,
|
||||
`.ogv`: `video/ogg`,
|
||||
`.webm`: `video/webm`,
|
||||
}
|
||||
}
|
||||
|
||||
func (t Video) MimeTypes() []string {
|
||||
return []string{
|
||||
`video/mp4`,
|
||||
`video/ogg`,
|
||||
`video/webm`,
|
||||
}
|
||||
}
|
||||
|
||||
func (t Video) Validate(filePath string) bool {
|
||||
return true
|
||||
}
|
Loading…
Reference in New Issue