fileupload: hardcode a few primary file extensions

This commit is contained in:
Simon Ser 2024-03-11 11:41:54 +01:00
parent 185f6ae3bd
commit f214e5a5aa
2 changed files with 20 additions and 2 deletions

View file

@ -39,6 +39,23 @@ var inlineMIMETypes = map[string]bool{
"video/webm": true,
}
// Some MIME types have multiple possible extensions, and
// mime.ExtensionsByType returns them out-of-order. We have to hardcode
// a few MIME types to work around this unfortunately (e.g. to not use
// ".jfif" for "image/jpeg").
//
// Note, this is not for registering new MIME types (use mime.AddExtensionType
// for that purpose).
var primaryExts = map[string]string{
"audio/aac": "aac",
"audio/mp4": "mp4",
"audio/mpeg": "mp3",
"audio/ogg": "oga",
"image/jpeg": "jpeg",
"text/plain": "txt",
"video/mp4": "mp4",
}
type Uploader interface {
load(filename string) (basename string, modTime time.Time, content io.ReadSeekCloser, err error)
store(r io.Reader, username, mimeType, basename string) (outFilename string, err error)

View file

@ -44,8 +44,9 @@ func (fs *fs) store(r io.Reader, username, mimeType, origBasename string) (outFi
var suffix string
if filepath.Ext(origBasename) == "" && mimeType != "" {
exts, _ := mime.ExtensionsByType(mimeType)
if len(exts) > 0 {
if ext, ok := primaryExts[mimeType]; ok {
suffix = "." + ext
} else if exts, _ := mime.ExtensionsByType(mimeType); len(exts) == 1 {
suffix = exts[0]
}
}