diff --git a/go.mod b/go.mod index 2cc60c830d..c576dc2d61 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 5485bf24e3..386e5f7220 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/mime/mime.go b/pkg/mime/mime.go index 6908450308..ac9cdade7c 100644 --- a/pkg/mime/mime.go +++ b/pkg/mime/mime.go @@ -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" @@ -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 == "" { @@ -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)