Fix media URL extension for m4a uploads
This commit is contained in:
@@ -4,6 +4,7 @@ package media
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
@@ -68,6 +69,37 @@ type AllowedOrigin struct {
|
||||
|
||||
var fileNamePattern = regexp.MustCompile(`^[-_A-Za-z0-9]+`)
|
||||
|
||||
// ExtensionByMimeType returns a stable URL extension for uploaded media.
|
||||
func ExtensionByMimeType(mimeType string) string {
|
||||
switch strings.ToLower(strings.TrimSpace(strings.Split(mimeType, ";")[0])) {
|
||||
case "audio/x-m4a", "audio/m4a", "audio/mp4", "audio/mp4a-latm":
|
||||
return ".m4a"
|
||||
case "audio/aac", "audio/aacp":
|
||||
return ".aac"
|
||||
case "audio/mpeg", "audio/mp3":
|
||||
return ".mp3"
|
||||
case "audio/ogg":
|
||||
return ".ogg"
|
||||
case "audio/wav", "audio/x-wav", "audio/wave":
|
||||
return ".wav"
|
||||
case "audio/webm":
|
||||
return ".webm"
|
||||
case "image/pjpeg", "image/jpg":
|
||||
return ".jpg"
|
||||
case "image/jpeg":
|
||||
return ".jpg"
|
||||
case "image/jfif":
|
||||
return ".jfif"
|
||||
case "video/mp4":
|
||||
return ".mp4"
|
||||
}
|
||||
exts, _ := mime.ExtensionsByType(mimeType)
|
||||
if len(exts) > 0 {
|
||||
return exts[0]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetIdFromUrl is a helper method for extracting file ID from a URL.
|
||||
func GetIdFromUrl(url, serveUrl string) types.Uid {
|
||||
dir, fname := path.Split(path.Clean(url))
|
||||
|
||||
Reference in New Issue
Block a user