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" "seedno.de/seednode/roulette/types"
) )
type fileCache struct { type FileIndex struct {
mutex sync.RWMutex mutex sync.RWMutex
list []string list []string
} }
func (cache *fileCache) List() []string { func (i *FileIndex) Index() []string {
cache.mutex.RLock() i.mutex.RLock()
val := cache.list val := i.list
cache.mutex.RUnlock() i.mutex.RUnlock()
return val return val
} }
func (cache *fileCache) remove(path string) { func (i *FileIndex) Remove(path string) {
cache.mutex.RLock() i.mutex.RLock()
tempIndex := make([]string, len(cache.list)) tempIndex := make([]string, len(i.list))
copy(tempIndex, cache.list) copy(tempIndex, i.list)
cache.mutex.RUnlock() i.mutex.RUnlock()
var position int var position int
@ -46,39 +46,39 @@ func (cache *fileCache) remove(path string) {
tempIndex[position] = tempIndex[len(tempIndex)-1] tempIndex[position] = tempIndex[len(tempIndex)-1]
cache.mutex.Lock() i.mutex.Lock()
cache.list = make([]string, len(tempIndex)-1) i.list = make([]string, len(tempIndex)-1)
copy(cache.list, tempIndex[:len(tempIndex)-1]) copy(i.list, tempIndex[:len(tempIndex)-1])
cache.mutex.Unlock() i.mutex.Unlock()
} }
func (cache *fileCache) set(val []string) { func (i *FileIndex) setIndex(val []string) {
cache.mutex.Lock() i.mutex.Lock()
cache.list = val i.list = val
cache.mutex.Unlock() i.mutex.Unlock()
} }
func (cache *fileCache) generate(args []string, formats *types.Types) { func (i *FileIndex) generateCache(args []string, formats *types.Types) {
cache.mutex.Lock() i.mutex.Lock()
cache.list = []string{} i.list = []string{}
cache.mutex.Unlock() i.mutex.Unlock()
fileList(args, &filters{}, "", cache, formats) fileList(args, &Filters{}, "", i, formats)
if Cache && CacheFile != "" { if Cache && CacheFile != "" {
cache.Export(CacheFile) i.Export(CacheFile)
} }
} }
func (cache *fileCache) isEmpty() bool { func (i *FileIndex) IsEmpty() bool {
cache.mutex.RLock() i.mutex.RLock()
length := len(cache.list) length := len(i.list)
cache.mutex.RUnlock() i.mutex.RUnlock()
return length == 0 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) 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,14 +93,16 @@ func (cache *fileCache) Export(path string) error {
enc := gob.NewEncoder(z) enc := gob.NewEncoder(z)
cache.mutex.RLock() i.mutex.RLock()
enc.Encode(&cache.list)
cache.mutex.RUnlock() enc.Encode(&i.list)
i.mutex.RUnlock()
return nil 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) file, err := os.OpenFile(path, os.O_RDONLY, 0600)
if err != nil { if err != nil {
return err return err
@ -115,11 +117,11 @@ func (cache *fileCache) Import(path string) error {
dec := gob.NewDecoder(z) 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 { if err != nil {
return err return err
@ -128,29 +130,12 @@ func (cache *fileCache) Import(path string) error {
return nil 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) { 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.Header().Set("Content-Type", "text/plain")
w.Write([]byte("Ok")) 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 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, path string) error { func notFound(w http.ResponseWriter, r *http.Request, filePath 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),
path, filePath,
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 splitPath struct { type Path struct {
base string base string
number int number int
extension string extension string
} }
func (splitPath *splitPath) increment() { func (p *Path) increment() {
splitPath.number = splitPath.number + 1 p.number = p.number + 1
} }
func (splitPath *splitPath) decrement() { func (p *Path) decrement() {
splitPath.number = splitPath.number - 1 p.number = p.number - 1
} }
func contains(slice []string, value string) bool { func contains(s []string, e string) bool {
for _, v := range slice { for _, a := range s {
if v == value { if a == e {
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 := formats.FileType(path) registered, _, _, err := types.FileType(path, formats)
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.excluded); i++ { for i := 0; i < len(filters.excludes); i++ {
if strings.Contains( if strings.Contains(
filename, filename,
filters.excluded[i], filters.excludes[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.included); i++ { for i := 0; i < len(filters.includes); i++ {
if strings.Contains( if strings.Contains(
filename, filename,
filters.included[i], filters.includes[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 *fileCache, formats *types.Types) (string, error) { func newFile(paths []string, filters *Filters, sortOrder string, Regexes *Regexes, index *FileIndex, formats *types.Types) (string, error) {
path, err := pickFile(paths, filters, sortOrder, index, formats) filePath, err := pickFile(paths, filters, sortOrder, index, formats)
if err != nil { if err != nil {
return "", nil return "", nil
} }
splitPath, err := split(path, regexes) path, err := splitPath(filePath, Regexes)
if err != nil { if err != nil {
return "", err return "", err
} }
splitPath.number = 1 path.number = 1
switch { switch {
case sortOrder == "asc": case sortOrder == "asc":
path, err = tryExtensions(splitPath, formats) filePath, err = tryExtensions(path, formats)
if err != nil { if err != nil {
return "", err return "", err
} }
case sortOrder == "desc": case sortOrder == "desc":
for { for {
splitPath.increment() path.increment()
path, err = tryExtensions(splitPath, formats) filePath, err = tryExtensions(path, formats)
if err != nil { if err != nil {
return "", err return "", err
} }
if path == "" { if filePath == "" {
splitPath.decrement() path.decrement()
path, err = tryExtensions(splitPath, formats) filePath, err = tryExtensions(path, 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 path, nil return filePath, nil
} }
func nextFile(path, sortOrder string, regexes *regexes, formats *types.Types) (string, error) { func nextFile(filePath, sortOrder string, Regexes *Regexes, formats *types.Types) (string, error) {
splitPath, err := split(path, regexes) path, err := splitPath(filePath, Regexes)
if err != nil { if err != nil {
return "", err return "", err
} }
switch { switch {
case sortOrder == "asc": case sortOrder == "asc":
splitPath.increment() path.increment()
case sortOrder == "desc": case sortOrder == "desc":
splitPath.decrement() path.decrement()
default: default:
return "", nil return "", nil
} }
fileName, err := tryExtensions(splitPath, formats) fileName, err := tryExtensions(path, formats)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -247,21 +247,21 @@ func nextFile(path, sortOrder string, regexes *regexes, formats *types.Types) (s
return fileName, err return fileName, err
} }
func split(path string, regexes *regexes) (*splitPath, error) { func splitPath(path string, Regexes *Regexes) (*Path, error) {
p := splitPath{} p := Path{}
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 &splitPath{}, nil return &Path{}, 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 &splitPath{}, err return &Path{}, err
} }
p.extension = split[0][3] p.extension = split[0][3]
@ -269,11 +269,11 @@ func split(path string, regexes *regexes) (*splitPath, error) {
return &p, nil return &p, nil
} }
func tryExtensions(splitPath *splitPath, formats *types.Types) (string, error) { func tryExtensions(p *Path, 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", splitPath.base, splitPath.number, extension) fileName = fmt.Sprintf("%s%.3d%s", p.base, p.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(path string, paths []string) bool { func pathIsValid(filePath 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(path, paths[i]) { if strings.HasPrefix(filePath, paths[i]) {
matchesPrefix = true matchesPrefix = true
} }
} }
@ -312,8 +312,8 @@ func pathIsValid(path 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),
path, filePath,
) )
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 := formats.FileType(p) registered, _, _, err := types.FileType(p, formats)
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, cache *fileCache, formats *types.Types) ([]string, bool) { func fileList(paths []string, filters *Filters, sort string, index *FileIndex, formats *types.Types) ([]string, bool) {
if Cache && filters.isEmpty() && !cache.isEmpty() { if Cache && filters.IsEmpty() && !index.IsEmpty() {
return cache.List(), true return index.Index(), 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, cache *fileCache, 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, cache *fileCache, f
) )
} }
if Cache && filters.isEmpty() { if Cache && filters.IsEmpty() {
cache.set(fileList) index.setIndex(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, cache *fileCache, formats *types.Types) (string, error) { func pickFile(args []string, filters *Filters, sort string, index *FileIndex, formats *types.Types) (string, error) {
fileList, fromCache := fileList(args, filters, sort, cache, formats) fileList, fromCache := fileList(args, filters, sort, index, formats)
fileCount := len(fileList) fileCount := len(fileList)
if fileCount < 1 { if fileCount < 1 {
@ -581,40 +581,29 @@ func pickFile(args []string, filters *filters, sort string, cache *fileCache, fo
val++ val++
} }
path := fileList[val] filePath := fileList[val]
if !fromCache { if !fromCache {
registered, _, _, err := formats.FileType(path) registered, _, _, err := types.FileType(filePath, formats)
if err != nil { if err != nil {
return "", err return "", err
} }
if registered { if registered {
return path, nil return filePath, nil
} }
continue continue
} }
return path, nil return filePath, nil
} }
return "", ErrNoMediaFound return "", ErrNoMediaFound
} }
func normalizePath(path string) (string, error) { func normalizePath(path string) (string, error) {
homeDir, err := os.UserHomeDir() path, err := filepath.EvalSymlinks(path)
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)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

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

View File

@ -19,13 +19,13 @@ import (
"seedno.de/seednode/roulette/types" "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) { 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 := cache.List() indexDump := index.Index()
fileCount := len(indexDump) fileCount := len(indexDump)
@ -54,7 +54,7 @@ func serveIndexHtml(args []string, cache *fileCache, 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, cache *fileCache, 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, cache *fileCache, 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, 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) { 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()
cachedFiles := index.List() indexDump := index.Index()
fileCount := len(cachedFiles) fileCount := len(indexDump)
sort.SliceStable(cachedFiles, func(p, q int) bool { sort.SliceStable(indexDump, func(p, q int) bool {
return strings.ToLower(cachedFiles[p]) < strings.ToLower(cachedFiles[q]) return strings.ToLower(indexDump[p]) < strings.ToLower(indexDump[q])
}) })
var startIndex, stopIndex int var startIndex, stopIndex int
@ -159,14 +159,14 @@ func serveIndexJson(args []string, index *fileCache) httprouter.Handle {
} }
if startIndex > (fileCount - 1) { if startIndex > (fileCount - 1) {
cachedFiles = []string{} indexDump = []string{}
} }
if stopIndex > fileCount { if stopIndex > fileCount {
stopIndex = fileCount stopIndex = fileCount
} }
response, err := json.MarshalIndent(cachedFiles[startIndex:stopIndex], "", " ") response, err := json.MarshalIndent(indexDump[startIndex:stopIndex], "", " ")
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -179,7 +179,7 @@ func serveIndexJson(args []string, index *fileCache) 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),
@ -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 ( const (
ReleaseVersion string = "0.78.2" ReleaseVersion string = "0.76.0"
) )
var ( var (

View File

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

@ -22,6 +22,8 @@ import (
"syscall" "syscall"
"time" "time"
"net/http/pprof"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
"github.com/yosssi/gohtml" "github.com/yosssi/gohtml"
"seedno.de/seednode/roulette/types" "seedno.de/seednode/roulette/types"
@ -33,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, 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) { 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 {
@ -53,7 +55,7 @@ func serveStaticFile(paths []string, stats *serveStats, cache *fileCache) 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)
@ -109,13 +111,13 @@ func serveStaticFile(paths []string, stats *serveStats, cache *fileCache) httpro
} }
if Cache { if Cache {
cache.remove(filePath) index.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),
@ -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) { 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 {
@ -141,21 +143,21 @@ func serveRoot(paths []string, regexes *regexes, cache *fileCache, formats *type
return return
} }
strippedRefererUri := strings.TrimPrefix(refererUri, mediaPrefix) strippedRefererUri := strings.TrimPrefix(refererUri, MediaPrefix)
filters := &filters{ filters := &Filters{
included: splitQueryParams(r.URL.Query().Get("include"), regexes), includes: splitQueryParams(r.URL.Query().Get("include"), Regexes),
excluded: splitQueryParams(r.URL.Query().Get("exclude"), regexes), excludes: 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)
@ -166,7 +168,7 @@ func serveRoot(paths []string, regexes *regexes, cache *fileCache, formats *type
} }
loop: loop:
for timeout := time.After(timeout); ; { for timeout := time.After(Timeout); ; {
select { select {
case <-timeout: case <-timeout:
break loop break loop
@ -177,7 +179,7 @@ func serveRoot(paths []string, regexes *regexes, cache *fileCache, formats *type
break loop break loop
} }
filePath, err = newFile(paths, filters, sortOrder, regexes, cache, formats) filePath, err = newFile(paths, filters, sortOrder, Regexes, index, formats)
switch { switch {
case err != nil && err == ErrNoMediaFound: case err != nil && err == ErrNoMediaFound:
notFound(w, r, filePath) notFound(w, r, filePath)
@ -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) { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
filters := &filters{ filters := &Filters{
included: splitQueryParams(r.URL.Query().Get("include"), regexes), includes: splitQueryParams(r.URL.Query().Get("include"), Regexes),
excluded: splitQueryParams(r.URL.Query().Get("exclude"), 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" { if runtime.GOOS == "windows" {
path = strings.TrimPrefix(path, "/") filePath = strings.TrimPrefix(filePath, "/")
} }
exists, err := fileExists(path) exists, err := fileExists(filePath)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -227,12 +229,12 @@ func serveMedia(paths []string, regexes *regexes, formats *types.Types) httprout
return return
} }
if !exists { if !exists {
notFound(w, r, path) notFound(w, r, filePath)
return return
} }
registered, fileType, mimeType, err := formats.FileType(path) registered, fileType, mimeType, err := types.FileType(filePath, formats)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -242,14 +244,14 @@ func serveMedia(paths []string, regexes *regexes, formats *types.Types) httprout
} }
if !registered { if !registered {
notFound(w, r, path) notFound(w, r, filePath)
return return
} }
fileUri := generateFileUri(path) fileUri := generateFileUri(filePath)
fileName := filepath.Base(path) fileName := filepath.Base(filePath)
w.Header().Add("Content-Type", "text/html") 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 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, path, fileName, mimeType))) htmlBody.WriteString((fileType.Title(queryParams, fileUri, filePath, fileName, mimeType)))
htmlBody.WriteString(`</head><body>`) htmlBody.WriteString(`</head><body>`)
if refreshInterval != "0ms" { if refreshInterval != "0ms" {
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, path, fileName, mimeType))) htmlBody.WriteString((fileType.Body(queryParams, fileUri, filePath, 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()))
@ -312,31 +314,33 @@ func ServePage(args []string) error {
return errors.New("invalid bind address provided") return errors.New("invalid bind address provided")
} }
mux := httprouter.New()
formats := &types.Types{ formats := &types.Types{
Extensions: make(map[string]string), Extensions: make(map[string]string),
MimeTypes: make(map[string]types.Type), MimeTypes: make(map[string]types.Type),
} }
if Audio || All { if Audio || All {
formats.Add(audio.New()) formats.Add(audio.Format{})
} }
if Flash || All { if Flash || All {
formats.Add(flash.New()) formats.Add(flash.Format{})
} }
if Text || All { if Text || All {
formats.Add(text.New()) formats.Add(text.Format{})
} }
if Videos || All { if Videos || All {
formats.Add(video.New()) formats.Add(video.Format{})
} }
// 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.New()) formats.Add(images.Format{})
} }
paths, err := normalizePaths(args, formats) paths, err := normalizePaths(args, formats)
@ -348,18 +352,20 @@ func ServePage(args []string) error {
return ErrNoMediaFound return ErrNoMediaFound
} }
cache := &fileCache{ if Russian {
fmt.Printf("WARNING! Files *will* be deleted after serving!\n\n")
}
index := &FileIndex{
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]*$`),
} }
mux := httprouter.New()
srv := &http.Server{ srv := &http.Server{
Addr: net.JoinHostPort(Bind, strconv.Itoa(int(Port))), Addr: net.JoinHostPort(Bind, strconv.Itoa(int(Port))),
Handler: mux, Handler: mux,
@ -368,7 +374,7 @@ func ServePage(args []string) error {
WriteTimeout: 5 * time.Minute, WriteTimeout: 5 * time.Minute,
} }
stats := &serveStats{ stats := &ServeStats{
mutex: sync.RWMutex{}, mutex: sync.RWMutex{},
list: []string{}, list: []string{},
count: make(map[string]uint32), count: make(map[string]uint32),
@ -378,45 +384,65 @@ func ServePage(args []string) error {
mux.PanicHandler = serverErrorHandler() mux.PanicHandler = serverErrorHandler()
mux.GET("/", serveRoot(paths, regexes, cache, formats)) mux.GET("/", serveRoot(paths, regexes, index, formats))
mux.GET("/favicons/*favicon", serveFavicons()) mux.GET("/favicons/*favicon", serveFavicons())
mux.GET("/favicon.ico", serveFavicons()) mux.GET("/favicon.ico", serveFavicons())
mux.GET(mediaPrefix+"/*media", serveMedia(paths, regexes, 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()) mux.GET("/version", serveVersion())
if Cache { 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 { 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 { if Profile {
registerProfileHandlers(mux) mux.HandlerFunc("GET", "/debug/pprof/", pprof.Index)
} mux.HandlerFunc("GET", "/debug/pprof/cmdline", pprof.Cmdline)
mux.HandlerFunc("GET", "/debug/pprof/profile", pprof.Profile)
if Russian { mux.HandlerFunc("GET", "/debug/pprof/symbol", pprof.Symbol)
fmt.Printf("WARNING! Files *will* be deleted after serving!\n\n") mux.HandlerFunc("GET", "/debug/pprof/trace", pprof.Trace)
} }
if Statistics { if Statistics {
if StatisticsFile != "" { if StatisticsFile != "" {
StatisticsFile, err = normalizePath(StatisticsFile) stats.Import(StatisticsFile)
if err != nil {
return err
}
err := stats.importFile(StatisticsFile)
if err != nil {
return err
}
gracefulShutdown := make(chan os.Signal, 1) gracefulShutdown := make(chan os.Signal, 1)
signal.Notify(gracefulShutdown, syscall.SIGINT, syscall.SIGTERM) signal.Notify(gracefulShutdown, syscall.SIGINT, syscall.SIGTERM)
@ -424,13 +450,16 @@ func ServePage(args []string) error {
go func() { go func() {
<-gracefulShutdown <-gracefulShutdown
stats.exportFile(StatisticsFile) stats.Export(StatisticsFile)
os.Exit(0) 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() err = srv.ListenAndServe()

View File

@ -57,10 +57,8 @@ func (t Format) Validate(filePath string) bool {
return true return true
} }
func New() Format {
return Format{}
}
func init() { 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 return true
} }
func New() Format {
return Format{}
}
func init() { 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 return &dimensions{width: decodedConfig.Width, height: decodedConfig.Height}, nil
} }
func New() Format {
return Format{}
}
func init() { 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) return utf8.Valid(head)
} }
func New() Format {
return Format{}
}
func init() { func init() {
types.SupportedFormats.Register(New()) format := Format{}
types.Register(format)
} }

View File

@ -32,23 +32,23 @@ type Types struct {
MimeTypes map[string]Type MimeTypes map[string]Type
} }
func (t *Types) Add(format Type) { func (s *Types) Add(t Type) {
for k, v := range format.Extensions() { for k, v := range t.Extensions() {
_, exists := t.Extensions[k] _, exists := s.Extensions[k]
if !exists { if !exists {
t.Extensions[k] = v s.Extensions[k] = v
} }
} }
for _, v := range format.MimeTypes() { for _, v := range t.MimeTypes() {
_, exists := t.Extensions[v] _, exists := s.Extensions[v]
if !exists { 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) file, err := os.Open(path)
switch { switch {
case errors.Is(err, os.ErrNotExist): case errors.Is(err, os.ErrNotExist):
@ -64,15 +64,15 @@ func (t *Types) FileType(path string) (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 := t.MimeTypes[mimeType] fileType, exists := registeredFormats.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 = t.Extensions[filepath.Ext(path)] mimeType, exists = registeredFormats.Extensions[filepath.Ext(path)]
if exists { if exists {
fileType, exists := t.MimeTypes[mimeType] fileType, exists := registeredFormats.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 (t *Types) FileType(path string) (bool, Type, string, error) {
return false, nil, "", nil return false, nil, "", nil
} }
func (t *Types) Register(format Type) { func Register(t Type) {
t.Add(format) SupportedFormats.Add(t)
} }
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,10 +57,8 @@ func (t Format) Validate(filePath string) bool {
return true return true
} }
func New() Format {
return Format{}
}
func init() { func init() {
types.SupportedFormats.Register(New()) format := Format{}
types.Register(format)
} }