Skip to content

Commit

Permalink
Implemented reverse resolution for mimetypes
Browse files Browse the repository at this point in the history
  • Loading branch information
glpatcern committed Oct 26, 2022
1 parent 1238448 commit f7a8012
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ require (
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/glpatcern/go-mime v0.0.0-20221026162842-2a8d71ad17a9 // indirect
github.com/go-asn1-ber/asn1-ber v1.5.4 // indirect
github.com/go-kit/log v0.2.0 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ github.com/gdexlab/go-render v1.0.1/go.mod h1:wRi5nW2qfjiGj4mPukH4UV0IknS1cHD4Vg
github.com/getkin/kin-openapi v0.13.0/go.mod h1:WGRs2ZMM1Q8LR1QBEwUxC6RJEfaBcD0s+pcEVXFuAjw=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/glpatcern/go-mime v0.0.0-20221026162842-2a8d71ad17a9 h1:3um08ooi0/lyRmK2eE1XTKmRQHDzPu0IvpCPMljyMZ8=
github.com/glpatcern/go-mime v0.0.0-20221026162842-2a8d71ad17a9/go.mod h1:EJaddanP+JfU3UkVvn0rYYF3b/gD7eZRejbTHqiQExA=
github.com/go-acme/lego/v4 v4.4.0/go.mod h1:l3+tFUFZb590dWcqhWZegynUthtaHJbG2fevUpoOOE0=
github.com/go-asn1-ber/asn1-ber v1.5.4 h1:vXT6d/FNDiELJnLb6hGNa309LMsrCoYFvpwHDF0+Y1A=
github.com/go-asn1-ber/asn1-ber v1.5.4/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0=
Expand Down
25 changes: 24 additions & 1 deletion pkg/mime/mime.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ package mime

import (
"path"
"strings"
"sync"

gomime "github.com/cubewise-code/go-mime"
gomime "github.com/glpatcern/go-mime" // hopefully temporary
)

const defaultMimeDir = "httpd/unix-directory"
Expand All @@ -47,11 +48,15 @@ func Detect(isDir bool, fn string) string {
}

ext := path.Ext(fn)
ext = strings.TrimPrefix(ext, ".")

mimeType := getCustomMime(ext)

if mimeType == "" {
mimeType = gomime.TypeByExtension(ext)
if mimeType != "" {
mimes.Store(ext, mimeType)
}
}

if mimeType == "" {
Expand All @@ -61,6 +66,24 @@ func Detect(isDir bool, fn string) string {
return mimeType
}

// GetFileExt performs the inverse resolution from mimetype to file extension
func GetFileExt(mime string) []string {
var found []string
// first look in our cache
mimes.Range(func(e, m interface{}) bool {
if m.(string) == mime {
found = append(found, e.(string))
}
return true
})
if len(found) > 0 {
return found
}

// then use the gomime package
return gomime.ExtensionsByType(mime)
}

func getCustomMime(ext string) string {
if m, ok := mimes.Load(ext); ok {
return m.(string)
Expand Down

0 comments on commit f7a8012

Please sign in to comment.