Changed all variables to non-exported by default

This commit is contained in:
Seednode 2023-09-13 09:26:15 -05:00
parent 3ea2227a9f
commit 64639a87a3
16 changed files with 255 additions and 247 deletions

View File

@ -15,24 +15,24 @@ import (
"seedno.de/seednode/roulette/types" "seedno.de/seednode/roulette/types"
) )
type FileIndex struct { type fileCache struct {
mutex sync.RWMutex mutex sync.RWMutex
list []string list []string
} }
func (i *FileIndex) Index() []string { func (cache *fileCache) List() []string {
i.mutex.RLock() cache.mutex.RLock()
val := i.list val := cache.list
i.mutex.RUnlock() cache.mutex.RUnlock()
return val return val
} }
func (i *FileIndex) Remove(path string) { func (cache *fileCache) remove(path string) {
i.mutex.RLock() cache.mutex.RLock()
tempIndex := make([]string, len(i.list)) tempIndex := make([]string, len(cache.list))
copy(tempIndex, i.list) copy(tempIndex, cache.list)
i.mutex.RUnlock() cache.mutex.RUnlock()
var position int var position int
@ -46,39 +46,39 @@ func (i *FileIndex) Remove(path string) {
tempIndex[position] = tempIndex[len(tempIndex)-1] tempIndex[position] = tempIndex[len(tempIndex)-1]
i.mutex.Lock() cache.mutex.Lock()
i.list = make([]string, len(tempIndex)-1) cache.list = make([]string, len(tempIndex)-1)
copy(i.list, tempIndex[:len(tempIndex)-1]) copy(cache.list, tempIndex[:len(tempIndex)-1])
i.mutex.Unlock() cache.mutex.Unlock()
} }
func (i *FileIndex) setIndex(val []string) { func (cache *fileCache) set(val []string) {
i.mutex.Lock() cache.mutex.Lock()
i.list = val cache.list = val
i.mutex.Unlock() cache.mutex.Unlock()
} }
func (i *FileIndex) generateCache(args []string, formats *types.Types) { func (cache *fileCache) generate(args []string, formats *types.Types) {
i.mutex.Lock() cache.mutex.Lock()
i.list = []string{} cache.list = []string{}
i.mutex.Unlock() cache.mutex.Unlock()
fileList(args, &Filters{}, "", i, formats) fileList(args, &filters{}, "", cache, formats)
if Cache && CacheFile != "" { if Cache && CacheFile != "" {
i.Export(CacheFile) cache.Export(CacheFile)
} }
} }
func (i *FileIndex) IsEmpty() bool { func (cache *fileCache) isEmpty() bool {
i.mutex.RLock() cache.mutex.RLock()
length := len(i.list) length := len(cache.list)
i.mutex.RUnlock() cache.mutex.RUnlock()
return length == 0 return length == 0
} }
func (i *FileIndex) Export(path string) error { func (cache *fileCache) Export(path string) error {
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil { if err != nil {
return err return err
@ -93,16 +93,14 @@ func (i *FileIndex) Export(path string) error {
enc := gob.NewEncoder(z) enc := gob.NewEncoder(z)
i.mutex.RLock() cache.mutex.RLock()
enc.Encode(&cache.list)
enc.Encode(&i.list) cache.mutex.RUnlock()
i.mutex.RUnlock()
return nil return nil
} }
func (i *FileIndex) Import(path string) error { func (cache *fileCache) Import(path string) error {
file, err := os.OpenFile(path, os.O_RDONLY, 0600) file, err := os.OpenFile(path, os.O_RDONLY, 0600)
if err != nil { if err != nil {
return err return err
@ -117,11 +115,11 @@ func (i *FileIndex) Import(path string) error {
dec := gob.NewDecoder(z) dec := gob.NewDecoder(z)
i.mutex.Lock() cache.mutex.Lock()
err = dec.Decode(&i.list) err = dec.Decode(&cache.list)
i.mutex.Unlock() cache.mutex.Unlock()
if err != nil { if err != nil {
return err return err
@ -130,9 +128,9 @@ func (i *FileIndex) Import(path string) error {
return nil return nil
} }
func serveCacheClear(args []string, index *FileIndex, formats *types.Types) httprouter.Handle { func serveCacheClear(args []string, cache *fileCache, 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, formats) cache.generate(args, formats)
w.Header().Set("Content-Type", "text/plain") w.Header().Set("Content-Type", "text/plain")

View File

@ -24,7 +24,7 @@ func newErrorPage(title, body string) string {
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>a{display:block;height:100%;width:100%;text-decoration:none;color:inherit;cursor:auto;}</style>`) htmlBody.WriteString(`<style>a{display:block;height:100%;width:100%;text-decoration:none;color:inherit;cursor:auto;}</style>`)
htmlBody.WriteString(fmt.Sprintf("<title>%s</title></head>", title)) htmlBody.WriteString(fmt.Sprintf("<title>%s</title></head>", title))
htmlBody.WriteString(fmt.Sprintf("<body><a href=\"/\">%s</a></body></html>", body)) htmlBody.WriteString(fmt.Sprintf("<body><a href=\"/\">%s</a></body></html>", body))
@ -32,13 +32,13 @@ func newErrorPage(title, body string) string {
return htmlBody.String() return htmlBody.String()
} }
func notFound(w http.ResponseWriter, r *http.Request, filePath string) error { func notFound(w http.ResponseWriter, r *http.Request, path string) error {
startTime := time.Now() startTime := time.Now()
if Verbose { if Verbose {
fmt.Printf("%s | Unavailable file %s requested by %s\n", fmt.Printf("%s | Unavailable file %s requested by %s\n",
startTime.Format(LogDate), startTime.Format(logDate),
filePath, path,
r.RemoteAddr, r.RemoteAddr,
) )
} }
@ -59,7 +59,7 @@ func serverError(w http.ResponseWriter, r *http.Request, i interface{}) {
if Verbose { if Verbose {
fmt.Printf("%s | Invalid request for %s from %s\n", fmt.Printf("%s | Invalid request for %s from %s\n",
startTime.Format(LogDate), startTime.Format(logDate),
r.URL.Path, r.URL.Path,
r.RemoteAddr, r.RemoteAddr,
) )

View File

@ -18,7 +18,7 @@ import (
var favicons embed.FS var favicons embed.FS
const ( const (
FaviconHtml string = `<link rel="apple-touch-icon" sizes="180x180" href="/favicons/apple-touch-icon.png"> faviconHtml string = `<link rel="apple-touch-icon" sizes="180x180" href="/favicons/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicons/favicon-32x32.png"> <link rel="icon" type="image/png" sizes="32x32" href="/favicons/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicons/favicon-16x16.png"> <link rel="icon" type="image/png" sizes="16x16" href="/favicons/favicon-16x16.png">
<link rel="manifest" href="/favicons/site.webmanifest"> <link rel="manifest" href="/favicons/site.webmanifest">

View File

@ -31,51 +31,51 @@ const (
maxFileScans maxConcurrency = 256 maxFileScans maxConcurrency = 256
) )
type Regexes struct { type regexes struct {
alphanumeric *regexp.Regexp alphanumeric *regexp.Regexp
filename *regexp.Regexp filename *regexp.Regexp
} }
type Concurrency struct { type concurrency struct {
directoryScans chan int directoryScans chan int
fileScans chan int fileScans chan int
} }
type Files struct { type files struct {
mutex sync.RWMutex mutex sync.RWMutex
list map[string][]string list map[string][]string
} }
func (f *Files) Append(directory, path string) { func (f *files) append(directory, path string) {
f.mutex.Lock() f.mutex.Lock()
f.list[directory] = append(f.list[directory], path) f.list[directory] = append(f.list[directory], path)
f.mutex.Unlock() f.mutex.Unlock()
} }
type ScanStats struct { type scanStats struct {
filesMatched atomic.Uint32 filesMatched atomic.Uint32
filesSkipped atomic.Uint32 filesSkipped atomic.Uint32
directoriesMatched atomic.Uint32 directoriesMatched atomic.Uint32
directoriesSkipped atomic.Uint32 directoriesSkipped atomic.Uint32
} }
type Path struct { type splitPath struct {
base string base string
number int number int
extension string extension string
} }
func (p *Path) increment() { func (splitPath *splitPath) increment() {
p.number = p.number + 1 splitPath.number = splitPath.number + 1
} }
func (p *Path) decrement() { func (splitPath *splitPath) decrement() {
p.number = p.number - 1 splitPath.number = splitPath.number - 1
} }
func contains(s []string, e string) bool { func contains(slice []string, value string) bool {
for _, a := range s { for _, v := range slice {
if a == e { if v == value {
return true return true
} }
} }
@ -102,15 +102,15 @@ func humanReadableSize(bytes int) string {
func preparePath(path string) string { func preparePath(path string) string {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
return fmt.Sprintf("%s/%s", MediaPrefix, filepath.ToSlash(path)) return fmt.Sprintf("%s/%s", mediaPrefix, filepath.ToSlash(path))
} }
return MediaPrefix + path 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, formats *types.Types, shouldCache bool) error {
if shouldCache { if shouldCache {
registered, _, _, err := types.FileType(path, formats) registered, _, _, err := formats.FileType(path)
if err != nil { if err != nil {
return err return err
} }
@ -120,15 +120,15 @@ func appendPath(directory, path string, files *Files, stats *ScanStats, formats
} }
} }
files.Append(directory, path) files.append(directory, path)
stats.filesMatched.Add(1) stats.filesMatched.Add(1)
return nil 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, formats *types.Types) error {
shouldCache := Cache && filters.IsEmpty() shouldCache := Cache && filters.isEmpty()
absolutePath, err := filepath.Abs(path) absolutePath, err := filepath.Abs(path)
if err != nil { if err != nil {
@ -139,11 +139,11 @@ func appendPaths(path string, files *Files, filters *Filters, stats *ScanStats,
filename = strings.ToLower(filename) filename = strings.ToLower(filename)
if filters.HasExcludes() { if filters.hasExcludes() {
for i := 0; i < len(filters.excludes); i++ { for i := 0; i < len(filters.excluded); i++ {
if strings.Contains( if strings.Contains(
filename, filename,
filters.excludes[i], filters.excluded[i],
) { ) {
stats.filesSkipped.Add(1) stats.filesSkipped.Add(1)
@ -152,11 +152,11 @@ func appendPaths(path string, files *Files, filters *Filters, stats *ScanStats,
} }
} }
if filters.HasIncludes() { if filters.hasIncludes() {
for i := 0; i < len(filters.includes); i++ { for i := 0; i < len(filters.included); i++ {
if strings.Contains( if strings.Contains(
filename, filename,
filters.includes[i], filters.included[i],
) { ) {
err := appendPath(directory, path, files, stats, formats, shouldCache) err := appendPath(directory, path, files, stats, formats, shouldCache)
if err != nil { if err != nil {
@ -180,38 +180,38 @@ 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, formats *types.Types) (string, error) { func newFile(paths []string, filters *filters, sortOrder string, regexes *regexes, index *fileCache, formats *types.Types) (string, error) {
filePath, err := pickFile(paths, filters, sortOrder, index, formats) path, err := pickFile(paths, filters, sortOrder, index, formats)
if err != nil { if err != nil {
return "", nil return "", nil
} }
path, err := splitPath(filePath, Regexes) splitPath, err := split(path, regexes)
if err != nil { if err != nil {
return "", err return "", err
} }
path.number = 1 splitPath.number = 1
switch { switch {
case sortOrder == "asc": case sortOrder == "asc":
filePath, err = tryExtensions(path, formats) path, err = tryExtensions(splitPath, formats)
if err != nil { if err != nil {
return "", err return "", err
} }
case sortOrder == "desc": case sortOrder == "desc":
for { for {
path.increment() splitPath.increment()
filePath, err = tryExtensions(path, formats) path, err = tryExtensions(splitPath, formats)
if err != nil { if err != nil {
return "", err return "", err
} }
if filePath == "" { if path == "" {
path.decrement() splitPath.decrement()
filePath, err = tryExtensions(path, formats) path, err = tryExtensions(splitPath, formats)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -221,25 +221,25 @@ func newFile(paths []string, filters *Filters, sortOrder string, Regexes *Regexe
} }
} }
return filePath, nil return path, nil
} }
func nextFile(filePath, sortOrder string, Regexes *Regexes, formats *types.Types) (string, error) { func nextFile(path, sortOrder string, regexes *regexes, formats *types.Types) (string, error) {
path, err := splitPath(filePath, Regexes) splitPath, err := split(path, regexes)
if err != nil { if err != nil {
return "", err return "", err
} }
switch { switch {
case sortOrder == "asc": case sortOrder == "asc":
path.increment() splitPath.increment()
case sortOrder == "desc": case sortOrder == "desc":
path.decrement() splitPath.decrement()
default: default:
return "", nil return "", nil
} }
fileName, err := tryExtensions(path, formats) fileName, err := tryExtensions(splitPath, formats)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -247,21 +247,21 @@ func nextFile(filePath, sortOrder string, Regexes *Regexes, formats *types.Types
return fileName, err return fileName, err
} }
func splitPath(path string, Regexes *Regexes) (*Path, error) { func split(path string, regexes *regexes) (*splitPath, error) {
p := Path{} p := splitPath{}
var err error var err error
split := Regexes.filename.FindAllStringSubmatch(path, -1) split := regexes.filename.FindAllStringSubmatch(path, -1)
if len(split) < 1 || len(split[0]) < 3 { if len(split) < 1 || len(split[0]) < 3 {
return &Path{}, nil return &splitPath{}, nil
} }
p.base = split[0][1] p.base = split[0][1]
p.number, err = strconv.Atoi(split[0][2]) p.number, err = strconv.Atoi(split[0][2])
if err != nil { if err != nil {
return &Path{}, err return &splitPath{}, err
} }
p.extension = split[0][3] p.extension = split[0][3]
@ -269,11 +269,11 @@ func splitPath(path string, Regexes *Regexes) (*Path, error) {
return &p, nil return &p, nil
} }
func tryExtensions(p *Path, formats *types.Types) (string, error) { func tryExtensions(splitPath *splitPath, formats *types.Types) (string, error) {
var fileName string var fileName string
for extension := range formats.Extensions { for extension := range formats.Extensions {
fileName = fmt.Sprintf("%s%.3d%s", p.base, p.number, extension) fileName = fmt.Sprintf("%s%.3d%s", splitPath.base, splitPath.number, extension)
exists, err := fileExists(fileName) exists, err := fileExists(fileName)
if err != nil { if err != nil {
@ -300,11 +300,11 @@ func fileExists(path string) (bool, error) {
} }
} }
func pathIsValid(filePath string, paths []string) bool { func pathIsValid(path string, paths []string) bool {
var matchesPrefix = false var matchesPrefix = false
for i := 0; i < len(paths); i++ { for i := 0; i < len(paths); i++ {
if strings.HasPrefix(filePath, paths[i]) { if strings.HasPrefix(path, paths[i]) {
matchesPrefix = true matchesPrefix = true
} }
} }
@ -312,8 +312,8 @@ func pathIsValid(filePath string, paths []string) bool {
switch { switch {
case Verbose && !matchesPrefix: case Verbose && !matchesPrefix:
fmt.Printf("%s | Error: Failed to serve file outside specified path(s): %s\n", fmt.Printf("%s | Error: Failed to serve file outside specified path(s): %s\n",
time.Now().Format(LogDate), time.Now().Format(logDate),
filePath, path,
) )
return false return false
@ -336,7 +336,7 @@ func pathHasSupportedFiles(path string, formats *types.Types) (bool, error) {
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 := types.FileType(p, formats) registered, _, _, err := formats.FileType(p)
if err != nil { if err != nil {
return err return err
} }
@ -381,7 +381,7 @@ func pathCount(path string) (uint32, uint32, error) {
return files, directories, nil 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, 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 {
@ -442,26 +442,26 @@ 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, formats *types.Types) ([]string, bool) { func fileList(paths []string, filters *filters, sort string, cache *fileCache, formats *types.Types) ([]string, bool) {
if Cache && filters.IsEmpty() && !index.IsEmpty() { if Cache && filters.isEmpty() && !cache.isEmpty() {
return index.Index(), true return cache.List(), true
} }
var fileList []string var fileList []string
files := &Files{ files := &files{
mutex: sync.RWMutex{}, mutex: sync.RWMutex{},
list: make(map[string][]string), list: make(map[string][]string),
} }
stats := &ScanStats{ stats := &scanStats{
filesMatched: atomic.Uint32{}, filesMatched: atomic.Uint32{},
filesSkipped: atomic.Uint32{}, filesSkipped: atomic.Uint32{},
directoriesMatched: atomic.Uint32{}, directoriesMatched: atomic.Uint32{},
directoriesSkipped: atomic.Uint32{}, directoriesSkipped: atomic.Uint32{},
} }
concurrency := &Concurrency{ concurrency := &concurrency{
directoryScans: make(chan int, maxDirectoryScans), directoryScans: make(chan int, maxDirectoryScans),
fileScans: make(chan int, maxFileScans), fileScans: make(chan int, maxFileScans),
} }
@ -498,7 +498,7 @@ func fileList(paths []string, filters *Filters, sort string, index *FileIndex, f
if Verbose { if Verbose {
fmt.Printf("%s | Indexed %d/%d files across %d/%d directories in %s\n", fmt.Printf("%s | Indexed %d/%d files across %d/%d directories in %s\n",
time.Now().Format(LogDate), time.Now().Format(logDate),
stats.filesMatched.Load(), stats.filesMatched.Load(),
stats.filesMatched.Load()+stats.filesSkipped.Load(), stats.filesMatched.Load()+stats.filesSkipped.Load(),
stats.directoriesMatched.Load(), stats.directoriesMatched.Load(),
@ -507,8 +507,8 @@ func fileList(paths []string, filters *Filters, sort string, index *FileIndex, f
) )
} }
if Cache && filters.IsEmpty() { if Cache && filters.isEmpty() {
index.setIndex(fileList) cache.set(fileList)
} }
return fileList, false return fileList, false
@ -532,7 +532,7 @@ func prepareDirectory(directory []string) []string {
} }
} }
func prepareDirectories(files *Files, sort string) []string { func prepareDirectories(files *files, sort string) []string {
directories := []string{} directories := []string{}
keys := make([]string, len(files.list)) keys := make([]string, len(files.list))
@ -556,8 +556,8 @@ func prepareDirectories(files *Files, sort string) []string {
return directories return directories
} }
func pickFile(args []string, filters *Filters, sort string, index *FileIndex, formats *types.Types) (string, error) { func pickFile(args []string, filters *filters, sort string, cache *fileCache, formats *types.Types) (string, error) {
fileList, fromCache := fileList(args, filters, sort, index, formats) fileList, fromCache := fileList(args, filters, sort, cache, formats)
fileCount := len(fileList) fileCount := len(fileList)
if fileCount < 1 { if fileCount < 1 {
@ -581,22 +581,22 @@ func pickFile(args []string, filters *Filters, sort string, index *FileIndex, fo
val++ val++
} }
filePath := fileList[val] path := fileList[val]
if !fromCache { if !fromCache {
registered, _, _, err := types.FileType(filePath, formats) registered, _, _, err := formats.FileType(path)
if err != nil { if err != nil {
return "", err return "", err
} }
if registered { if registered {
return filePath, nil return path, nil
} }
continue continue
} }
return filePath, nil return path, nil
} }
return "", ErrNoMediaFound return "", ErrNoMediaFound

View File

@ -6,27 +6,27 @@ package cmd
import "strings" import "strings"
type Filters struct { type filters struct {
includes []string included []string
excludes []string excluded []string
} }
func (f *Filters) IsEmpty() bool { func (filters *filters) isEmpty() bool {
return !(f.HasIncludes() || f.HasExcludes()) return !(filters.hasIncludes() || filters.hasExcludes())
} }
func (f *Filters) HasIncludes() bool { func (filters *filters) hasIncludes() bool {
return len(f.includes) != 0 return len(filters.included) != 0
} }
func (f *Filters) Includes() string { func (filters *filters) includes() string {
return strings.Join(f.includes, ",") return strings.Join(filters.included, ",")
} }
func (f *Filters) HasExcludes() bool { func (filters *filters) hasExcludes() bool {
return len(f.excludes) != 0 return len(filters.excluded) != 0
} }
func (f *Filters) Excludes() string { func (filters *filters) excludes() string {
return strings.Join(f.excludes, ",") return strings.Join(filters.excluded, ",")
} }

View File

@ -19,13 +19,13 @@ import (
"seedno.de/seednode/roulette/types" "seedno.de/seednode/roulette/types"
) )
func serveIndexHtml(args []string, index *FileIndex, paginate bool) httprouter.Handle { func serveIndexHtml(args []string, cache *fileCache, paginate bool) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Header().Set("Content-Type", "text/html") w.Header().Set("Content-Type", "text/html")
startTime := time.Now() startTime := time.Now()
indexDump := index.Index() indexDump := cache.List()
fileCount := len(indexDump) fileCount := len(indexDump)
@ -54,7 +54,7 @@ func serveIndexHtml(args []string, index *FileIndex, paginate bool) httprouter.H
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>a{text-decoration:none;height:100%;width:100%;color:inherit;cursor:pointer}`) htmlBody.WriteString(`<style>a{text-decoration:none;height:100%;width:100%;color:inherit;cursor:pointer}`)
htmlBody.WriteString(`table,td,tr{border:1px solid black;border-collapse:collapse}td{white-space:nowrap;padding:.5em}</style>`) htmlBody.WriteString(`table,td,tr{border:1px solid black;border-collapse:collapse}td{white-space:nowrap;padding:.5em}</style>`)
htmlBody.WriteString(fmt.Sprintf("<title>Index contains %d files</title></head><body><table>", fileCount)) htmlBody.WriteString(fmt.Sprintf("<title>Index contains %d files</title></head><body><table>", fileCount))
@ -65,7 +65,7 @@ func serveIndexHtml(args []string, index *FileIndex, paginate bool) httprouter.H
if Sorting { if Sorting {
shouldSort = "?sort=asc" shouldSort = "?sort=asc"
} }
htmlBody.WriteString(fmt.Sprintf("<tr><td><a href=\"%s%s%s\">%s</a></td></tr>\n", MediaPrefix, v, shouldSort, v)) htmlBody.WriteString(fmt.Sprintf("<tr><td><a href=\"%s%s%s\">%s</a></td></tr>\n", mediaPrefix, v, shouldSort, v))
} }
} }
if PageLength != 0 { if PageLength != 0 {
@ -124,7 +124,7 @@ func serveIndexHtml(args []string, index *FileIndex, paginate bool) httprouter.H
if Verbose { if Verbose {
fmt.Printf("%s | Served HTML index page (%s) to %s in %s\n", fmt.Printf("%s | Served HTML index page (%s) to %s in %s\n",
startTime.Format(LogDate), startTime.Format(logDate),
humanReadableSize(b), humanReadableSize(b),
realIP(r), realIP(r),
time.Since(startTime).Round(time.Microsecond), time.Since(startTime).Round(time.Microsecond),
@ -133,18 +133,18 @@ func serveIndexHtml(args []string, index *FileIndex, paginate bool) httprouter.H
} }
} }
func serveIndexJson(args []string, index *FileIndex) httprouter.Handle { func serveIndexJson(args []string, index *fileCache) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
startTime := time.Now() startTime := time.Now()
indexDump := index.Index() cachedFiles := index.List()
fileCount := len(indexDump) fileCount := len(cachedFiles)
sort.SliceStable(indexDump, func(p, q int) bool { sort.SliceStable(cachedFiles, func(p, q int) bool {
return strings.ToLower(indexDump[p]) < strings.ToLower(indexDump[q]) return strings.ToLower(cachedFiles[p]) < strings.ToLower(cachedFiles[q])
}) })
var startIndex, stopIndex int var startIndex, stopIndex int
@ -159,14 +159,14 @@ func serveIndexJson(args []string, index *FileIndex) httprouter.Handle {
} }
if startIndex > (fileCount - 1) { if startIndex > (fileCount - 1) {
indexDump = []string{} cachedFiles = []string{}
} }
if stopIndex > fileCount { if stopIndex > fileCount {
stopIndex = fileCount stopIndex = fileCount
} }
response, err := json.MarshalIndent(indexDump[startIndex:stopIndex], "", " ") response, err := json.MarshalIndent(cachedFiles[startIndex:stopIndex], "", " ")
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -179,7 +179,7 @@ func serveIndexJson(args []string, index *FileIndex) httprouter.Handle {
if Verbose { if Verbose {
fmt.Printf("%s | Served JSON index page (%s) to %s in %s\n", fmt.Printf("%s | Served JSON index page (%s) to %s in %s\n",
startTime.Format(LogDate), startTime.Format(logDate),
humanReadableSize(len(response)), humanReadableSize(len(response)),
realIP(r), realIP(r),
time.Since(startTime).Round(time.Microsecond), time.Since(startTime).Round(time.Microsecond),
@ -200,7 +200,7 @@ func serveAvailableExtensions() httprouter.Handle {
if Verbose { if Verbose {
fmt.Printf("%s | Served available extensions list (%s) to %s in %s\n", fmt.Printf("%s | Served available extensions list (%s) to %s in %s\n",
startTime.Format(LogDate), startTime.Format(logDate),
humanReadableSize(len(response)), humanReadableSize(len(response)),
realIP(r), realIP(r),
time.Since(startTime).Round(time.Microsecond), time.Since(startTime).Round(time.Microsecond),
@ -221,7 +221,7 @@ func serveEnabledExtensions(formats *types.Types) httprouter.Handle {
if Verbose { if Verbose {
fmt.Printf("%s | Served registered extensions list (%s) to %s in %s\n", fmt.Printf("%s | Served registered extensions list (%s) to %s in %s\n",
startTime.Format(LogDate), startTime.Format(logDate),
humanReadableSize(len(response)), humanReadableSize(len(response)),
realIP(r), realIP(r),
time.Since(startTime).Round(time.Microsecond), time.Since(startTime).Round(time.Microsecond),
@ -242,7 +242,7 @@ func serveAvailableMimeTypes() httprouter.Handle {
if Verbose { if Verbose {
fmt.Printf("%s | Served available MIME types list (%s) to %s in %s\n", fmt.Printf("%s | Served available MIME types list (%s) to %s in %s\n",
startTime.Format(LogDate), startTime.Format(logDate),
humanReadableSize(len(response)), humanReadableSize(len(response)),
realIP(r), realIP(r),
time.Since(startTime).Round(time.Microsecond), time.Since(startTime).Round(time.Microsecond),
@ -263,7 +263,7 @@ func serveEnabledMimeTypes(formats *types.Types) httprouter.Handle {
if Verbose { if Verbose {
fmt.Printf("%s | Served registered MIME types list (%s) to %s in %s\n", fmt.Printf("%s | Served registered MIME types list (%s) to %s in %s\n",
startTime.Format(LogDate), startTime.Format(logDate),
humanReadableSize(len(response)), humanReadableSize(len(response)),
realIP(r), realIP(r),
time.Since(startTime).Round(time.Microsecond), time.Since(startTime).Round(time.Microsecond),

View File

@ -12,7 +12,7 @@ import (
) )
const ( const (
ReleaseVersion string = "0.76.0" ReleaseVersion string = "0.77.0"
) )
var ( var (

View File

@ -40,7 +40,7 @@ func (s *ServeStats) incrementCounter(file string, timestamp time.Time, filesize
s.count[file]++ s.count[file]++
s.times[file] = append(s.times[file], timestamp.Format(LogDate)) s.times[file] = append(s.times[file], timestamp.Format(logDate))
_, exists := s.size[file] _, exists := s.size[file]
if !exists { if !exists {
@ -233,7 +233,7 @@ func serveStats(args []string, stats *ServeStats) httprouter.Handle {
if Verbose { if Verbose {
fmt.Printf("%s | Served statistics page (%s) to %s in %s\n", fmt.Printf("%s | Served statistics page (%s) to %s in %s\n",
startTime.Format(LogDate), startTime.Format(logDate),
humanReadableSize(len(response)), humanReadableSize(len(response)),
realIP(r), realIP(r),
time.Since(startTime).Round(time.Microsecond), time.Since(startTime).Round(time.Microsecond),

View File

@ -34,7 +34,7 @@ func refreshInterval(r *http.Request) (int64, string) {
} }
} }
func SortOrder(r *http.Request) string { func sortOrder(r *http.Request) string {
sortOrder := r.URL.Query().Get("sort") sortOrder := r.URL.Query().Get("sort")
if sortOrder == "asc" || sortOrder == "desc" { if sortOrder == "asc" || sortOrder == "desc" {
return sortOrder return sortOrder
@ -43,7 +43,7 @@ func SortOrder(r *http.Request) string {
return "" return ""
} }
func splitQueryParams(query string, Regexes *Regexes) []string { func splitQueryParams(query string, regexes *regexes) []string {
results := []string{} results := []string{}
if query == "" { if query == "" {
@ -53,7 +53,7 @@ func splitQueryParams(query string, Regexes *Regexes) []string {
params := strings.Split(query, ",") params := strings.Split(query, ",")
for i := 0; i < len(params); i++ { for i := 0; i < len(params); i++ {
if Regexes.alphanumeric.MatchString(params[i]) { if regexes.alphanumeric.MatchString(params[i]) {
results = append(results, strings.ToLower(params[i])) results = append(results, strings.ToLower(params[i]))
} }
} }
@ -61,7 +61,7 @@ func splitQueryParams(query string, Regexes *Regexes) []string {
return results return results
} }
func generateQueryParams(filters *Filters, sortOrder, refreshInterval string) string { func generateQueryParams(filters *filters, sortOrder, refreshInterval string) string {
var hasParams bool var hasParams bool
var queryParams strings.Builder var queryParams strings.Builder
@ -70,13 +70,13 @@ func generateQueryParams(filters *Filters, sortOrder, refreshInterval string) st
if Filtering { if Filtering {
queryParams.WriteString("include=") queryParams.WriteString("include=")
if filters.HasIncludes() { if filters.hasIncludes() {
queryParams.WriteString(filters.Includes()) queryParams.WriteString(filters.includes())
} }
queryParams.WriteString("&exclude=") queryParams.WriteString("&exclude=")
if filters.HasExcludes() { if filters.hasExcludes() {
queryParams.WriteString(filters.Excludes()) queryParams.WriteString(filters.excludes())
} }
hasParams = true hasParams = true
@ -100,8 +100,8 @@ func generateQueryParams(filters *Filters, sortOrder, refreshInterval string) st
return queryParams.String() return queryParams.String()
} }
func stripQueryParams(u string) (string, error) { func stripQueryParams(request string) (string, error) {
uri, err := url.Parse(u) uri, err := url.Parse(request)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -123,7 +123,7 @@ func stripQueryParams(u string) (string, error) {
func generateFileUri(path string) string { func generateFileUri(path string) string {
var uri strings.Builder var uri strings.Builder
uri.WriteString(SourcePrefix) uri.WriteString(sourcePrefix)
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
uri.WriteString(`/`) uri.WriteString(`/`)
} }

View File

@ -35,16 +35,16 @@ import (
) )
const ( const (
LogDate string = `2006-01-02T15:04:05.000-07:00` logDate string = `2006-01-02T15:04:05.000-07:00`
SourcePrefix string = `/source` sourcePrefix string = `/source`
MediaPrefix string = `/view` mediaPrefix string = `/view`
RedirectStatusCode int = http.StatusSeeOther RedirectStatusCode int = http.StatusSeeOther
Timeout time.Duration = 10 * time.Second timeout time.Duration = 10 * time.Second
) )
func serveStaticFile(paths []string, stats *ServeStats, index *FileIndex) httprouter.Handle { func serveStaticFile(paths []string, stats *ServeStats, cache *fileCache) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
path := strings.TrimPrefix(r.URL.Path, SourcePrefix) path := strings.TrimPrefix(r.URL.Path, sourcePrefix)
prefixedFilePath, err := stripQueryParams(path) prefixedFilePath, err := stripQueryParams(path)
if err != nil { if err != nil {
@ -55,7 +55,7 @@ func serveStaticFile(paths []string, stats *ServeStats, index *FileIndex) httpro
return return
} }
filePath, err := filepath.EvalSymlinks(strings.TrimPrefix(prefixedFilePath, SourcePrefix)) filePath, err := filepath.EvalSymlinks(strings.TrimPrefix(prefixedFilePath, sourcePrefix))
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -111,13 +111,13 @@ func serveStaticFile(paths []string, stats *ServeStats, index *FileIndex) httpro
} }
if Cache { if Cache {
index.Remove(filePath) cache.remove(filePath)
} }
} }
if Verbose { if Verbose {
fmt.Printf("%s | Served %s (%s) to %s in %s\n", fmt.Printf("%s | Served %s (%s) to %s in %s\n",
startTime.Format(LogDate), startTime.Format(logDate),
filePath, filePath,
fileSize, fileSize,
realIP(r), realIP(r),
@ -132,7 +132,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, cache *fileCache, 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 {
@ -143,21 +143,21 @@ func serveRoot(paths []string, Regexes *Regexes, index *FileIndex, formats *type
return return
} }
strippedRefererUri := strings.TrimPrefix(refererUri, MediaPrefix) strippedRefererUri := strings.TrimPrefix(refererUri, mediaPrefix)
filters := &Filters{ filters := &filters{
includes: splitQueryParams(r.URL.Query().Get("include"), Regexes), included: splitQueryParams(r.URL.Query().Get("include"), regexes),
excludes: splitQueryParams(r.URL.Query().Get("exclude"), Regexes), excluded: splitQueryParams(r.URL.Query().Get("exclude"), regexes),
} }
sortOrder := SortOrder(r) sortOrder := sortOrder(r)
_, refreshInterval := refreshInterval(r) _, refreshInterval := refreshInterval(r)
var filePath string var filePath string
if refererUri != "" { if refererUri != "" {
filePath, err = nextFile(strippedRefererUri, sortOrder, Regexes, formats) filePath, err = nextFile(strippedRefererUri, sortOrder, regexes, formats)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -168,7 +168,7 @@ func serveRoot(paths []string, Regexes *Regexes, index *FileIndex, formats *type
} }
loop: loop:
for timeout := time.After(Timeout); ; { for timeout := time.After(timeout); ; {
select { select {
case <-timeout: case <-timeout:
break loop break loop
@ -179,7 +179,7 @@ func serveRoot(paths []string, Regexes *Regexes, index *FileIndex, formats *type
break loop break loop
} }
filePath, err = newFile(paths, filters, sortOrder, Regexes, index, formats) filePath, err = newFile(paths, filters, sortOrder, regexes, cache, formats)
switch { switch {
case err != nil && err == ErrNoMediaFound: case err != nil && err == ErrNoMediaFound:
notFound(w, r, filePath) notFound(w, r, filePath)
@ -205,22 +205,22 @@ 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, 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), included: splitQueryParams(r.URL.Query().Get("include"), regexes),
excludes: splitQueryParams(r.URL.Query().Get("exclude"), Regexes), excluded: splitQueryParams(r.URL.Query().Get("exclude"), regexes),
} }
sortOrder := SortOrder(r) sortOrder := sortOrder(r)
filePath := strings.TrimPrefix(r.URL.Path, MediaPrefix) path := strings.TrimPrefix(r.URL.Path, mediaPrefix)
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
filePath = strings.TrimPrefix(filePath, "/") path = strings.TrimPrefix(path, "/")
} }
exists, err := fileExists(filePath) exists, err := fileExists(path)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -229,12 +229,12 @@ func serveMedia(paths []string, Regexes *Regexes, index *FileIndex, formats *typ
return return
} }
if !exists { if !exists {
notFound(w, r, filePath) notFound(w, r, path)
return return
} }
registered, fileType, mimeType, err := types.FileType(filePath, formats) registered, fileType, mimeType, err := formats.FileType(path)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -244,14 +244,14 @@ func serveMedia(paths []string, Regexes *Regexes, index *FileIndex, formats *typ
} }
if !registered { if !registered {
notFound(w, r, filePath) notFound(w, r, path)
return return
} }
fileUri := generateFileUri(filePath) fileUri := generateFileUri(path)
fileName := filepath.Base(filePath) fileName := filepath.Base(path)
w.Header().Add("Content-Type", "text/html") w.Header().Add("Content-Type", "text/html")
@ -261,16 +261,16 @@ func serveMedia(paths []string, Regexes *Regexes, index *FileIndex, formats *typ
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(fmt.Sprintf(`<style>%s</style>`, fileType.Css())) htmlBody.WriteString(fmt.Sprintf(`<style>%s</style>`, fileType.Css()))
htmlBody.WriteString((fileType.Title(queryParams, fileUri, filePath, fileName, mimeType))) htmlBody.WriteString((fileType.Title(queryParams, fileUri, path, fileName, mimeType)))
htmlBody.WriteString(`</head><body>`) htmlBody.WriteString(`</head><body>`)
if refreshInterval != "0ms" { if refreshInterval != "0ms" {
htmlBody.WriteString(fmt.Sprintf("<script>window.onload = function(){setInterval(function(){window.location.href = '/%s';}, %d);};</script>", htmlBody.WriteString(fmt.Sprintf("<script>window.onload = function(){setInterval(function(){window.location.href = '/%s';}, %d);};</script>",
queryParams, queryParams,
refreshTimer)) refreshTimer))
} }
htmlBody.WriteString((fileType.Body(queryParams, fileUri, filePath, fileName, mimeType))) htmlBody.WriteString((fileType.Body(queryParams, fileUri, path, fileName, mimeType)))
htmlBody.WriteString(`</body></html>`) htmlBody.WriteString(`</body></html>`)
_, err = io.WriteString(w, gohtml.Format(htmlBody.String())) _, err = io.WriteString(w, gohtml.Format(htmlBody.String()))
@ -322,25 +322,25 @@ func ServePage(args []string) error {
} }
if Audio || All { if Audio || All {
formats.Add(audio.Format{}) formats.Add(audio.New())
} }
if Flash || All { if Flash || All {
formats.Add(flash.Format{}) formats.Add(flash.New())
} }
if Text || All { if Text || All {
formats.Add(text.Format{}) formats.Add(text.New())
} }
if Videos || All { if Videos || All {
formats.Add(video.Format{}) formats.Add(video.New())
} }
// 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 rootCmd.MarkFlagsOneRequired on next spf13/cobra update // to be replaced with rootCmd.MarkFlagsOneRequired on next spf13/cobra update
if Images || All || len(formats.Extensions) == 0 { if Images || All || len(formats.Extensions) == 0 {
formats.Add(images.Format{}) formats.Add(images.New())
} }
paths, err := normalizePaths(args, formats) paths, err := normalizePaths(args, formats)
@ -356,12 +356,12 @@ func ServePage(args []string) error {
fmt.Printf("WARNING! Files *will* be deleted after serving!\n\n") fmt.Printf("WARNING! Files *will* be deleted after serving!\n\n")
} }
index := &FileIndex{ cache := &fileCache{
mutex: sync.RWMutex{}, mutex: sync.RWMutex{},
list: []string{}, list: []string{},
} }
regexes := &Regexes{ regexes := &regexes{
filename: regexp.MustCompile(`(.+)([0-9]{3})(\..+)`), filename: regexp.MustCompile(`(.+)([0-9]{3})(\..+)`),
alphanumeric: regexp.MustCompile(`^[A-z0-9]*$`), alphanumeric: regexp.MustCompile(`^[A-z0-9]*$`),
} }
@ -384,15 +384,15 @@ func ServePage(args []string) error {
mux.PanicHandler = serverErrorHandler() mux.PanicHandler = serverErrorHandler()
mux.GET("/", serveRoot(paths, regexes, index, formats)) mux.GET("/", serveRoot(paths, regexes, cache, 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, formats)) mux.GET(mediaPrefix+"/*media", serveMedia(paths, regexes, formats))
mux.GET(SourcePrefix+"/*static", serveStaticFile(paths, stats, index)) mux.GET(sourcePrefix+"/*static", serveStaticFile(paths, stats, cache))
mux.GET("/version", serveVersion()) mux.GET("/version", serveVersion())
@ -400,29 +400,29 @@ func ServePage(args []string) error {
skipIndex := false skipIndex := false
if CacheFile != "" { if CacheFile != "" {
err := index.Import(CacheFile) err := cache.Import(CacheFile)
if err == nil { if err == nil {
skipIndex = true skipIndex = true
} }
} }
if !skipIndex { if !skipIndex {
index.generateCache(args, formats) cache.generate(args, formats)
} }
mux.GET("/clear_cache", serveCacheClear(args, index, formats)) mux.GET("/clear_cache", serveCacheClear(args, cache, formats))
} }
if Info { if Info {
if Cache { if Cache {
mux.GET("/html/", serveIndexHtml(args, index, false)) mux.GET("/html/", serveIndexHtml(args, cache, false))
if PageLength != 0 { if PageLength != 0 {
mux.GET("/html/:page", serveIndexHtml(args, index, true)) mux.GET("/html/:page", serveIndexHtml(args, cache, true))
} }
mux.GET("/json", serveIndexJson(args, index)) mux.GET("/json", serveIndexJson(args, cache))
if PageLength != 0 { if PageLength != 0 {
mux.GET("/json/:page", serveIndexJson(args, index)) mux.GET("/json/:page", serveIndexJson(args, cache))
} }
} }

View File

@ -57,8 +57,10 @@ func (t Format) Validate(filePath string) bool {
return true return true
} }
func init() { func New() Format {
format := Format{} return Format{}
}
types.Register(format)
func init() {
types.SupportedFormats.Register(New())
} }

View File

@ -51,8 +51,10 @@ func (t Format) Validate(filePath string) bool {
return true return true
} }
func init() { func New() Format {
format := Format{} return Format{}
}
types.Register(format)
func init() {
types.SupportedFormats.Register(New())
} }

View File

@ -123,8 +123,10 @@ func ImageDimensions(path string) (*dimensions, error) {
return &dimensions{width: decodedConfig.Width, height: decodedConfig.Height}, nil return &dimensions{width: decodedConfig.Width, height: decodedConfig.Height}, nil
} }
func init() { func New() Format {
format := Format{} return Format{}
}
types.Register(format)
func init() {
types.SupportedFormats.Register(New())
} }

View File

@ -85,8 +85,10 @@ func (t Format) Validate(filePath string) bool {
return utf8.Valid(head) return utf8.Valid(head)
} }
func init() { func New() Format {
format := Format{} return Format{}
}
types.Register(format)
func init() {
types.SupportedFormats.Register(New())
} }

View File

@ -32,23 +32,23 @@ type Types struct {
MimeTypes map[string]Type MimeTypes map[string]Type
} }
func (s *Types) Add(t Type) { func (t *Types) Add(format Type) {
for k, v := range t.Extensions() { for k, v := range format.Extensions() {
_, exists := s.Extensions[k] _, exists := t.Extensions[k]
if !exists { if !exists {
s.Extensions[k] = v t.Extensions[k] = v
} }
} }
for _, v := range t.MimeTypes() { for _, v := range format.MimeTypes() {
_, exists := s.Extensions[v] _, exists := t.Extensions[v]
if !exists { if !exists {
s.MimeTypes[v] = t t.MimeTypes[v] = format
} }
} }
} }
func FileType(path string, registeredFormats *Types) (bool, Type, string, error) { func (t *Types) FileType(path string) (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):
@ -64,15 +64,15 @@ func FileType(path string, registeredFormats *Types) (bool, Type, string, error)
mimeType := http.DetectContentType(head) mimeType := http.DetectContentType(head)
// try identifying files by mime types first // try identifying files by mime types first
fileType, exists := registeredFormats.MimeTypes[mimeType] fileType, exists := t.MimeTypes[mimeType]
if exists { if exists {
return fileType.Validate(path), fileType, mimeType, nil return fileType.Validate(path), fileType, mimeType, nil
} }
// if mime type detection fails, use the file extension // if mime type detection fails, use the file extension
mimeType, exists = registeredFormats.Extensions[filepath.Ext(path)] mimeType, exists = t.Extensions[filepath.Ext(path)]
if exists { if exists {
fileType, exists := registeredFormats.MimeTypes[mimeType] fileType, exists := t.MimeTypes[mimeType]
if exists { if exists {
return fileType.Validate(path), fileType, mimeType, nil return fileType.Validate(path), fileType, mimeType, nil
@ -82,11 +82,11 @@ func FileType(path string, registeredFormats *Types) (bool, Type, string, error)
return false, nil, "", nil return false, nil, "", nil
} }
func Register(t Type) { func (t *Types) Register(format Type) {
SupportedFormats.Add(t) t.Add(format)
} }
func (t Types) GetExtensions() string { func (t *Types) GetExtensions() string {
var output strings.Builder var output strings.Builder
extensions := make([]string, len(t.Extensions)) extensions := make([]string, len(t.Extensions))
@ -107,7 +107,7 @@ func (t Types) GetExtensions() string {
return output.String() return output.String()
} }
func (t Types) GetMimeTypes() string { func (t *Types) GetMimeTypes() string {
var output strings.Builder var output strings.Builder
mimeTypes := make([]string, len(t.MimeTypes)) mimeTypes := make([]string, len(t.MimeTypes))

View File

@ -57,8 +57,10 @@ func (t Format) Validate(filePath string) bool {
return true return true
} }
func init() { func New() Format {
format := Format{} return Format{}
}
types.Register(format)
func init() {
types.SupportedFormats.Register(New())
} }