Replace []string with map[string]string for extensions, to allow setting default MIME type values

This commit is contained in:
Seednode 2023-09-12 19:46:27 -05:00
parent 57bcd40c29
commit a7daf81754
9 changed files with 52 additions and 48 deletions

View File

@ -272,10 +272,8 @@ func splitPath(path string, Regexes *Regexes) (*Path, error) {
func tryExtensions(p *Path, formats *types.Types) (string, error) { func tryExtensions(p *Path, formats *types.Types) (string, error) {
var fileName string var fileName string
for _, format := range formats.Extensions { for extension := range formats.Extensions {
for _, extension := range format.Extensions {
fileName = fmt.Sprintf("%s%.3d%s", p.base, p.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 {

View File

@ -12,7 +12,7 @@ import (
) )
const ( const (
ReleaseVersion string = "0.72.2" ReleaseVersion string = "0.73.0"
) )
var ( var (

View File

@ -312,7 +312,7 @@ func ServePage(args []string) error {
mux := httprouter.New() mux := httprouter.New()
formats := &types.Types{ formats := &types.Types{
Extensions: make(map[string]*types.Type), Extensions: make(map[string]string),
MimeTypes: make(map[string]*types.Type), MimeTypes: make(map[string]*types.Type),
} }

View File

@ -29,16 +29,18 @@ func RegisterAudio() *Type {
mime, mime,
fileName) fileName)
}, },
Extensions: []string{ Extensions: map[string]string{
`.mp3`, `.mp3`: `audio/mpeg`,
`.ogg`, `.ogg`: `audio/ogg`,
`.oga`, `.oga`: `audio/ogg`,
`.wav`,
}, },
MimeTypes: []string{ MimeTypes: []string{
`application/ogg`,
`audio/mp3`,
`audio/mpeg`, `audio/mpeg`,
`audio/mpeg3`,
`audio/ogg`, `audio/ogg`,
`audio/wav`, `audio/x-mpeg-3`,
}, },
Validate: func(filePath string) bool { Validate: func(filePath string) bool {
return true return true

View File

@ -30,8 +30,8 @@ func RegisterFlash() *Type {
return html.String() return html.String()
}, },
Extensions: []string{ Extensions: map[string]string{
`.swf`, `.swf`: `application/x-shockwave-flash`,
}, },
MimeTypes: []string{ MimeTypes: []string{
`application/x-shockwave-flash`, `application/x-shockwave-flash`,

View File

@ -60,19 +60,19 @@ func RegisterImages() *Type {
mime, mime,
fileName) fileName)
}, },
Extensions: []string{ Extensions: map[string]string{
`.apng`, `.apng`: `image/apng`,
`.avif`, `.avif`: `image/avif`,
`.bmp`, `.bmp`: `image/bmp`,
`.gif`, `.gif`: `image/gif`,
`.jpg`, `.jpg`: `image/jpeg`,
`.jpeg`, `.jpeg`: `image/jpeg`,
`.jfif`, `.jfif`: `image/jpeg`,
`.pjp`, `.pjp`: `image/jpeg`,
`.pjpeg`, `.pjpeg`: `image/jpeg`,
`.png`, `.png`: `image/png`,
`.svg`, `.svg`: `image/svg+xml`,
`.webp`, `.webp`: `image/webp`,
}, },
MimeTypes: []string{ MimeTypes: []string{
`image/apng`, `image/apng`,

View File

@ -36,15 +36,15 @@ func RegisterText() *Type {
queryParams, queryParams,
body) body)
}, },
Extensions: []string{ Extensions: map[string]string{
`.css`, `.css`: `text/css`,
`.csv`, `.csv`: `text/csv`,
`.html`, `.html`: `text/html`,
`.js`, `.js`: `text/javascript`,
`.json`, `.json`: `application/json`,
`.md`, `.md`: `text/markdown`,
`.txt`, `.txt`: `text/plain`,
`.xml`, `.xml`: `application/xml`,
}, },
MimeTypes: []string{ MimeTypes: []string{
`application/json`, `application/json`,

View File

@ -15,21 +15,21 @@ type Type struct {
Css func() string Css func() string
Title func(queryParams, fileUri, filePath, fileName, mime string) string Title func(queryParams, fileUri, filePath, fileName, mime string) string
Body 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 MimeTypes []string
Validate func(filePath string) bool Validate func(filePath string) bool
} }
type Types struct { type Types struct {
Extensions map[string]*Type Extensions map[string]string
MimeTypes map[string]*Type MimeTypes map[string]*Type
} }
func (s *Types) Add(t *Type) { func (s *Types) Add(t *Type) {
for _, v := range t.Extensions { for k, v := range t.Extensions {
_, exists := s.Extensions[v] _, exists := s.Extensions[k]
if !exists { if !exists {
s.Extensions[v] = t s.Extensions[k] = v
} }
} }
@ -63,10 +63,14 @@ func FileType(path string, registeredFormats *Types) (bool, *Type, string, error
} }
// if mime type detection fails, use the file extension // 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 {
fileType, exists := registeredFormats.MimeTypes[mimeType]
if exists { if exists {
return fileType.Validate(path), fileType, mimeType, nil return fileType.Validate(path), fileType, mimeType, nil
} }
}
return false, nil, "", nil return false, nil, "", nil
} }

View File

@ -31,11 +31,11 @@ func RegisterVideos() *Type {
mime, mime,
fileName) fileName)
}, },
Extensions: []string{ Extensions: map[string]string{
`.mp4`, `.mp4`: `video/mp4`,
`.ogm`, `.ogm`: `video/ogg`,
`.ogv`, `.ogv`: `video/ogg`,
`.webm`, `.webm`: `video/webm`,
}, },
MimeTypes: []string{ MimeTypes: []string{
`video/mp4`, `video/mp4`,