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) {
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 {

View File

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

View File

@ -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),
}

View File

@ -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

View File

@ -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`,

View File

@ -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`,

View File

@ -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`,

View File

@ -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

View File

@ -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`,