2020-01-12 14:00:56 +01:00
|
|
|
package fs
|
2019-12-15 17:19:16 +01:00
|
|
|
|
|
|
|
import (
|
2022-09-15 00:43:08 +02:00
|
|
|
"path/filepath"
|
2022-10-07 16:37:47 +02:00
|
|
|
"strings"
|
2020-07-21 14:43:45 +02:00
|
|
|
|
2022-10-07 16:37:47 +02:00
|
|
|
"github.com/gabriel-vasile/mimetype"
|
2019-12-15 17:19:16 +01:00
|
|
|
)
|
|
|
|
|
2020-04-07 10:42:42 +02:00
|
|
|
const (
|
2023-02-14 14:43:49 +01:00
|
|
|
MimeTypeUnknown = ""
|
|
|
|
MimeTypeJpeg = "image/jpeg"
|
|
|
|
MimeTypeJpegXL = "image/jxl"
|
|
|
|
MimeTypePng = "image/png"
|
|
|
|
MimeTypeAnimatedPng = "image/vnd.mozilla.apng"
|
|
|
|
MimeTypeGif = "image/gif"
|
|
|
|
MimeTypeBitmap = "image/bmp"
|
|
|
|
MimeTypeTiff = "image/tiff"
|
|
|
|
MimeTypeDNG = "image/dng"
|
|
|
|
MimeTypeAVIF = "image/avif"
|
|
|
|
MimeTypeHEIC = "image/heic"
|
|
|
|
MimeTypeWebP = "image/webp"
|
|
|
|
MimeTypeMP4 = "video/mp4"
|
|
|
|
MimeTypeMOV = "video/quicktime"
|
|
|
|
MimeTypeSVG = "image/svg+xml"
|
|
|
|
MimeTypeAI = "application/vnd.adobe.illustrator"
|
|
|
|
MimeTypePS = "application/ps"
|
|
|
|
MimeTypeEPS = "image/eps"
|
|
|
|
MimeTypeXML = "text/xml"
|
|
|
|
MimeTypeJSON = "application/json"
|
2020-04-07 10:42:42 +02:00
|
|
|
)
|
|
|
|
|
2022-10-07 16:37:47 +02:00
|
|
|
// MimeType returns the mime type of a file, or an empty string if it could not be detected.
|
|
|
|
func MimeType(filename string) (mimeType string) {
|
2023-02-14 11:37:22 +01:00
|
|
|
if filename == "" {
|
|
|
|
return MimeTypeUnknown
|
|
|
|
}
|
|
|
|
|
2023-02-13 20:02:26 +01:00
|
|
|
// Workaround for types that cannot be reliably detected.
|
|
|
|
switch Extensions[strings.ToLower(filepath.Ext(filename))] {
|
|
|
|
case ImageDNG:
|
2022-10-07 16:37:47 +02:00
|
|
|
return MimeTypeDNG
|
2023-02-13 20:02:26 +01:00
|
|
|
case MimeTypeAVIF:
|
2022-10-07 16:37:47 +02:00
|
|
|
return MimeTypeAVIF
|
2023-02-13 20:02:26 +01:00
|
|
|
case VideoMP4:
|
2022-12-23 02:02:47 +01:00
|
|
|
return MimeTypeMP4
|
2023-02-13 20:02:26 +01:00
|
|
|
case MimeTypeMOV:
|
2022-12-23 02:02:47 +01:00
|
|
|
return MimeTypeMOV
|
2023-02-13 20:02:26 +01:00
|
|
|
case VectorSVG:
|
2023-02-11 20:18:04 +01:00
|
|
|
return MimeTypeSVG
|
2023-02-13 20:02:26 +01:00
|
|
|
case VectorAI:
|
|
|
|
return MimeTypeAI
|
|
|
|
case VectorPS:
|
2023-02-11 21:12:39 +01:00
|
|
|
return MimeTypePS
|
2023-02-13 20:02:26 +01:00
|
|
|
case VectorEPS:
|
2023-02-11 20:18:04 +01:00
|
|
|
return MimeTypeEPS
|
2019-12-15 17:19:16 +01:00
|
|
|
}
|
|
|
|
|
2022-10-07 16:37:47 +02:00
|
|
|
if t, err := mimetype.DetectFile(filename); err != nil {
|
|
|
|
return MimeTypeUnknown
|
2020-09-20 15:12:45 +02:00
|
|
|
} else {
|
2022-10-07 16:37:47 +02:00
|
|
|
mimeType, _, _ = strings.Cut(t.String(), ";")
|
2019-12-15 17:19:16 +01:00
|
|
|
}
|
2022-10-07 16:37:47 +02:00
|
|
|
|
|
|
|
return mimeType
|
2019-12-15 17:19:16 +01:00
|
|
|
}
|