FEATURE: Add support for AVIF images (#21680)

This commit is contained in:
Rafael dos Santos Silva 2023-05-24 16:13:36 -03:00 committed by GitHub
parent f3f841a018
commit baa5389a23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 18 additions and 16 deletions

View File

@ -196,7 +196,7 @@ export function authorizedExtensions(staff, siteSettings) {
function authorizedImagesExtensions(staff, siteSettings) {
return authorizesAllExtensions(staff, siteSettings)
? "png, jpg, jpeg, gif, svg, ico, heic, heif, webp"
? "png, jpg, jpeg, gif, svg, ico, heic, heif, webp, avif"
: imagesExtensions(staff, siteSettings).join(", ");
}
@ -230,7 +230,7 @@ export function authorizesOneOrMoreImageExtensions(staff, siteSettings) {
}
export function isImage(path) {
return /\.(png|webp|jpe?g|gif|svg|ico|heic|heif)$/i.test(path);
return /\.(png|webp|jpe?g|gif|svg|ico|heic|heif|avif)$/i.test(path);
}
export function isVideo(path) {

View File

@ -236,14 +236,16 @@ module("Unit | Utility | uploads", function (hooks) {
});
test("isImage", function (assert) {
["png", "webp", "jpg", "jpeg", "gif", "ico"].forEach((extension) => {
let image = "image." + extension;
assert.ok(isImage(image), image + " is recognized as an image");
assert.ok(
isImage("http://foo.bar/path/to/" + image),
image + " is recognized as an image"
);
});
["png", "webp", "jpg", "jpeg", "gif", "ico", "avif"].forEach(
(extension) => {
let image = "image." + extension;
assert.ok(isImage(image), image + " is recognized as an image");
assert.ok(
isImage("http://foo.bar/path/to/" + image),
image + " is recognized as an image"
);
}
);
assert.notOk(isImage("file.txt"));
assert.notOk(isImage("http://foo.bar/path/to/file.txt"));
assert.notOk(isImage(""));

View File

@ -175,7 +175,7 @@ class OptimizedImage < ActiveRecord::Base
paths.each { |path| raise Discourse::InvalidAccess unless safe_path?(path) }
end
IM_DECODERS ||= /\A(jpe?g|png|ico|gif|webp)\z/i
IM_DECODERS ||= /\A(jpe?g|png|ico|gif|webp|avif)\z/i
def self.prepend_decoder!(path, ext_path = nil, opts = nil)
opts ||= {}

View File

@ -203,7 +203,7 @@ server {
try_files $uri =404;
}
# this allows us to bypass rails
location ~* \.(gif|png|jpg|jpeg|bmp|tif|tiff|ico|webp)$ {
location ~* \.(gif|png|jpg|jpeg|bmp|tif|tiff|ico||avif)$ {
add_header Access-Control-Allow-Origin *;
try_files $uri =404;
}

View File

@ -1352,12 +1352,12 @@ files:
default: 50000
max: 1024000
theme_authorized_extensions:
default: "wasm|jpg|jpeg|png|woff|woff2|svg|eot|ttf|otf|gif|webp|js"
default: "wasm|jpg|jpeg|png|woff|woff2|svg|eot|ttf|otf|gif|webp|avif|js"
type: list
list_type: compact
authorized_extensions:
client: true
default: "jpg|jpeg|png|gif|heic|heif|webp"
default: "jpg|jpeg|png|gif|heic|heif|webp|avif"
refresh: true
type: list
list_type: compact

View File

@ -158,7 +158,7 @@ class FileHelper
end
def self.supported_images
@@supported_images ||= Set.new %w[jpg jpeg png gif svg ico webp]
@@supported_images ||= Set.new %w[jpg jpeg png gif svg ico webp avif]
end
def self.inline_images

View File

@ -662,7 +662,7 @@ class UploadCreator
if is_animated != nil
# FastImage will return nil if it cannot determine if animated
is_animated
elsif type == "gif" || type == "webp"
elsif %w[gif webp avif].include?(type)
# Only GIFs, WEBPs and a few other unsupported image types can be animated
OptimizedImage.ensure_safe_paths!(@file.path)