mirror of
1
Fork 0

feat: Make AVIF Images work with Forgejo

This commit is contained in:
JakobDev 2024-11-13 19:09:40 +01:00
parent 5614719e0a
commit 19a27ded86
No known key found for this signature in database
GPG Key ID: 39DEF62C3ED6DC4C
4 changed files with 23 additions and 5 deletions

View File

@ -1938,7 +1938,7 @@ LEVEL = Info
;ENABLED = true ;ENABLED = true
;; ;;
;; Comma-separated list of allowed file extensions (`.zip`), mime types (`text/plain`) or wildcard type (`image/*`, `audio/*`, `video/*`). Empty value or `*/*` allows all types. ;; Comma-separated list of allowed file extensions (`.zip`), mime types (`text/plain`) or wildcard type (`image/*`, `audio/*`, `video/*`). Empty value or `*/*` allows all types.
;ALLOWED_TYPES = .cpuprofile,.csv,.dmp,.docx,.fodg,.fodp,.fods,.fodt,.gif,.gz,.jpeg,.jpg,.json,.jsonc,.log,.md,.mov,.mp4,.odf,.odg,.odp,.ods,.odt,.patch,.pdf,.png,.pptx,.svg,.tgz,.txt,.webm,.xls,.xlsx,.zip ;ALLOWED_TYPES = .avif,.cpuprofile,.csv,.dmp,.docx,.fodg,.fodp,.fods,.fodt,.gif,.gz,.jpeg,.jpg,.json,.jsonc,.log,.md,.mov,.mp4,.odf,.odg,.odp,.ods,.odt,.patch,.pdf,.png,.pptx,.svg,.tgz,.txt,.webm,.xls,.xlsx,.zip
;; ;;
;; Max size of each file. Defaults to 2048MB ;; Max size of each file. Defaults to 2048MB
;MAX_SIZE = 2048 ;MAX_SIZE = 2048
@ -1976,7 +1976,7 @@ LEVEL = Info
;; Url lookup for the minio bucket only available when STORAGE_TYPE is `minio` ;; Url lookup for the minio bucket only available when STORAGE_TYPE is `minio`
;; Available values: auto, dns, path ;; Available values: auto, dns, path
;; If empty, it behaves the same as "auto" was set ;; If empty, it behaves the same as "auto" was set
;MINIO_BUCKET_LOOKUP = ;MINIO_BUCKET_LOOKUP =
;; ;;
;; Minio location to create bucket only available when STORAGE_TYPE is `minio` ;; Minio location to create bucket only available when STORAGE_TYPE is `minio`
;MINIO_LOCATION = us-east-1 ;MINIO_LOCATION = us-east-1
@ -2703,7 +2703,7 @@ LEVEL = Info
;; Url lookup for the minio bucket only available when STORAGE_TYPE is `minio` ;; Url lookup for the minio bucket only available when STORAGE_TYPE is `minio`
;; Available values: auto, dns, path ;; Available values: auto, dns, path
;; If empty, it behaves the same as "auto" was set ;; If empty, it behaves the same as "auto" was set
;MINIO_BUCKET_LOOKUP = ;MINIO_BUCKET_LOOKUP =
;; ;;
;; Minio location to create bucket only available when STORAGE_TYPE is `minio` ;; Minio location to create bucket only available when STORAGE_TYPE is `minio`
;MINIO_LOCATION = us-east-1 ;MINIO_LOCATION = us-east-1

View File

@ -12,7 +12,7 @@ var Attachment = struct {
Enabled bool Enabled bool
}{ }{
Storage: &Storage{}, Storage: &Storage{},
AllowedTypes: ".cpuprofile,.csv,.dmp,.docx,.fodg,.fodp,.fods,.fodt,.gif,.gz,.jpeg,.jpg,.json,.jsonc,.log,.md,.mov,.mp4,.odf,.odg,.odp,.ods,.odt,.patch,.pdf,.png,.pptx,.svg,.tgz,.txt,.webm,.webp,.xls,.xlsx,.zip", AllowedTypes: ".avif,.cpuprofile,.csv,.dmp,.docx,.fodg,.fodp,.fods,.fodt,.gif,.gz,.jpeg,.jpg,.json,.jsonc,.log,.md,.mov,.mp4,.odf,.odg,.odp,.ods,.odt,.patch,.pdf,.png,.pptx,.svg,.tgz,.txt,.webm,.webp,.xls,.xlsx,.zip",
MaxSize: 2048, MaxSize: 2048,
MaxFiles: 5, MaxFiles: 5,
Enabled: true, Enabled: true,
@ -25,7 +25,7 @@ func loadAttachmentFrom(rootCfg ConfigProvider) (err error) {
return err return err
} }
Attachment.AllowedTypes = sec.Key("ALLOWED_TYPES").MustString(".cpuprofile,.csv,.dmp,.docx,.fodg,.fodp,.fods,.fodt,.gif,.gz,.jpeg,.jpg,.json,.jsonc,.log,.md,.mov,.mp4,.odf,.odg,.odp,.ods,.odt,.patch,.pdf,.png,.pptx,.svg,.tgz,.txt,.webm,.webp,.xls,.xlsx,.zip") Attachment.AllowedTypes = sec.Key("ALLOWED_TYPES").MustString(".avif,.cpuprofile,.csv,.dmp,.docx,.fodg,.fodp,.fods,.fodt,.gif,.gz,.jpeg,.jpg,.json,.jsonc,.log,.md,.mov,.mp4,.odf,.odg,.odp,.ods,.odt,.patch,.pdf,.png,.pptx,.svg,.tgz,.txt,.webm,.webp,.xls,.xlsx,.zip")
Attachment.MaxSize = sec.Key("MAX_SIZE").MustInt64(2048) Attachment.MaxSize = sec.Key("MAX_SIZE").MustInt64(2048)
Attachment.MaxFiles = sec.Key("MAX_FILES").MustInt(5) Attachment.MaxFiles = sec.Key("MAX_FILES").MustInt(5)
Attachment.Enabled = sec.Key("ENABLED").MustBool(true) Attachment.Enabled = sec.Key("ENABLED").MustBool(true)

View File

@ -20,6 +20,8 @@ const sniffLen = 1024
const ( const (
// SvgMimeType MIME type of SVG images. // SvgMimeType MIME type of SVG images.
SvgMimeType = "image/svg+xml" SvgMimeType = "image/svg+xml"
// AvifMimeType MIME type of AVIF images
AvifMimeType = "image/avif"
// ApplicationOctetStream MIME type of binary files. // ApplicationOctetStream MIME type of binary files.
ApplicationOctetStream = "application/octet-stream" ApplicationOctetStream = "application/octet-stream"
) )
@ -106,6 +108,12 @@ func DetectContentType(data []byte) SniffedType {
} }
} }
// AVIF is unsuported by http.DetectContentType
// Signature taken from https://stackoverflow.com/a/68322450
if bytes.Index(data, []byte("ftypavif")) == 4 {
ct = AvifMimeType
}
if strings.HasPrefix(ct, "audio/") && bytes.HasPrefix(data, []byte("ID3")) { if strings.HasPrefix(ct, "audio/") && bytes.HasPrefix(data, []byte("ID3")) {
// The MP3 detection is quite inaccurate, any content with "ID3" prefix will result in "audio/mpeg". // The MP3 detection is quite inaccurate, any content with "ID3" prefix will result in "audio/mpeg".
// So remove the "ID3" prefix and detect again, if result is text, then it must be text content. // So remove the "ID3" prefix and detect again, if result is text, then it must be text content.

View File

@ -135,3 +135,13 @@ func TestDetectContentTypeOgg(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
assert.True(t, st.IsVideo()) assert.True(t, st.IsVideo())
} }
func TestDetectContentTypeAvif(t *testing.T) {
avifImage, err := hex.DecodeString("000000206674797061766966")
require.NoError(t, err)
st, err := DetectContentTypeFromReader(bytes.NewReader(avifImage))
require.NoError(t, err)
assert.True(t, st.IsImage())
}