feat: support jpeg-xl (jxl) (#40283)

Major browsers (Chrome/Firefox/Safari) all support it now,
https://developer.chrome.com/blog/jpeg-xl-in-chrome

* Fix #16629
This commit is contained in:
wxiaoguang authored and GitHub committed 2026-11-08 15:24:46 +00:00
1 parent 8cb6fb2001
commit 3699309edd
10 files changed
+44 -34

No files matched your search

+1 -1
View File
@@ -2058,7 +2058,7 @@ LEVEL = Info
;ENABLED = true
;;
;; Comma-separated list of allowed issue/pull-request attachment file extensions (`.zip`), mime types (`text/plain`) or wildcard type (`image/*`, `audio/*`, `video/*`). Empty value or `*/*` allows all types.
;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,.webp,.xls,.xlsx,.zip
;ALLOWED_TYPES = .avif,.cpuprofile,.csv,.dmp,.docx,.fodg,.fodp,.fods,.fodt,.gif,.gz,.jpeg,.jpg,.json,.jsonc,.jxl,.log,.md,.mov,.mp4,.odf,.odg,.odp,.ods,.odt,.patch,.pdf,.png,.pptx,.svg,.tgz,.txt,.webm,.webp,.xls,.xlsx,.zip
;;
;; Max size of each issue/pull-request attachment file. Defaults to 100MB
;MAX_SIZE = 100
+1 -1
View File
@@ -80,7 +80,7 @@ func serveSetHeaderContentRelated(w http.ResponseWriter, contentType string) {
func ServeSetHeaders(w http.ResponseWriter, opts ServeHeaderOptions) {
header := w.Header()
skipCompressionExts := container.SetOf(".gz", ".bz2", ".zip", ".xz", ".zst", ".deb", ".apk", ".jar", ".png", ".jpg", ".webp")
skipCompressionExts := container.SetOf(".gz", ".bz2", ".zip", ".xz", ".zst", ".deb", ".apk", ".jar", ".avif", ".png", ".jpeg", ".jpg", ".jxl", ".webp")
if skipCompressionExts.Contains(strings.ToLower(path.Ext(opts.Filename))) {
w.Header().Add(gzhttp.HeaderNoCompression, "1")
}
+1 -1
View File
@@ -97,7 +97,7 @@ func shortLinkProcessor(ctx *RenderContext, node *html.Node) {
// fast path: empty string, ignore
case "":
// leave image as false
case ".jpg", ".jpeg", ".png", ".tif", ".tiff", ".webp", ".gif", ".bmp", ".ico", ".svg":
case ".avif", ".jpg", ".jpeg", ".jxl", ".png", ".tif", ".tiff", ".webp", ".gif", ".bmp", ".ico", ".svg":
image = true
}
+1
View File
@@ -20,6 +20,7 @@ var wellKnownMimeTypesLower = sync.OnceValue(func() map[string]string {
".html": "text/html; charset=utf-8",
".jpeg": "image/jpeg",
".jpg": "image/jpeg",
".jxl": "image/jxl",
".js": "text/javascript; charset=utf-8",
".json": "application/json",
".mjs": "text/javascript; charset=utf-8",
+1 -1
View File
@@ -15,7 +15,7 @@ var Attachment AttachmentSettingType
func loadAttachmentFrom(rootCfg ConfigProvider) (err error) {
Attachment = AttachmentSettingType{
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",
AllowedTypes: ".avif,.cpuprofile,.csv,.dmp,.docx,.fodg,.fodp,.fods,.fodt,.gif,.gz,.jpeg,.jpg,.json,.jsonc,.jxl,.log,.md,.mov,.mp4,.odf,.odg,.odp,.ods,.odt,.patch,.pdf,.png,.pptx,.svg,.tgz,.txt,.webm,.webp,.xls,.xlsx,.zip",
MaxSize: 100,
MaxFiles: 5,
Enabled: true,
+7
View File
@@ -18,6 +18,7 @@ const SniffContentSize = 1024
const (
MimeTypeImageSvg = "image/svg+xml"
MimeTypeImageAvif = "image/avif"
MimeTypeImageJxl = "image/jxl"
MimeTypeApplicationOctetStream = "application/octet-stream"
)
@@ -170,6 +171,12 @@ func DetectContentType(data []byte) SniffedType {
ct = MimeTypeImageAvif
}
// https://github.com/libjxl/libjxl/blob/main/plugins/mime/image-jxl.xml
if bytes.HasPrefix(data, []byte("\xFF\x0A")) ||
bytes.HasPrefix(data, []byte("\x00\x00\x00\x0CJXL \x0D\x0A\x87\x0A")) {
ct = MimeTypeImageJxl
}
if ct == "application/ogg" {
dataHead := data
if len(dataHead) > 256 {
+10 -4
View File
@@ -150,10 +150,16 @@ func TestDetectFileTypeBox(t *testing.T) {
assert.Equal(t, []string{"AAAA"}, brands)
}
func TestDetectContentTypeAvif(t *testing.T) {
buf := []byte("\x00\x00\x00\x20ftypavif.......................")
st := DetectContentType(buf)
assert.Equal(t, MimeTypeImageAvif, st.contentType)
func TestDetectContentTypeImage(t *testing.T) {
t.Run("avif", func(t *testing.T) {
buf := []byte("\x00\x00\x00\x20ftypavif.......................")
st := DetectContentType(buf)
assert.Equal(t, MimeTypeImageAvif, st.contentType)
})
t.Run("jxl", func(t *testing.T) {
assert.Equal(t, MimeTypeImageJxl, DetectContentType([]byte("\xFF\x0A....")).contentType)
assert.Equal(t, MimeTypeImageJxl, DetectContentType([]byte("\x00\x00\x00\x0CJXL \x0D\x0A\x87\x0A...")).contentType)
})
}
func TestDetectContentTypeIncorrectFont(t *testing.T) {
+2 -2
View File
@@ -5,7 +5,6 @@ package upload
import (
"mime"
"net/http"
"net/url"
"path"
"regexp"
@@ -16,6 +15,7 @@ import (
"gitea.dev/modules/log"
"gitea.dev/modules/reqctx"
"gitea.dev/modules/setting"
"gitea.dev/modules/typesniffer"
"gitea.dev/modules/util"
"gitea.dev/services/context"
)
@@ -60,7 +60,7 @@ func Verify(buf []byte, fileName, allowedTypesStr string) error {
return nil // everything is allowed
}
fullMimeType := http.DetectContentType(buf)
fullMimeType := typesniffer.DetectContentType(buf).GetMimeType()
mimeType, _, err := mime.ParseMediaType(fullMimeType)
if err != nil {
log.Warn("Detected attachment type could not be parsed %s", fullMimeType)
+19 -23
View File
@@ -4,42 +4,38 @@
package upload
import (
"bytes"
"compress/gzip"
"testing"
"github.com/stretchr/testify/assert"
)
func TestUpload(t *testing.T) {
testContent := []byte(`This is a plain text file.`)
var b bytes.Buffer
w := gzip.NewWriter(&b)
w.Write(testContent)
w.Close()
kases := []struct {
text := []byte(`This is a plain text file.`)
gzip := []byte("\x1F\x8B\x08\x00\x00\x00\x00\x00\x00\xFF")
jxl := []byte("\xFF\x0A....")
cases := []struct {
data []byte
fileName string
allowedTypes []string
err error
}{
{testContent, "test.txt", []string{""}, nil},
{testContent, "dir/test.txt", []string{""}, nil},
{testContent, "../../../test.txt", []string{""}, nil},
{testContent, "test.txt", []string{"", ",", "|", "*/*", "*/*,", "*/*|", "text/plain"}, nil},
{testContent, "dir/test.txt", []string{"text/plain"}, nil},
{testContent, "/dir.txt/test.js", []string{".js"}, nil},
{testContent, "test.txt", []string{" text/plain ", ".txt", " .txt,.js", " .txt|.js"}, nil},
{testContent, "../../test.txt", []string{" .txt|.js"}, nil},
{testContent, "test.txt", []string{" .txt ,.js ", "text/plain, .txt", "text/*", "text/*,.js"}, nil},
{testContent, "test.txt", []string{"text/**", "application/x-gzip", ".zip", ".zip,.txtx", ".zip|.txtx"}, ErrFileTypeForbidden{"text/plain; charset=utf-8"}},
{b.Bytes(), "test.txt", []string{"application/x-gzip"}, nil},
{text, "test.txt", []string{""}, nil},
{text, "dir/test.txt", []string{""}, nil},
{text, "../../../test.txt", []string{""}, nil},
{text, "test.txt", []string{"", ",", "|", "*/*", "*/*,", "*/*|", "text/plain"}, nil},
{text, "dir/test.txt", []string{"text/plain"}, nil},
{text, "/dir.txt/test.js", []string{".js"}, nil},
{text, "test.txt", []string{" text/plain ", ".txt", " .txt,.js", " .txt|.js"}, nil},
{text, "../../test.txt", []string{" .txt|.js"}, nil},
{text, "test.txt", []string{" .txt ,.js ", "text/plain, .txt", "text/*", "text/*,.js"}, nil},
{text, "test.txt", []string{"text/**", "application/x-gzip", ".zip", ".zip,.txtx", ".zip|.txtx"}, ErrFileTypeForbidden{"text/plain"}},
{gzip, "test.txt", []string{"application/x-gzip"}, nil},
{jxl, "test.jxl", []string{"image/jxl"}, nil},
}
for _, kase := range kases {
for _, allowedTypes := range kase.allowedTypes {
assert.Equal(t, kase.err, Verify(kase.data, kase.fileName, allowedTypes))
for _, tc := range cases {
for _, allowedTypes := range tc.allowedTypes {
assert.Equal(t, tc.err, Verify(tc.data, tc.fileName, allowedTypes))
}
}
}
+1 -1
View File
@@ -180,7 +180,7 @@ export function sleep(ms: number): Promise<void> {
}
export function isImageFile({name, type}: {name: string | null, type: string | null}): boolean {
return Boolean(/\.(avif|jpe?g|png|gif|webp|svg|heic)$/i.test(name || '') || type?.startsWith('image/'));
return Boolean(/\.(avif|jpeg|jpg|jxl|png|gif|webp|svg|heic)$/i.test(name || '') || type?.startsWith('image/'));
}
export function isVideoFile({name, type}: {name: string | null, type: string | null}): boolean {