Compare commits

..

No commits in common. "d2d9418b8b7b8e9821ce911cc0d1d826b6d728d7" and "3ea2227a9fa3fdb6d007cf2248f1dcaa85c8342c" have entirely different histories.

17 changed files with 386 additions and 437 deletions

View File

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

View File

@ -24,7 +24,7 @@ func newErrorPage(title, body string) string {
var htmlBody strings.Builder
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(fmt.Sprintf("<title>%s</title></head>", title))
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()
}
func notFound(w http.ResponseWriter, r *http.Request, path string) error {
func notFound(w http.ResponseWriter, r *http.Request, filePath string) error {
startTime := time.Now()
if Verbose {
fmt.Printf("%s | Unavailable file %s requested by %s\n",
startTime.Format(logDate),
path,
startTime.Format(LogDate),
filePath,
r.RemoteAddr,
)
}
@ -59,7 +59,7 @@ func serverError(w http.ResponseWriter, r *http.Request, i interface{}) {
if Verbose {
fmt.Printf("%s | Invalid request for %s from %s\n",
startTime.Format(logDate),
startTime.Format(LogDate),
r.URL.Path,
r.RemoteAddr,
)

View File

@ -18,7 +18,7 @@ import (
var favicons embed.FS
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="16x16" href="/favicons/favicon-16x16.png">
<link rel="manifest" href="/favicons/site.webmanifest">

View File

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

View File

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

View File

@ -19,13 +19,13 @@ import (
"seedno.de/seednode/roulette/types"
)
func serveIndexHtml(args []string, cache *fileCache, paginate bool) httprouter.Handle {
func serveIndexHtml(args []string, index *FileIndex, paginate bool) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Header().Set("Content-Type", "text/html")
startTime := time.Now()
indexDump := cache.List()
indexDump := index.Index()
fileCount := len(indexDump)
@ -54,7 +54,7 @@ func serveIndexHtml(args []string, cache *fileCache, paginate bool) httprouter.H
var htmlBody strings.Builder
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(`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))
@ -65,7 +65,7 @@ func serveIndexHtml(args []string, cache *fileCache, paginate bool) httprouter.H
if Sorting {
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 {
@ -124,7 +124,7 @@ func serveIndexHtml(args []string, cache *fileCache, paginate bool) httprouter.H
if Verbose {
fmt.Printf("%s | Served HTML index page (%s) to %s in %s\n",
startTime.Format(logDate),
startTime.Format(LogDate),
humanReadableSize(b),
realIP(r),
time.Since(startTime).Round(time.Microsecond),
@ -133,18 +133,18 @@ func serveIndexHtml(args []string, cache *fileCache, paginate bool) httprouter.H
}
}
func serveIndexJson(args []string, index *fileCache) httprouter.Handle {
func serveIndexJson(args []string, index *FileIndex) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Header().Set("Content-Type", "application/json")
startTime := time.Now()
cachedFiles := index.List()
indexDump := index.Index()
fileCount := len(cachedFiles)
fileCount := len(indexDump)
sort.SliceStable(cachedFiles, func(p, q int) bool {
return strings.ToLower(cachedFiles[p]) < strings.ToLower(cachedFiles[q])
sort.SliceStable(indexDump, func(p, q int) bool {
return strings.ToLower(indexDump[p]) < strings.ToLower(indexDump[q])
})
var startIndex, stopIndex int
@ -159,14 +159,14 @@ func serveIndexJson(args []string, index *fileCache) httprouter.Handle {
}
if startIndex > (fileCount - 1) {
cachedFiles = []string{}
indexDump = []string{}
}
if stopIndex > fileCount {
stopIndex = fileCount
}
response, err := json.MarshalIndent(cachedFiles[startIndex:stopIndex], "", " ")
response, err := json.MarshalIndent(indexDump[startIndex:stopIndex], "", " ")
if err != nil {
fmt.Println(err)
@ -179,7 +179,7 @@ func serveIndexJson(args []string, index *fileCache) httprouter.Handle {
if Verbose {
fmt.Printf("%s | Served JSON index page (%s) to %s in %s\n",
startTime.Format(logDate),
startTime.Format(LogDate),
humanReadableSize(len(response)),
realIP(r),
time.Since(startTime).Round(time.Microsecond),
@ -200,7 +200,7 @@ func serveAvailableExtensions() httprouter.Handle {
if Verbose {
fmt.Printf("%s | Served available extensions list (%s) to %s in %s\n",
startTime.Format(logDate),
startTime.Format(LogDate),
humanReadableSize(len(response)),
realIP(r),
time.Since(startTime).Round(time.Microsecond),
@ -221,7 +221,7 @@ func serveEnabledExtensions(formats *types.Types) httprouter.Handle {
if Verbose {
fmt.Printf("%s | Served registered extensions list (%s) to %s in %s\n",
startTime.Format(logDate),
startTime.Format(LogDate),
humanReadableSize(len(response)),
realIP(r),
time.Since(startTime).Round(time.Microsecond),
@ -242,7 +242,7 @@ func serveAvailableMimeTypes() httprouter.Handle {
if Verbose {
fmt.Printf("%s | Served available MIME types list (%s) to %s in %s\n",
startTime.Format(logDate),
startTime.Format(LogDate),
humanReadableSize(len(response)),
realIP(r),
time.Since(startTime).Round(time.Microsecond),
@ -263,7 +263,7 @@ func serveEnabledMimeTypes(formats *types.Types) httprouter.Handle {
if Verbose {
fmt.Printf("%s | Served registered MIME types list (%s) to %s in %s\n",
startTime.Format(logDate),
startTime.Format(LogDate),
humanReadableSize(len(response)),
realIP(r),
time.Since(startTime).Round(time.Microsecond),
@ -271,22 +271,3 @@ func serveEnabledMimeTypes(formats *types.Types) httprouter.Handle {
}
}
}
func registerInfoHandlers(mux *httprouter.Router, args []string, cache *fileCache, formats *types.Types) {
if Cache {
mux.GET("/html/", serveIndexHtml(args, cache, false))
if PageLength != 0 {
mux.GET("/html/:page", serveIndexHtml(args, cache, true))
}
mux.GET("/json", serveIndexJson(args, cache))
if PageLength != 0 {
mux.GET("/json/:page", serveIndexJson(args, cache))
}
}
mux.GET("/available_extensions", serveAvailableExtensions())
mux.GET("/enabled_extensions", serveEnabledExtensions(formats))
mux.GET("/available_mime_types", serveAvailableMimeTypes())
mux.GET("/enabled_mime_types", serveEnabledMimeTypes(formats))
}

View File

@ -1,19 +0,0 @@
/*
Copyright © 2023 Seednode <seednode@seedno.de>
*/
package cmd
import (
"net/http/pprof"
"github.com/julienschmidt/httprouter"
)
func registerProfileHandlers(mux *httprouter.Router) {
mux.HandlerFunc("GET", "/debug/pprof/", pprof.Index)
mux.HandlerFunc("GET", "/debug/pprof/cmdline", pprof.Cmdline)
mux.HandlerFunc("GET", "/debug/pprof/profile", pprof.Profile)
mux.HandlerFunc("GET", "/debug/pprof/symbol", pprof.Symbol)
mux.HandlerFunc("GET", "/debug/pprof/trace", pprof.Trace)
}

View File

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

View File

@ -20,7 +20,7 @@ import (
"github.com/klauspost/compress/zstd"
)
type serveStats struct {
type ServeStats struct {
mutex sync.RWMutex
list []string
count map[string]uint32
@ -28,93 +28,123 @@ type serveStats struct {
times map[string][]string
}
type publicServeStats struct {
type exportedServeStats struct {
List []string
Count map[string]uint32
Size map[string]string
Times map[string][]string
}
type timesServed struct {
File string
Served uint32
Size string
Times []string
}
func (s *ServeStats) incrementCounter(file string, timestamp time.Time, filesize string) {
s.mutex.Lock()
func (stats *serveStats) incrementCounter(file string, timestamp time.Time, filesize string) {
stats.mutex.Lock()
s.count[file]++
stats.count[file]++
s.times[file] = append(s.times[file], timestamp.Format(LogDate))
stats.times[file] = append(stats.times[file], timestamp.Format(logDate))
_, exists := stats.size[file]
_, exists := s.size[file]
if !exists {
stats.size[file] = filesize
s.size[file] = filesize
}
if !contains(stats.list, file) {
stats.list = append(stats.list, file)
if !contains(s.list, file) {
s.list = append(s.list, file)
}
stats.mutex.Unlock()
s.mutex.Unlock()
}
func (stats *serveStats) Import(source *publicServeStats) {
stats.mutex.Lock()
copy(stats.list, source.List)
for k, v := range source.Count {
fmt.Printf("Setting count[%s] to %d\n", k, v)
stats.count[k] = v
func (s *ServeStats) toExported() *exportedServeStats {
stats := &exportedServeStats{
List: make([]string, len(s.list)),
Count: make(map[string]uint32),
Size: make(map[string]string),
Times: make(map[string][]string),
}
for k, v := range source.Size {
fmt.Printf("Setting size[%s] to %v\n", k, v)
s.mutex.RLock()
stats.size[k] = v
}
copy(stats.List, s.list)
for k, v := range source.Times {
fmt.Printf("Setting times[%s] to %v\n", k, v)
stats.times[k] = v
}
stats.mutex.Unlock()
}
func (source *serveStats) Export() *publicServeStats {
source.mutex.RLock()
stats := &publicServeStats{
List: make([]string, len(source.list)),
Count: make(map[string]uint32, len(source.count)),
Size: make(map[string]string, len(source.size)),
Times: make(map[string][]string, len(source.times)),
}
copy(stats.List, source.list)
for k, v := range source.count {
for k, v := range s.count {
stats.Count[k] = v
}
for k, v := range source.size {
for k, v := range s.size {
stats.Size[k] = v
}
for k, v := range source.times {
for k, v := range s.times {
stats.Times[k] = v
}
source.mutex.RUnlock()
s.mutex.RUnlock()
return stats
}
func (stats *serveStats) exportFile(path string) error {
func (s *ServeStats) toImported(stats *exportedServeStats) {
s.mutex.Lock()
s.list = make([]string, len(stats.List))
copy(s.list, stats.List)
for k, v := range stats.Count {
s.count[k] = v
}
for k, v := range stats.Size {
s.size[k] = v
}
for k, v := range stats.Times {
s.times[k] = v
}
s.mutex.Unlock()
}
func (s *ServeStats) ListFiles(page int) ([]byte, error) {
stats := s.toExported()
sort.SliceStable(stats.List, func(p, q int) bool {
return strings.ToLower(stats.List[p]) < strings.ToLower(stats.List[q])
})
var startIndex, stopIndex int
if page == -1 {
startIndex = 0
stopIndex = len(stats.List) - 1
} else {
startIndex = ((page - 1) * int(PageLength))
stopIndex = (startIndex + int(PageLength))
}
if startIndex > len(stats.List)-1 {
return []byte("{}"), nil
}
if stopIndex > len(stats.List)-1 {
stopIndex = len(stats.List) - 1
}
a := make([]timesServed, stopIndex-startIndex)
for k, v := range stats.List[startIndex:stopIndex] {
a[k] = timesServed{v, stats.Count[v], stats.Size[v], stats.Times[v]}
}
r, err := json.MarshalIndent(a, "", " ")
if err != nil {
return []byte{}, err
}
return r, nil
}
func (s *ServeStats) Export(path string) error {
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
@ -129,7 +159,9 @@ func (stats *serveStats) exportFile(path string) error {
enc := gob.NewEncoder(z)
err = enc.Encode(stats.Export())
stats := s.toExported()
err = enc.Encode(&stats)
if err != nil {
return err
}
@ -137,7 +169,7 @@ func (stats *serveStats) exportFile(path string) error {
return nil
}
func (stats *serveStats) importFile(path string) error {
func (s *ServeStats) Import(path string) error {
file, err := os.OpenFile(path, os.O_RDONLY, 0600)
if err != nil {
return err
@ -152,64 +184,33 @@ func (stats *serveStats) importFile(path string) error {
dec := gob.NewDecoder(z)
source := &publicServeStats{
stats := &exportedServeStats{
List: []string{},
Count: make(map[string]uint32),
Size: make(map[string]string),
Times: make(map[string][]string),
}
err = dec.Decode(source)
err = dec.Decode(stats)
if err != nil {
return err
}
stats.Import(source)
s.toImported(stats)
return nil
}
func (source *serveStats) listFiles(page int) ([]byte, error) {
stats := source.Export()
sort.SliceStable(stats.List, func(p, q int) bool {
return strings.ToLower(stats.List[p]) < strings.ToLower(stats.List[q])
})
var startIndex, stopIndex int
if page == -1 {
startIndex = 0
stopIndex = len(stats.List)
} else {
startIndex = ((page - 1) * int(PageLength))
stopIndex = (startIndex + int(PageLength))
type timesServed struct {
File string
Served uint32
Size string
Times []string
}
if startIndex > len(stats.List)-1 {
return []byte("{}"), nil
}
if stopIndex > len(stats.List) {
stopIndex = len(stats.List)
}
a := make([]timesServed, (stopIndex - startIndex))
for k, v := range stats.List[startIndex:stopIndex] {
a[k] = timesServed{v, stats.Count[v], stats.Size[v], stats.Times[v]}
}
r, err := json.MarshalIndent(a, "", " ")
if err != nil {
return []byte{}, err
}
return r, nil
}
func serveStatsPage(args []string, stats *serveStats) httprouter.Handle {
func serveStats(args []string, stats *ServeStats) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
startTime := time.Now()
page, err := strconv.Atoi(p.ByName("page"))
@ -217,7 +218,7 @@ func serveStatsPage(args []string, stats *serveStats) httprouter.Handle {
page = -1
}
response, err := stats.listFiles(page)
response, err := stats.ListFiles(page)
if err != nil {
fmt.Println(err)
@ -232,7 +233,7 @@ func serveStatsPage(args []string, stats *serveStats) httprouter.Handle {
if Verbose {
fmt.Printf("%s | Served statistics page (%s) to %s in %s\n",
startTime.Format(logDate),
startTime.Format(LogDate),
humanReadableSize(len(response)),
realIP(r),
time.Since(startTime).Round(time.Microsecond),
@ -240,14 +241,7 @@ func serveStatsPage(args []string, stats *serveStats) httprouter.Handle {
}
if StatisticsFile != "" {
stats.exportFile(StatisticsFile)
stats.Export(StatisticsFile)
}
}
}
func registerStatsHandlers(mux *httprouter.Router, args []string, stats *serveStats) {
mux.GET("/stats", serveStatsPage(args, stats))
if PageLength != 0 {
mux.GET("/stats/:page", serveStatsPage(args, stats))
}
}

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

View File

@ -22,6 +22,8 @@ import (
"syscall"
"time"
"net/http/pprof"
"github.com/julienschmidt/httprouter"
"github.com/yosssi/gohtml"
"seedno.de/seednode/roulette/types"
@ -33,16 +35,16 @@ import (
)
const (
logDate string = `2006-01-02T15:04:05.000-07:00`
sourcePrefix string = `/source`
mediaPrefix string = `/view`
LogDate string = `2006-01-02T15:04:05.000-07:00`
SourcePrefix string = `/source`
MediaPrefix string = `/view`
RedirectStatusCode int = http.StatusSeeOther
timeout time.Duration = 10 * time.Second
Timeout time.Duration = 10 * time.Second
)
func serveStaticFile(paths []string, stats *serveStats, cache *fileCache) httprouter.Handle {
func serveStaticFile(paths []string, stats *ServeStats, index *FileIndex) httprouter.Handle {
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)
if err != nil {
@ -53,7 +55,7 @@ func serveStaticFile(paths []string, stats *serveStats, cache *fileCache) httpro
return
}
filePath, err := filepath.EvalSymlinks(strings.TrimPrefix(prefixedFilePath, sourcePrefix))
filePath, err := filepath.EvalSymlinks(strings.TrimPrefix(prefixedFilePath, SourcePrefix))
if err != nil {
fmt.Println(err)
@ -109,13 +111,13 @@ func serveStaticFile(paths []string, stats *serveStats, cache *fileCache) httpro
}
if Cache {
cache.remove(filePath)
index.Remove(filePath)
}
}
if Verbose {
fmt.Printf("%s | Served %s (%s) to %s in %s\n",
startTime.Format(logDate),
startTime.Format(LogDate),
filePath,
fileSize,
realIP(r),
@ -130,7 +132,7 @@ func serveStaticFile(paths []string, stats *serveStats, cache *fileCache) httpro
}
}
func serveRoot(paths []string, regexes *regexes, cache *fileCache, formats *types.Types) httprouter.Handle {
func serveRoot(paths []string, Regexes *Regexes, index *FileIndex, formats *types.Types) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
refererUri, err := stripQueryParams(refererToUri(r.Referer()))
if err != nil {
@ -141,21 +143,21 @@ func serveRoot(paths []string, regexes *regexes, cache *fileCache, formats *type
return
}
strippedRefererUri := strings.TrimPrefix(refererUri, mediaPrefix)
strippedRefererUri := strings.TrimPrefix(refererUri, MediaPrefix)
filters := &filters{
included: splitQueryParams(r.URL.Query().Get("include"), regexes),
excluded: splitQueryParams(r.URL.Query().Get("exclude"), regexes),
filters := &Filters{
includes: splitQueryParams(r.URL.Query().Get("include"), Regexes),
excludes: splitQueryParams(r.URL.Query().Get("exclude"), Regexes),
}
sortOrder := sortOrder(r)
sortOrder := SortOrder(r)
_, refreshInterval := refreshInterval(r)
var filePath string
if refererUri != "" {
filePath, err = nextFile(strippedRefererUri, sortOrder, regexes, formats)
filePath, err = nextFile(strippedRefererUri, sortOrder, Regexes, formats)
if err != nil {
fmt.Println(err)
@ -166,7 +168,7 @@ func serveRoot(paths []string, regexes *regexes, cache *fileCache, formats *type
}
loop:
for timeout := time.After(timeout); ; {
for timeout := time.After(Timeout); ; {
select {
case <-timeout:
break loop
@ -177,7 +179,7 @@ func serveRoot(paths []string, regexes *regexes, cache *fileCache, formats *type
break loop
}
filePath, err = newFile(paths, filters, sortOrder, regexes, cache, formats)
filePath, err = newFile(paths, filters, sortOrder, Regexes, index, formats)
switch {
case err != nil && err == ErrNoMediaFound:
notFound(w, r, filePath)
@ -203,22 +205,22 @@ func serveRoot(paths []string, regexes *regexes, cache *fileCache, formats *type
}
}
func serveMedia(paths []string, regexes *regexes, formats *types.Types) httprouter.Handle {
func serveMedia(paths []string, Regexes *Regexes, index *FileIndex, formats *types.Types) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
filters := &filters{
included: splitQueryParams(r.URL.Query().Get("include"), regexes),
excluded: splitQueryParams(r.URL.Query().Get("exclude"), regexes),
filters := &Filters{
includes: splitQueryParams(r.URL.Query().Get("include"), Regexes),
excludes: splitQueryParams(r.URL.Query().Get("exclude"), Regexes),
}
sortOrder := sortOrder(r)
sortOrder := SortOrder(r)
path := strings.TrimPrefix(r.URL.Path, mediaPrefix)
filePath := strings.TrimPrefix(r.URL.Path, MediaPrefix)
if runtime.GOOS == "windows" {
path = strings.TrimPrefix(path, "/")
filePath = strings.TrimPrefix(filePath, "/")
}
exists, err := fileExists(path)
exists, err := fileExists(filePath)
if err != nil {
fmt.Println(err)
@ -227,12 +229,12 @@ func serveMedia(paths []string, regexes *regexes, formats *types.Types) httprout
return
}
if !exists {
notFound(w, r, path)
notFound(w, r, filePath)
return
}
registered, fileType, mimeType, err := formats.FileType(path)
registered, fileType, mimeType, err := types.FileType(filePath, formats)
if err != nil {
fmt.Println(err)
@ -242,14 +244,14 @@ func serveMedia(paths []string, regexes *regexes, formats *types.Types) httprout
}
if !registered {
notFound(w, r, path)
notFound(w, r, filePath)
return
}
fileUri := generateFileUri(path)
fileUri := generateFileUri(filePath)
fileName := filepath.Base(path)
fileName := filepath.Base(filePath)
w.Header().Add("Content-Type", "text/html")
@ -259,16 +261,16 @@ func serveMedia(paths []string, regexes *regexes, formats *types.Types) httprout
var htmlBody strings.Builder
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((fileType.Title(queryParams, fileUri, path, fileName, mimeType)))
htmlBody.WriteString((fileType.Title(queryParams, fileUri, filePath, fileName, mimeType)))
htmlBody.WriteString(`</head><body>`)
if refreshInterval != "0ms" {
htmlBody.WriteString(fmt.Sprintf("<script>window.onload = function(){setInterval(function(){window.location.href = '/%s';}, %d);};</script>",
queryParams,
refreshTimer))
}
htmlBody.WriteString((fileType.Body(queryParams, fileUri, path, fileName, mimeType)))
htmlBody.WriteString((fileType.Body(queryParams, fileUri, filePath, fileName, mimeType)))
htmlBody.WriteString(`</body></html>`)
_, err = io.WriteString(w, gohtml.Format(htmlBody.String()))
@ -312,31 +314,33 @@ func ServePage(args []string) error {
return errors.New("invalid bind address provided")
}
mux := httprouter.New()
formats := &types.Types{
Extensions: make(map[string]string),
MimeTypes: make(map[string]types.Type),
}
if Audio || All {
formats.Add(audio.New())
formats.Add(audio.Format{})
}
if Flash || All {
formats.Add(flash.New())
formats.Add(flash.Format{})
}
if Text || All {
formats.Add(text.New())
formats.Add(text.Format{})
}
if Videos || All {
formats.Add(video.New())
formats.Add(video.Format{})
}
// 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(images.New())
formats.Add(images.Format{})
}
paths, err := normalizePaths(args, formats)
@ -348,18 +352,20 @@ func ServePage(args []string) error {
return ErrNoMediaFound
}
cache := &fileCache{
if Russian {
fmt.Printf("WARNING! Files *will* be deleted after serving!\n\n")
}
index := &FileIndex{
mutex: sync.RWMutex{},
list: []string{},
}
regexes := &regexes{
regexes := &Regexes{
filename: regexp.MustCompile(`(.+)([0-9]{3})(\..+)`),
alphanumeric: regexp.MustCompile(`^[A-z0-9]*$`),
}
mux := httprouter.New()
srv := &http.Server{
Addr: net.JoinHostPort(Bind, strconv.Itoa(int(Port))),
Handler: mux,
@ -368,7 +374,7 @@ func ServePage(args []string) error {
WriteTimeout: 5 * time.Minute,
}
stats := &serveStats{
stats := &ServeStats{
mutex: sync.RWMutex{},
list: []string{},
count: make(map[string]uint32),
@ -378,45 +384,65 @@ func ServePage(args []string) error {
mux.PanicHandler = serverErrorHandler()
mux.GET("/", serveRoot(paths, regexes, cache, formats))
mux.GET("/", serveRoot(paths, regexes, index, formats))
mux.GET("/favicons/*favicon", serveFavicons())
mux.GET("/favicon.ico", serveFavicons())
mux.GET(mediaPrefix+"/*media", serveMedia(paths, regexes, formats))
mux.GET(MediaPrefix+"/*media", serveMedia(paths, regexes, index, formats))
mux.GET(sourcePrefix+"/*static", serveStaticFile(paths, stats, cache))
mux.GET(SourcePrefix+"/*static", serveStaticFile(paths, stats, index))
mux.GET("/version", serveVersion())
if Cache {
registerCacheHandlers(mux, args, cache, formats)
skipIndex := false
if CacheFile != "" {
err := index.Import(CacheFile)
if err == nil {
skipIndex = true
}
}
if !skipIndex {
index.generateCache(args, formats)
}
mux.GET("/clear_cache", serveCacheClear(args, index, formats))
}
if Info {
registerInfoHandlers(mux, args, cache, formats)
if Cache {
mux.GET("/html/", serveIndexHtml(args, index, false))
if PageLength != 0 {
mux.GET("/html/:page", serveIndexHtml(args, index, true))
}
mux.GET("/json", serveIndexJson(args, index))
if PageLength != 0 {
mux.GET("/json/:page", serveIndexJson(args, index))
}
}
mux.GET("/available_extensions", serveAvailableExtensions())
mux.GET("/enabled_extensions", serveEnabledExtensions(formats))
mux.GET("/available_mime_types", serveAvailableMimeTypes())
mux.GET("/enabled_mime_types", serveEnabledMimeTypes(formats))
}
if Profile {
registerProfileHandlers(mux)
}
if Russian {
fmt.Printf("WARNING! Files *will* be deleted after serving!\n\n")
mux.HandlerFunc("GET", "/debug/pprof/", pprof.Index)
mux.HandlerFunc("GET", "/debug/pprof/cmdline", pprof.Cmdline)
mux.HandlerFunc("GET", "/debug/pprof/profile", pprof.Profile)
mux.HandlerFunc("GET", "/debug/pprof/symbol", pprof.Symbol)
mux.HandlerFunc("GET", "/debug/pprof/trace", pprof.Trace)
}
if Statistics {
if StatisticsFile != "" {
StatisticsFile, err = normalizePath(StatisticsFile)
if err != nil {
return err
}
err := stats.importFile(StatisticsFile)
if err != nil {
return err
}
stats.Import(StatisticsFile)
gracefulShutdown := make(chan os.Signal, 1)
signal.Notify(gracefulShutdown, syscall.SIGINT, syscall.SIGTERM)
@ -424,13 +450,16 @@ func ServePage(args []string) error {
go func() {
<-gracefulShutdown
stats.exportFile(StatisticsFile)
stats.Export(StatisticsFile)
os.Exit(0)
}()
}
registerStatsHandlers(mux, args, stats)
mux.GET("/stats", serveStats(args, stats))
if PageLength != 0 {
mux.GET("/stats/:page", serveStats(args, stats))
}
}
err = srv.ListenAndServe()

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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