From a7daf817540884396001e6a350467766e9443cbb Mon Sep 17 00:00:00 2001 From: Seednode Date: Tue, 12 Sep 2023 19:46:27 -0500 Subject: [PATCH] Replace []string with map[string]string for extensions, to allow setting default MIME type values --- cmd/files.go | 6 ++---- cmd/root.go | 2 +- cmd/web.go | 2 +- types/audio.go | 14 ++++++++------ types/flash.go | 4 ++-- types/images.go | 26 +++++++++++++------------- types/text.go | 18 +++++++++--------- types/types.go | 18 +++++++++++------- types/video.go | 10 +++++----- 9 files changed, 52 insertions(+), 48 deletions(-) diff --git a/cmd/files.go b/cmd/files.go index ab884b6..bc2a6af 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -272,10 +272,8 @@ func splitPath(path string, Regexes *Regexes) (*Path, error) { func tryExtensions(p *Path, formats *types.Types) (string, error) { var fileName string - for _, format := range formats.Extensions { - for _, extension := range format.Extensions { - fileName = fmt.Sprintf("%s%.3d%s", p.base, p.number, extension) - } + for extension := range formats.Extensions { + fileName = fmt.Sprintf("%s%.3d%s", p.base, p.number, extension) exists, err := fileExists(fileName) if err != nil { diff --git a/cmd/root.go b/cmd/root.go index 3979131..3b732f4 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -12,7 +12,7 @@ import ( ) const ( - ReleaseVersion string = "0.72.2" + ReleaseVersion string = "0.73.0" ) var ( diff --git a/cmd/web.go b/cmd/web.go index 2ebc1b7..93ca041 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -312,7 +312,7 @@ func ServePage(args []string) error { mux := httprouter.New() formats := &types.Types{ - Extensions: make(map[string]*types.Type), + Extensions: make(map[string]string), MimeTypes: make(map[string]*types.Type), } diff --git a/types/audio.go b/types/audio.go index 3b34677..bfe6671 100644 --- a/types/audio.go +++ b/types/audio.go @@ -29,16 +29,18 @@ func RegisterAudio() *Type { mime, fileName) }, - Extensions: []string{ - `.mp3`, - `.ogg`, - `.oga`, - `.wav`, + Extensions: map[string]string{ + `.mp3`: `audio/mpeg`, + `.ogg`: `audio/ogg`, + `.oga`: `audio/ogg`, }, MimeTypes: []string{ + `application/ogg`, + `audio/mp3`, `audio/mpeg`, + `audio/mpeg3`, `audio/ogg`, - `audio/wav`, + `audio/x-mpeg-3`, }, Validate: func(filePath string) bool { return true diff --git a/types/flash.go b/types/flash.go index 6492b07..ca38ecc 100644 --- a/types/flash.go +++ b/types/flash.go @@ -30,8 +30,8 @@ func RegisterFlash() *Type { return html.String() }, - Extensions: []string{ - `.swf`, + Extensions: map[string]string{ + `.swf`: `application/x-shockwave-flash`, }, MimeTypes: []string{ `application/x-shockwave-flash`, diff --git a/types/images.go b/types/images.go index bea8af1..9f75870 100644 --- a/types/images.go +++ b/types/images.go @@ -60,19 +60,19 @@ func RegisterImages() *Type { mime, fileName) }, - Extensions: []string{ - `.apng`, - `.avif`, - `.bmp`, - `.gif`, - `.jpg`, - `.jpeg`, - `.jfif`, - `.pjp`, - `.pjpeg`, - `.png`, - `.svg`, - `.webp`, + Extensions: map[string]string{ + `.apng`: `image/apng`, + `.avif`: `image/avif`, + `.bmp`: `image/bmp`, + `.gif`: `image/gif`, + `.jpg`: `image/jpeg`, + `.jpeg`: `image/jpeg`, + `.jfif`: `image/jpeg`, + `.pjp`: `image/jpeg`, + `.pjpeg`: `image/jpeg`, + `.png`: `image/png`, + `.svg`: `image/svg+xml`, + `.webp`: `image/webp`, }, MimeTypes: []string{ `image/apng`, diff --git a/types/text.go b/types/text.go index 6a9c77a..82b993b 100644 --- a/types/text.go +++ b/types/text.go @@ -36,15 +36,15 @@ func RegisterText() *Type { queryParams, body) }, - Extensions: []string{ - `.css`, - `.csv`, - `.html`, - `.js`, - `.json`, - `.md`, - `.txt`, - `.xml`, + Extensions: map[string]string{ + `.css`: `text/css`, + `.csv`: `text/csv`, + `.html`: `text/html`, + `.js`: `text/javascript`, + `.json`: `application/json`, + `.md`: `text/markdown`, + `.txt`: `text/plain`, + `.xml`: `application/xml`, }, MimeTypes: []string{ `application/json`, diff --git a/types/types.go b/types/types.go index 33c3c3b..bad02b4 100644 --- a/types/types.go +++ b/types/types.go @@ -15,21 +15,21 @@ type Type struct { Css func() string Title func(queryParams, fileUri, filePath, fileName, mime string) string Body func(queryParams, fileUri, filePath, fileName, mime string) string - Extensions []string + Extensions map[string]string MimeTypes []string Validate func(filePath string) bool } type Types struct { - Extensions map[string]*Type + Extensions map[string]string MimeTypes map[string]*Type } func (s *Types) Add(t *Type) { - for _, v := range t.Extensions { - _, exists := s.Extensions[v] + for k, v := range t.Extensions { + _, exists := s.Extensions[k] if !exists { - s.Extensions[v] = t + s.Extensions[k] = v } } @@ -63,9 +63,13 @@ func FileType(path string, registeredFormats *Types) (bool, *Type, string, error } // if mime type detection fails, use the file extension - fileType, exists = registeredFormats.Extensions[filepath.Ext(path)] + mimeType, exists = registeredFormats.Extensions[filepath.Ext(path)] if exists { - return fileType.Validate(path), fileType, mimeType, nil + fileType, exists := registeredFormats.MimeTypes[mimeType] + + if exists { + return fileType.Validate(path), fileType, mimeType, nil + } } return false, nil, "", nil diff --git a/types/video.go b/types/video.go index cdaaffc..c27c155 100644 --- a/types/video.go +++ b/types/video.go @@ -31,11 +31,11 @@ func RegisterVideos() *Type { mime, fileName) }, - Extensions: []string{ - `.mp4`, - `.ogm`, - `.ogv`, - `.webm`, + Extensions: map[string]string{ + `.mp4`: `video/mp4`, + `.ogm`: `video/ogg`, + `.ogv`: `video/ogg`, + `.webm`: `video/webm`, }, MimeTypes: []string{ `video/mp4`,