Replace single-value registered format struct with the map value itself

This commit is contained in:
Seednode 2023-10-04 14:09:49 -05:00
parent 5dc218c60e
commit 84c25310da
6 changed files with 39 additions and 45 deletions

View File

@ -72,7 +72,7 @@ func kill(path string, index *fileIndex) error {
return nil return nil
} }
func newFile(list []string, sortOrder string, regexes *regexes, formats *types.Types) (string, error) { func newFile(list []string, sortOrder string, regexes *regexes, formats types.Types) (string, error) {
path, err := pickFile(list) path, err := pickFile(list)
if err != nil { if err != nil {
return "", err return "", err
@ -118,7 +118,7 @@ func newFile(list []string, sortOrder string, regexes *regexes, formats *types.T
return path, nil return path, nil
} }
func nextFile(filePath, sortOrder string, regexes *regexes, formats *types.Types) (string, error) { func nextFile(filePath, sortOrder string, regexes *regexes, formats types.Types) (string, error) {
splitPath, _, err := split(filePath, regexes) splitPath, _, err := split(filePath, regexes)
if err != nil { if err != nil {
return "", err return "", err
@ -141,10 +141,10 @@ func nextFile(filePath, sortOrder string, regexes *regexes, formats *types.Types
return path, err return path, err
} }
func tryExtensions(splitPath *splitPath, formats *types.Types) (string, error) { func tryExtensions(splitPath *splitPath, formats types.Types) (string, error) {
var path string var path string
for extension := range formats.Extensions { for extension := range formats {
path = fmt.Sprintf("%s%s%s", splitPath.base, splitPath.number, extension) path = fmt.Sprintf("%s%s%s", splitPath.base, splitPath.number, extension)
exists, err := fileExists(path) exists, err := fileExists(path)
@ -198,7 +198,7 @@ func pathIsValid(path string, paths []string) bool {
} }
} }
func hasSupportedFiles(path string, formats *types.Types) (bool, error) { func hasSupportedFiles(path string, formats types.Types) (bool, error) {
hasRegisteredFiles := make(chan bool, 1) hasRegisteredFiles := make(chan bool, 1)
err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error { err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error {
@ -229,7 +229,7 @@ func hasSupportedFiles(path string, formats *types.Types) (bool, error) {
} }
} }
func walkPath(path string, fileChannel chan<- string, stats *scanStatsChannels, formats *types.Types) error { func walkPath(path string, fileChannel chan<- string, stats *scanStatsChannels, formats types.Types) error {
errorChannel := make(chan error) errorChannel := make(chan error)
done := make(chan bool, 1) done := make(chan bool, 1)
@ -321,7 +321,7 @@ Poll:
return nil return nil
} }
func scanPaths(paths []string, sort string, index *fileIndex, formats *types.Types) ([]string, error) { func scanPaths(paths []string, sort string, index *fileIndex, formats types.Types) ([]string, error) {
var list []string var list []string
fileChannel := make(chan string) fileChannel := make(chan string)
@ -375,13 +375,13 @@ Poll:
case path := <-fileChannel: case path := <-fileChannel:
list = append(list, path) list = append(list, path)
case stat := <-statsChannels.filesMatched: case stat := <-statsChannels.filesMatched:
stats.filesMatched = stats.filesMatched + stat stats.filesMatched += stat
case stat := <-statsChannels.filesSkipped: case stat := <-statsChannels.filesSkipped:
stats.filesSkipped = stats.filesSkipped + stat stats.filesSkipped += stat
case stat := <-statsChannels.directoriesMatched: case stat := <-statsChannels.directoriesMatched:
stats.directoriesMatched = stats.directoriesMatched + stat stats.directoriesMatched += stat
case stat := <-statsChannels.directoriesSkipped: case stat := <-statsChannels.directoriesSkipped:
stats.directoriesSkipped = stats.directoriesSkipped + stat stats.directoriesSkipped += stat
case err := <-errorChannel: case err := <-errorChannel:
return []string{}, err return []string{}, err
case <-done: case <-done:
@ -409,7 +409,7 @@ Poll:
return list, nil return list, nil
} }
func fileList(paths []string, filters *filters, sort string, index *fileIndex, formats *types.Types) ([]string, error) { func fileList(paths []string, filters *filters, sort string, index *fileIndex, formats types.Types) ([]string, error) {
switch { switch {
case Index && !index.isEmpty() && filters.isEmpty(): case Index && !index.isEmpty() && filters.isEmpty():
return index.List(), nil return index.List(), nil
@ -493,7 +493,7 @@ func normalizePath(path string) (string, error) {
return absolutePath, nil return absolutePath, nil
} }
func validatePaths(args []string, formats *types.Types) ([]string, error) { func validatePaths(args []string, formats types.Types) ([]string, error) {
var paths []string var paths []string
for i := 0; i < len(args); i++ { for i := 0; i < len(args); i++ {

View File

@ -158,7 +158,7 @@ func (index *fileIndex) Import(path string) error {
return nil return nil
} }
func serveIndexRebuild(args []string, index *fileIndex, formats *types.Types, errorChannel chan<- error) httprouter.Handle { func serveIndexRebuild(args []string, index *fileIndex, formats types.Types, errorChannel chan<- error) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
index.clear() index.clear()
@ -175,13 +175,13 @@ func serveIndexRebuild(args []string, index *fileIndex, formats *types.Types, er
} }
} }
func registerIndexHandlers(mux *httprouter.Router, args []string, index *fileIndex, formats *types.Types, errorChannel chan<- error) error { func registerIndexHandlers(mux *httprouter.Router, args []string, index *fileIndex, formats types.Types, errorChannel chan<- error) error {
registerHandler(mux, Prefix+"/rebuild_index", serveIndexRebuild(args, index, formats, errorChannel)) registerHandler(mux, Prefix+"/rebuild_index", serveIndexRebuild(args, index, formats, errorChannel))
return nil return nil
} }
func importIndex(args []string, index *fileIndex, formats *types.Types) error { func importIndex(args []string, index *fileIndex, formats types.Types) error {
skipIndex := false skipIndex := false
if IndexFile != "" { if IndexFile != "" {

View File

@ -229,7 +229,7 @@ func serveAvailableExtensions() httprouter.Handle {
} }
} }
func serveEnabledExtensions(formats *types.Types) httprouter.Handle { func serveEnabledExtensions(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) {
w.Header().Set("Content-Type", "text/plain") w.Header().Set("Content-Type", "text/plain")
@ -271,7 +271,7 @@ func serveAvailableMimeTypes() httprouter.Handle {
} }
} }
func serveEnabledMimeTypes(formats *types.Types) httprouter.Handle { func serveEnabledMimeTypes(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) {
w.Header().Set("Content-Type", "text/plain") w.Header().Set("Content-Type", "text/plain")
@ -292,7 +292,7 @@ func serveEnabledMimeTypes(formats *types.Types) httprouter.Handle {
} }
} }
func registerInfoHandlers(mux *httprouter.Router, args []string, index *fileIndex, formats *types.Types, errorChannel chan<- error) { func registerInfoHandlers(mux *httprouter.Router, args []string, index *fileIndex, formats types.Types, errorChannel chan<- error) {
if Index { if Index {
registerHandler(mux, Prefix+"/html", serveIndexHtml(args, index, false)) registerHandler(mux, Prefix+"/html", serveIndexHtml(args, index, false))
if PageLength != 0 { if PageLength != 0 {

View File

@ -12,7 +12,7 @@ import (
) )
const ( const (
ReleaseVersion string = "2.4.0" ReleaseVersion string = "2.5.0"
) )
var ( var (

View File

@ -153,7 +153,7 @@ func serveStaticFile(paths []string, index *fileIndex, errorChannel chan<- error
} }
} }
func serveRoot(paths []string, regexes *regexes, index *fileIndex, formats *types.Types, errorChannel chan<- error) httprouter.Handle { func serveRoot(paths []string, regexes *regexes, index *fileIndex, formats types.Types, errorChannel chan<- error) 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 {
@ -236,7 +236,7 @@ func serveRoot(paths []string, regexes *regexes, index *fileIndex, formats *type
} }
} }
func serveMedia(paths []string, regexes *regexes, index *fileIndex, formats *types.Types, errorChannel chan<- error) httprouter.Handle { func serveMedia(paths []string, regexes *regexes, index *fileIndex, formats types.Types, errorChannel chan<- error) 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), included: splitQueryParams(r.URL.Query().Get("include"), regexes),
@ -433,9 +433,7 @@ func ServePage(args []string) error {
return errors.New("invalid bind address provided") return errors.New("invalid bind address provided")
} }
formats := &types.Types{ formats := make(types.Types)
Extensions: make(map[string]types.Type),
}
if Audio || All { if Audio || All {
formats.Add(audio.Format{}) formats.Add(audio.Format{})
@ -459,7 +457,7 @@ func ServePage(args []string) error {
// enable image support if no other flags are passed, to retain backwards compatibility // enable image support if no other flags are passed, to retain backwards compatibility
// to be replaced with 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) == 0 {
formats.Add(images.Format{}) formats.Add(images.Format{})
} }

View File

@ -10,9 +10,7 @@ import (
"strings" "strings"
) )
var SupportedFormats = &Types{ var SupportedFormats = make(Types)
Extensions: make(map[string]Type),
}
type Type interface { type Type interface {
Type() string Type() string
@ -24,21 +22,19 @@ type Type interface {
Validate(filePath string) bool Validate(filePath string) bool
} }
type Types struct { type Types map[string]Type
Extensions map[string]Type
}
func (t *Types) Add(format Type) { func (t Types) Add(format Type) {
for k := range format.Extensions() { for k := range format.Extensions() {
_, exists := t.Extensions[k] _, exists := t[k]
if !exists { if !exists {
t.Extensions[k] = format t[k] = format
} }
} }
} }
func (t *Types) FileType(path string) Type { func (t Types) FileType(path string) Type {
fileType, exists := t.Extensions[filepath.Ext(path)] fileType, exists := t[filepath.Ext(path)]
if exists { if exists {
return fileType return fileType
} }
@ -46,12 +42,12 @@ func (t *Types) FileType(path string) Type {
return nil return nil
} }
func (t *Types) Register(format Type) { func (t Types) Register(format Type) {
t.Add(format) t.Add(format)
} }
func (t *Types) Validate(path string) bool { func (t Types) Validate(path string) bool {
format, exists := t.Extensions[filepath.Ext(path)] format, exists := t[filepath.Ext(path)]
if !exists { if !exists {
return false return false
} }
@ -59,14 +55,14 @@ func (t *Types) Validate(path string) bool {
return format.Validate(path) return format.Validate(path)
} }
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))
i := 0 i := 0
for k := range t.Extensions { for k := range t {
extensions[i] = k extensions[i] = k
i++ i++
} }
@ -80,12 +76,12 @@ 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
var mimeTypes []string var mimeTypes []string
for _, j := range t.Extensions { for _, j := range t {
extensions := j.Extensions() extensions := j.Extensions()
for _, v := range extensions { for _, v := range extensions {
mimeTypes = append(mimeTypes, v) mimeTypes = append(mimeTypes, v)