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
}
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)
if err != nil {
return "", err
@ -118,7 +118,7 @@ func newFile(list []string, sortOrder string, regexes *regexes, formats *types.T
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)
if err != nil {
return "", err
@ -141,10 +141,10 @@ func nextFile(filePath, sortOrder string, regexes *regexes, formats *types.Types
return path, err
}
func tryExtensions(splitPath *splitPath, formats *types.Types) (string, error) {
func tryExtensions(splitPath *splitPath, formats types.Types) (string, error) {
var path string
for extension := range formats.Extensions {
for extension := range formats {
path = fmt.Sprintf("%s%s%s", splitPath.base, splitPath.number, extension)
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)
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)
done := make(chan bool, 1)
@ -321,7 +321,7 @@ Poll:
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
fileChannel := make(chan string)
@ -375,13 +375,13 @@ Poll:
case path := <-fileChannel:
list = append(list, path)
case stat := <-statsChannels.filesMatched:
stats.filesMatched = stats.filesMatched + stat
stats.filesMatched += stat
case stat := <-statsChannels.filesSkipped:
stats.filesSkipped = stats.filesSkipped + stat
stats.filesSkipped += stat
case stat := <-statsChannels.directoriesMatched:
stats.directoriesMatched = stats.directoriesMatched + stat
stats.directoriesMatched += stat
case stat := <-statsChannels.directoriesSkipped:
stats.directoriesSkipped = stats.directoriesSkipped + stat
stats.directoriesSkipped += stat
case err := <-errorChannel:
return []string{}, err
case <-done:
@ -409,7 +409,7 @@ Poll:
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 {
case Index && !index.isEmpty() && filters.isEmpty():
return index.List(), nil
@ -493,7 +493,7 @@ func normalizePath(path string) (string, error) {
return absolutePath, nil
}
func validatePaths(args []string, formats *types.Types) ([]string, error) {
func validatePaths(args []string, formats types.Types) ([]string, error) {
var paths []string
for i := 0; i < len(args); i++ {

View File

@ -158,7 +158,7 @@ func (index *fileIndex) Import(path string) error {
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) {
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))
return nil
}
func importIndex(args []string, index *fileIndex, formats *types.Types) error {
func importIndex(args []string, index *fileIndex, formats types.Types) error {
skipIndex := false
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) {
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) {
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 {
registerHandler(mux, Prefix+"/html", serveIndexHtml(args, index, false))
if PageLength != 0 {

View File

@ -12,7 +12,7 @@ import (
)
const (
ReleaseVersion string = "2.4.0"
ReleaseVersion string = "2.5.0"
)
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) {
refererUri, err := stripQueryParams(refererToUri(r.Referer()))
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) {
filters := &filters{
included: splitQueryParams(r.URL.Query().Get("include"), regexes),
@ -433,9 +433,7 @@ func ServePage(args []string) error {
return errors.New("invalid bind address provided")
}
formats := &types.Types{
Extensions: make(map[string]types.Type),
}
formats := make(types.Types)
if Audio || All {
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
// 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{})
}

View File

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