Skip to content

A platform for community discussion. Free, open, simple.

DEV: Add image-processing regression coverage (#42117)

Alan Guo Xiang Tan9e2a10b4271d
diff --git a/lib/letter_avatar.rb b/lib/letter_avatar.rb
index 9d91b1f2266..81424e8fd81 100644
--- a/lib/letter_avatar.rb
+++ b/lib/letter_avatar.rb
@@ -31,6 +31,7 @@ class LetterAvatar
"tmp/letter_avatars/#{version}"
end
+ # Run `script/letter_avatar_pixel_diff` to inspect rendering changes.
def generate(username, size, opts = nil)
DistributedMutex.synchronize("letter_avatar_#{version}_#{username}") do
identity = (opts && opts[:identity]) || LetterAvatar::Identity.from_username(username)
diff --git a/script/letter_avatar_pixel_diff b/script/letter_avatar_pixel_diff
new file mode 100755
index 00000000000..b7c3db602fa
--- /dev/null
+++ b/script/letter_avatar_pixel_diff
@@ -0,0 +1,234 @@
+#!/usr/bin/env ruby
+# frozen_string_literal: true
+
+ENV["RAILS_ENV"] ||= "test"
+
+require_relative "../config/environment"
+
+require "base64"
+require "chunky_png"
+require "fileutils"
+require "letter_avatar"
+require "optparse"
+
+# Compare representative A-Z renders with Linux baselines and highlight pixel drift.
+module LetterAvatarPixelDiff
+ LETTERS = ("A".."Z").to_a.freeze
+ AVATAR_SIZE = 45
+ BASELINE_DIRECTORY = Rails.root.join("spec/fixtures/images/letter_avatar_visual/linux")
+ REPORT_DIRECTORY = Rails.root.join("tmp/letter-avatar-pixel-diff")
+
+ class << self
+ def run(arguments)
+ options = parse_options(arguments)
+ generated_paths = {}
+
+ if RbConfig::CONFIG["host_os"].match?(/darwin/i)
+ warn "LetterAvatar pixel baselines are Linux-only."
+ return 2
+ end
+
+ LETTERS.each do |letter|
+ generated_paths[letter] = LetterAvatar.generate(letter, AVATAR_SIZE, cache: false)
+ end
+
+ if options[:update_baselines]
+ update_baselines(generated_paths)
+ puts "Updated Linux A-Z baselines in #{BASELINE_DIRECTORY}."
+ return 0
+ end
+
+ missing_baselines =
+ LETTERS.reject { |letter| BASELINE_DIRECTORY.join("#{letter}.png").exist? }
+ if missing_baselines.any?
+ warn(
+ "Missing Linux baselines for #{missing_baselines.join(", ")}. " \
+ "Run `script/letter_avatar_pixel_diff --update-baselines`.",
+ )
+ return 2
+ end
+
+ comparisons = compare(generated_paths)
+ write_report(comparisons)
+ comparisons.each do |comparison|
+ changed_pixel_count = comparison[:changed_pixel_count]
+ pixel_count = comparison[:pixel_count]
+ changed_percentage = pixel_count.zero? ? 0 : (changed_pixel_count.fdiv(pixel_count) * 100)
+ puts(
+ "#{comparison[:letter]}: #{changed_pixel_count}/#{pixel_count} pixels differ " \
+ "(#{format("%.2f", changed_percentage)}%)",
+ )
+ end
+
+ if comparisons.any? { |comparison| comparison[:changed_pixel_count].positive? }
+ warn "LetterAvatar pixels changed. Open #{REPORT_DIRECTORY.join("index.html")}."
+ 1
+ else
+ puts "All Linux A-Z pixel baselines match. Report: #{REPORT_DIRECTORY.join("index.html")}"
+ 0
+ end
+ rescue OptionParser::ParseError => error
+ warn error.message
+ 2
+ ensure
+ remove_generated_avatars(generated_paths)
+ end
+
+ private
+
+ def parse_options(arguments)
+ options = { update_baselines: false }
+ OptionParser
+ .new do |parser|
+ parser.banner = "Usage: script/letter_avatar_pixel_diff [--update-baselines]"
+ parser.on("--update-baselines", "Regenerate every committed Linux A-Z baseline.") do
+ options[:update_baselines] = true
+ end
+ end
+ .parse!(arguments)
+ options
+ end
+
+ def update_baselines(generated_paths)
+ FileUtils.mkdir_p(BASELINE_DIRECTORY)
+ generated_paths.each do |letter, generated_path|
+ FileUtils.cp(generated_path, BASELINE_DIRECTORY.join("#{letter}.png"))
+ end
+ end
+
+ def compare(generated_paths)
+ generated_paths.map do |letter, generated_path|
+ expected = ChunkyPNG::Image.from_file(BASELINE_DIRECTORY.join("#{letter}.png"))
+ actual = ChunkyPNG::Image.from_file(generated_path)
+ changed_pixel_count, pixel_count = changed_pixel_count(expected, actual)
+
+ {
+ letter: letter,
+ expected: expected,
+ actual: actual,
+ changed_pixel_count: changed_pixel_count,
+ pixel_count: pixel_count,
+ }
+ end
+ end
+
+ def changed_pixel_count(expected, actual)
+ width = [expected.width, actual.width].max
+ height = [expected.height, actual.height].max
+ count =
+ height.times.sum do |y_position|
+ width.times.count do |x_position|
+ expected_pixel = expected[x_position, y_position] if x_position < expected.width &&
+ y_position < expected.height
+ actual_pixel = actual[x_position, y_position] if x_position < actual.width &&
+ y_position < actual.height
+
+ expected_pixel != actual_pixel
+ end
+ end
+
+ [count, width * height]
+ end
+
+ def write_report(comparisons)
+ FileUtils.mkdir_p(REPORT_DIRECTORY)
+ sections = comparisons.map { |comparison| write_comparison(comparison) }
+ report = <<~HTML
+ <!doctype html>
+ <html lang="en">
+ <head>
+ <meta charset="utf-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1">
+ <title>LetterAvatar pixel diff</title>
+ <style>
+ body { color: #222; font: 16px/1.5 sans-serif; margin: 2rem; }
+ section { border-top: 1px solid #ccc; margin-top: 2rem; }
+ .images { display: flex; flex-wrap: wrap; gap: 2rem; }
+ figure { margin: 0; }
+ img { image-rendering: pixelated; width: 180px; height: 180px; }
+ </style>
+ </head>
+ <body>
+ <h1>LetterAvatar pixel diff</h1>
+ <p>Baseline: Linux</p>
+ #{sections.join}
+ </body>
+ </html>
+ HTML
+ File.write(REPORT_DIRECTORY.join("index.html"), report)
+ end
+
+ def write_comparison(comparison)
+ letter = comparison[:letter]
+ expected = comparison[:expected]
+ actual = comparison[:actual]
+ diff = build_diff(expected, actual)
+ expected_path = REPORT_DIRECTORY.join("#{letter}-expected.png")
+ actual_path = REPORT_DIRECTORY.join("#{letter}-actual.png")
+ diff_path = REPORT_DIRECTORY.join("#{letter}-diff.png")
+ expected.save(expected_path)
+ actual.save(actual_path)
+ diff.save(diff_path)
+ expected_data = Base64.strict_encode64(File.binread(expected_path))
+ actual_data = Base64.strict_encode64(File.binread(actual_path))
+ diff_data = Base64.strict_encode64(File.binread(diff_path))
+ changed_pixel_count = comparison[:changed_pixel_count]
+ pixel_count = comparison[:pixel_count]
+ changed_percentage = pixel_count.zero? ? 0 : (changed_pixel_count.fdiv(pixel_count) * 100)
+
+ <<~HTML
+ <section>
+ <h2>#{letter}: #{changed_pixel_count}/#{pixel_count} pixels differ (#{format("%.2f", changed_percentage)}%)</h2>
+ <div class="images">
+ <figure><figcaption>Expected</figcaption><img src="data:image/png;base64,#{expected_data}" alt="Expected #{letter}"></figure>
+ <figure><figcaption>Actual</figcaption><img src="data:image/png;base64,#{actual_data}" alt="Actual #{letter}"></figure>
+ <figure><figcaption>Diff (changed pixels are magenta)</figcaption><img src="data:image/png;base64,#{diff_data}" alt="Pixel diff for #{letter}"></figure>
+ </div>
+ </section>
+ HTML
+ end
+
+ def build_diff(expected, actual)
+ width = [expected.width, actual.width].max
+ height = [expected.height, actual.height].max
+ diff = ChunkyPNG::Image.new(width, height, ChunkyPNG::Color::TRANSPARENT)
+
+ height.times do |y_position|
+ width.times do |x_position|
+ expected_pixel = expected[x_position, y_position] if x_position < expected.width &&
+ y_position < expected.height
+ actual_pixel = actual[x_position, y_position] if x_position < actual.width &&
+ y_position < actual.height
+
+ diff[x_position, y_position] = if expected_pixel.nil? && actual_pixel.nil?
+ ChunkyPNG::Color::TRANSPARENT
+ elsif expected_pixel == actual_pixel
+ dim(actual_pixel)
+ else
+ ChunkyPNG::Color.rgba(255, 0, 255, 255)
+ end
+ end
+ end
+
+ diff
+ end
+
+ def dim(pixel)
+ ChunkyPNG::Color.rgba(
+ ChunkyPNG::Color.r(pixel) / 4,
+ ChunkyPNG::Color.g(pixel) / 4,
+ ChunkyPNG::Color.b(pixel) / 4,
+ ChunkyPNG::Color.a(pixel),
+ )
+ end
+
+ def remove_generated_avatars(generated_paths)
+ generated_paths&.each do |letter, generated_path|
+ FileUtils.rm_f(generated_path)
+ FileUtils.rm_f(LetterAvatar.fullsize_path(LetterAvatar::Identity.from_username(letter)))
+ end
+ end
+ end
+end
+
+exit LetterAvatarPixelDiff.run(ARGV)
diff --git a/spec/fixtures/images/broken.png b/spec/fixtures/images/broken.png
new file mode 100644
index 00000000000..1eafb848262
Binary files /dev/null and b/spec/fixtures/images/broken.png differ
diff --git a/spec/fixtures/images/crop_position.png b/spec/fixtures/images/crop_position.png
new file mode 100644
index 00000000000..895db432bde
Binary files /dev/null and b/spec/fixtures/images/crop_position.png differ
diff --git a/spec/fixtures/images/exif_orientation.jpg b/spec/fixtures/images/exif_orientation.jpg
new file mode 100644
index 00000000000..b53c848dd12
Binary files /dev/null and b/spec/fixtures/images/exif_orientation.jpg differ
diff --git a/spec/fixtures/images/hostile_entity.svg b/spec/fixtures/images/hostile_entity.svg
new file mode 100644
index 00000000000..f693df37c01
--- /dev/null
+++ b/spec/fixtures/images/hostile_entity.svg
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE svg [
+ <!ENTITY local SYSTEM "file:///etc/passwd">
+]>
+<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10">
+ <text>&local;</text>
+ <rect width="10" height="10" fill="#0088cc"/>
+</svg>
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/A.png b/spec/fixtures/images/letter_avatar_visual/linux/A.png
new file mode 100644
index 00000000000..f03ee92d190
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/A.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/B.png b/spec/fixtures/images/letter_avatar_visual/linux/B.png
new file mode 100644
index 00000000000..e176d189cdf
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/B.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/C.png b/spec/fixtures/images/letter_avatar_visual/linux/C.png
new file mode 100644
index 00000000000..c1308e1633e
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/C.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/D.png b/spec/fixtures/images/letter_avatar_visual/linux/D.png
new file mode 100644
index 00000000000..9b7e10d9307
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/D.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/E.png b/spec/fixtures/images/letter_avatar_visual/linux/E.png
new file mode 100644
index 00000000000..a34f9003200
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/E.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/F.png b/spec/fixtures/images/letter_avatar_visual/linux/F.png
new file mode 100644
index 00000000000..f0f646e69db
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/F.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/G.png b/spec/fixtures/images/letter_avatar_visual/linux/G.png
new file mode 100644
index 00000000000..036015aaa95
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/G.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/H.png b/spec/fixtures/images/letter_avatar_visual/linux/H.png
new file mode 100644
index 00000000000..762a5dd5899
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/H.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/I.png b/spec/fixtures/images/letter_avatar_visual/linux/I.png
new file mode 100644
index 00000000000..832fb97b75e
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/I.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/J.png b/spec/fixtures/images/letter_avatar_visual/linux/J.png
new file mode 100644
index 00000000000..be4633e33ac
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/J.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/K.png b/spec/fixtures/images/letter_avatar_visual/linux/K.png
new file mode 100644
index 00000000000..90a06966c68
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/K.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/L.png b/spec/fixtures/images/letter_avatar_visual/linux/L.png
new file mode 100644
index 00000000000..2c264c77273
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/L.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/M.png b/spec/fixtures/images/letter_avatar_visual/linux/M.png
new file mode 100644
index 00000000000..ebe92022085
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/M.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/N.png b/spec/fixtures/images/letter_avatar_visual/linux/N.png
new file mode 100644
index 00000000000..1da40345e08
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/N.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/O.png b/spec/fixtures/images/letter_avatar_visual/linux/O.png
new file mode 100644
index 00000000000..cac47b653cf
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/O.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/P.png b/spec/fixtures/images/letter_avatar_visual/linux/P.png
new file mode 100644
index 00000000000..43a3333e402
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/P.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/Q.png b/spec/fixtures/images/letter_avatar_visual/linux/Q.png
new file mode 100644
index 00000000000..a775c6b225c
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/Q.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/R.png b/spec/fixtures/images/letter_avatar_visual/linux/R.png
new file mode 100644
index 00000000000..f1588acfb79
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/R.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/S.png b/spec/fixtures/images/letter_avatar_visual/linux/S.png
new file mode 100644
index 00000000000..f37f8d8230a
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/S.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/T.png b/spec/fixtures/images/letter_avatar_visual/linux/T.png
new file mode 100644
index 00000000000..14d0735e1e9
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/T.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/U.png b/spec/fixtures/images/letter_avatar_visual/linux/U.png
new file mode 100644
index 00000000000..fbcf3ae6339
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/U.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/V.png b/spec/fixtures/images/letter_avatar_visual/linux/V.png
new file mode 100644
index 00000000000..897fbfed013
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/V.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/W.png b/spec/fixtures/images/letter_avatar_visual/linux/W.png
new file mode 100644
index 00000000000..9c59ccd6458
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/W.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/X.png b/spec/fixtures/images/letter_avatar_visual/linux/X.png
new file mode 100644
index 00000000000..669bbdbd9ee
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/X.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/Y.png b/spec/fixtures/images/letter_avatar_visual/linux/Y.png
new file mode 100644
index 00000000000..ef515d4d7e7
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/Y.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/Z.png b/spec/fixtures/images/letter_avatar_visual/linux/Z.png
new file mode 100644
index 00000000000..b750847f9b8
Binary files /dev/null and b/spec/fixtures/images/letter_avatar_visual/linux/Z.png differ
diff --git a/spec/fixtures/images/tiny_animated.gif b/spec/fixtures/images/tiny_animated.gif
new file mode 100644
index 00000000000..ecab19dfcfa
Binary files /dev/null and b/spec/fixtures/images/tiny_animated.gif differ
diff --git a/spec/fixtures/images/transparent.png b/spec/fixtures/images/transparent.png
new file mode 100644
index 00000000000..e74e01abd74
Binary files /dev/null and b/spec/fixtures/images/transparent.png differ
diff --git a/spec/lib/letter_avatar_spec.rb b/spec/lib/letter_avatar_spec.rb
index 3dd910ab6ad..9ffa639a5b6 100644
--- a/spec/lib/letter_avatar_spec.rb
+++ b/spec/lib/letter_avatar_spec.rb
@@ -3,14 +3,33 @@
require "letter_avatar"
RSpec.describe LetterAvatar do
- it "can cleanup correctly" do
- path = LetterAvatar.cache_path
+ describe ".cleanup_old" do
+ it "removes stale cache directories" do
+ path = LetterAvatar.cache_path
- FileUtils.mkdir_p(path + "junk")
- LetterAvatar.generate("test", 100)
+ FileUtils.mkdir_p(path + "junk")
+ LetterAvatar.generate("test", 100)
- LetterAvatar.cleanup_old
+ LetterAvatar.cleanup_old
- expect(Dir.entries(File.dirname(path)).length).to eq(3)
+ expect(Dir.entries(File.dirname(path)).length).to eq(3)
+ end
+ end
+
+ describe ".generate" do
+ it "generates a PNG avatar with the requested dimensions" do
+ username = "A"
+ avatar_size = 45
+ generated_path = described_class.generate(username, avatar_size, cache: false)
+
+ expect(FastImage.type(generated_path)).to eq(:png)
+ expect(FastImage.size(generated_path)).to eq([avatar_size, avatar_size])
+ ensure
+ if generated_path
+ identity = LetterAvatar::Identity.from_username(username)
+ FileUtils.rm_f(generated_path)
+ FileUtils.rm_f(described_class.fullsize_path(identity))
+ end
+ end
end
end
diff --git a/spec/lib/upload_creator_spec.rb b/spec/lib/upload_creator_spec.rb
index a34fe07eeef..5f0024d3a59 100644
--- a/spec/lib/upload_creator_spec.rb
+++ b/spec/lib/upload_creator_spec.rb
@@ -7,7 +7,9 @@ RSpec.describe UploadCreator do
fab!(:admin)
describe "#create_for" do
- describe "when the image is an SVG" do
+ context "when the upload is an SVG" do
+ before { SiteSetting.authorized_extensions = "svg" }
+
it "removes external use references while preserving local fragments" do
xlink_namespace = "http://www.w3.org/1999/xlink"
svg = <<~XML
@@ -37,6 +39,57 @@ RSpec.describe UploadCreator do
document.at_css("#external-xlink-href").attribute_with_ns("href", xlink_namespace)&.value,
).to eq(nil)
end
+
+ it "stores an SVG upload without event handlers" do
+ file = Tempfile.new
+ file.write(<<~XML)
+ <?xml version="1.0" encoding="UTF-8"?>
+ <svg xmlns="http://www.w3.org/2000/svg" onload="alert(location)"></svg>
+ XML
+ file.rewind
+
+ upload = UploadCreator.new(file, "file.svg").create_for(user.id)
+ file_content = File.read(Discourse.store.path_for(upload))
+ document = Nokogiri.XML(file_content)
+
+ expect(upload).to be_persisted
+ expect(document.xpath("//@*[starts-with(name(), 'on')]")).to be_empty
+ ensure
+ file&.close!
+ end
+
+ it "removes entity references and subsets from a hostile SVG upload" do
+ upload =
+ UploadCreator.new(
+ file_from_fixtures("hostile_entity.svg"),
+ "hostile_entity.svg",
+ ).create_for(user.id)
+ file_content = File.read(Discourse.store.path_for(upload))
+ document = Nokogiri.XML(file_content)
+
+ expect(upload).to be_persisted
+ expect(document.internal_subset).to eq(nil)
+ expect(document.external_subset).to eq(nil)
+ expect(document.xpath("//text()").map(&:text).join).not_to include("root:")
+ expect(file_content).not_to include("file:///etc/passwd")
+ end
+ end
+
+ context "when the PNG contains transparency" do
+ let(:filename) { "transparent.png" }
+ let(:file) { file_from_fixtures(filename) }
+
+ it "preserves the transparent pixel in the stored image" do
+ upload = described_class.new(file, filename).create_for(user.id)
+ stored_path = Discourse.store.path_for(upload)
+ stored_image = ChunkyPNG::Image.from_file(stored_path)
+
+ expect(upload).to be_persisted
+ expect(upload.extension).to eq("png")
+ expect(FastImage.type(stored_path)).to eq(:png)
+ expect(FastImage.size(stored_path)).to eq([1, 1])
+ expect(ChunkyPNG::Color.a(stored_image[0, 0])).to eq(127)
+ end
end
describe "when upload is not an image" do
@@ -106,21 +159,19 @@ RSpec.describe UploadCreator do
let(:filename) { "png_as.bin" }
let(:file) { file_from_fixtures(filename) }
- it "should store the upload with the right extension" do
- expect do
- UploadCreator.new(
- file,
- filename,
- force_optimize: true,
- type: UploadCreator::TYPES_TO_CROP.first,
- ).create_for(user.id)
- end.to change { Upload.count }.by(1)
-
- upload = Upload.last
+ it "detects and stores PNG content with a .bin extension" do
+ upload = UploadCreator.new(file, filename).create_for(user.id)
+ stored_path = Discourse.store.path_for(upload)
- expect(upload.extension).to eq("png")
- expect(File.extname(upload.url)).to eq(".png")
- expect(upload.original_filename).to eq("png_as.png")
+ expect(upload).to be_persisted
+ expect(upload).to have_attributes(
+ original_filename: "png_as.png",
+ extension: "png",
+ width: 5,
+ height: 1,
+ )
+ expect(FastImage.type(stored_path)).to eq(:png)
+ expect(FastImage.size(stored_path)).to eq([5, 1])
end
describe "for tiff format" do
@@ -159,6 +210,109 @@ RSpec.describe UploadCreator do
end
end
+ context "when optimization is forced for an animated GIF" do
+ let(:file) do
+ # Regenerate from the repository root with:
+ # ruby -rbase64 -e 'File.binwrite("spec/fixtures/images/tiny_animated.gif", Base64.strict_decode64("R0lGODlhAgABAPAAAP8AAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQAAAAAACwAAAAAAgABAAACAgQKACH5BAAKAAAALAAAAAACAAEAgAAA/wAAAAICBAoAOw=="))'
+ file_from_fixtures("tiny_animated.gif")
+ end
+
+ it "preserves the GIF and records its animation metadata" do
+ upload =
+ described_class.new(file, "tiny_animated.gif", force_optimize: true).create_for(user.id)
+ stored_path = Discourse.store.path_for(upload)
+
+ expect(upload).to be_persisted
+ expect(upload.extension).to eq("gif")
+ expect(upload.width).to eq(2)
+ expect(upload.height).to eq(1)
+ expect(upload.animated).to eq(true)
+ expect(FastImage.type(stored_path)).to eq(:gif)
+ expect(FastImage.size(stored_path)).to eq([2, 1])
+ end
+ end
+
+ context "when a JPEG has EXIF orientation metadata" do
+ include ImageOrientationHelpers
+
+ # Regenerate from the repository root:
+ # magick \( xc:red xc:lime xc:blue +append \) \( xc:cyan xc:magenta xc:yellow +append \) -append -scale 60x40! -sampling-factor 4:4:4 -quality 95 spec/fixtures/images/exif_orientation.jpg
+ let(:source_path) { Rails.root.join("spec/fixtures/images/exif_orientation.jpg") }
+ let(:expected_color_grids) do
+ {
+ 1 => [%i[red green blue], %i[cyan magenta yellow]],
+ 2 => [%i[blue green red], %i[yellow magenta cyan]],
+ 3 => [%i[yellow magenta cyan], %i[blue green red]],
+ 4 => [%i[cyan magenta yellow], %i[red green blue]],
+ 5 => [%i[red cyan], %i[green magenta], %i[blue yellow]],
+ 6 => [%i[cyan red], %i[magenta green], %i[yellow blue]],
+ 7 => [%i[yellow blue], %i[magenta green], %i[cyan red]],
+ 8 => [%i[blue yellow], %i[green magenta], %i[red cyan]],
+ }
+ end
+ let(:palette) do
+ {
+ red: [255, 0, 0],
+ green: [0, 255, 0],
+ blue: [0, 0, 255],
+ cyan: [0, 255, 255],
+ magenta: [255, 0, 255],
+ yellow: [255, 255, 0],
+ }
+ end
+
+ it "honors every EXIF orientation when storing JPEG uploads" do
+ expected_color_grids.each do |orientation, expected_color_grid|
+ with_jpeg_orientation(
+ source_path: source_path,
+ orientation: orientation,
+ ) do |oriented_file|
+ upload =
+ described_class.new(
+ oriented_file,
+ "oriented-#{orientation}.jpg",
+ force_optimize: true,
+ ).create_for(user.id)
+ actual_color_grid =
+ stored_color_grid(
+ upload: upload,
+ rows: expected_color_grid.length,
+ columns: expected_color_grid.first.length,
+ palette: palette,
+ )
+
+ expect(upload).to be_persisted
+ expect(upload).to have_attributes(
+ width: expected_color_grid.first.length * 20,
+ height: expected_color_grid.length * 20,
+ )
+ expect(actual_color_grid).to eq(expected_color_grid)
+ end
+ end
+ end
+ end
+
+ context "when a detected PNG cannot be decoded during conversion" do
+ # Regenerate from the repository root with:
+ # ruby -rzlib -e 'ihdr = [1, 1, 8, 6, 0, 0, 0].pack("NNCCCCC"); chunk = [ihdr.bytesize].pack("N") + "IHDR" + ihdr + [Zlib.crc32("IHDR" + ihdr)].pack("N"); File.binwrite("spec/fixtures/images/broken.png", "\x89PNG\r\n\x1A\n".b + chunk + ("\0".b * 80_000))'
+ let(:broken_png_file) { file_from_fixtures("broken.png") }
+
+ it "does not persist the upload" do
+ SiteSetting.png_to_jpg_quality = 1
+
+ expect do
+ expect do
+ UploadCreator.new(
+ broken_png_file,
+ "broken.png",
+ pasted: true,
+ force_optimize: true,
+ ).create_for(user.id)
+ end.to raise_error(Discourse::Utils::CommandError)
+ end.not_to change(Upload, :count)
+ end
+ end
+
describe "pngquant" do
let(:filename) { "pngquant.png" }
let(:file) { file_from_fixtures(filename) }
@@ -231,6 +385,8 @@ RSpec.describe UploadCreator do
expect(upload.extension).to eq("jpeg")
expect(File.extname(upload.url)).to eq(".jpeg")
expect(upload.original_filename).to eq("should_be_jpeg.jpg")
+ expect(FastImage.type(Discourse.store.path_for(upload))).to eq(:jpeg)
+ expect(FastImage.size(Discourse.store.path_for(upload))).to eq([303, 231])
end
it "should not convert to jpeg when the image is uploaded from site setting" do
@@ -668,53 +824,23 @@ RSpec.describe UploadCreator do
expect(upload.persisted?).to eq(false)
end
end
- end
- describe "#convert_favicon_to_png!" do
- let(:filename) { "smallest.ico" }
- let(:file) { file_from_fixtures(filename, "images") }
+ context "when the upload is an ICO favicon" do
+ let(:filename) { "smallest.ico" }
+ let(:file) { file_from_fixtures(filename, "images") }
- before { SiteSetting.authorized_extensions = "png|jpg|ico" }
+ before { SiteSetting.authorized_extensions = "png|jpg|ico" }
- it "converts to png" do
- upload = UploadCreator.new(file, filename).create_for(user.id)
+ it "stores it as a PNG" do
+ upload = described_class.new(file, filename).create_for(user.id)
+ stored_path = Discourse.store.path_for(upload)
- expect(upload.persisted?).to eq(true)
- expect(upload.extension).to eq("png")
- end
- end
-
- describe "#clean_svg!" do
- let(:b64) { Base64.encode64('<svg onmouseover="alert(alert)" />') }
-
- let(:file) do
- file = Tempfile.new
- file.write(<<~XML)
- <?xml version="1.0" encoding="UTF-8"?>
- <svg xmlns="http://www.w3.org/2000/svg" width="200px" height="200px" onload="alert(location)">
- <defs>
- <path id="pathdef" d="m0 0h100v100h-77z" stroke="#000" />
- </defs>
- <g>
- <use id="valid-use" x="123" href="#pathdef" />
- </g>
- <use id="invalid-use1" xlink:href="https://svg.example.com/evil.svg" />
- <use id="invalid-use2" href="data:image/svg+xml;base64,#{b64}" />
- </svg>
- XML
- file.rewind
- file
- end
-
- it "removes event handlers" do
- UploadCreator.new(file, "file.svg").clean_svg!
- file_content = file.read
- expect(file_content).not_to include("onload")
- expect(file_content).to include("#pathdef")
- expect(file_content).not_to include("evil.svg")
- expect(file_content).not_to include(b64)
- ensure
- file.unlink
+ expect(upload).to be_persisted
+ expect(upload.extension).to eq("png")
+ expect(upload.original_filename).to eq("smallest.png")
+ expect(FastImage.type(stored_path)).to eq(:png)
+ expect(FastImage.size(stored_path)).to eq([1, 1])
+ end
end
end
diff --git a/spec/models/optimized_image_spec.rb b/spec/models/optimized_image_spec.rb
index a58c0d02e91..4d4e9bbd228 100644
--- a/spec/models/optimized_image_spec.rb
+++ b/spec/models/optimized_image_spec.rb
@@ -1,5 +1,7 @@
# frozen_string_literal: true
+require "chunky_png"
+
RSpec.describe OptimizedImage do
let(:upload) { build(:upload) }
before { upload.id = 42 }
@@ -30,25 +32,24 @@ RSpec.describe OptimizedImage do
end
end
- it "should correctly crop images vertically" do
- tmp_path = "/tmp/cropped.png"
- desired_width = 100
- desired_height = 66
+ it "crops vertical content from the top edge" do
+ FileHelper.stubs(:optimize_image!).returns(true)
- begin
- OptimizedImage.crop(
- "#{Rails.root.join("spec/fixtures/images/logo.png")}", # 244x66px
- tmp_path,
- desired_width,
- desired_height,
- )
+ Dir.mktmpdir do |directory|
+ output_path = File.join(directory, "cropped.png")
- w, h = FastImage.size(tmp_path)
+ described_class.crop(
+ Rails.root.join("spec/fixtures/images/crop_position.png").to_s,
+ output_path,
+ 3,
+ 2,
+ )
+ output_image = ChunkyPNG::Image.from_file(output_path)
- expect(w).to eq(desired_width)
- expect(h).to eq(desired_height)
- ensure
- File.delete(tmp_path) if File.exist?(tmp_path)
+ expect([output_image.width, output_image.height]).to eq([3, 2])
+ expect(output_image.pixels).to all(
+ satisfy { |pixel| ChunkyPNG::Color.r(pixel) > ChunkyPNG::Color.b(pixel) },
+ )
end
end
@@ -180,6 +181,23 @@ RSpec.describe OptimizedImage do
File.delete(tmp_path) if File.exist?(tmp_path)
end
end
+
+ it "keeps a tiny image at its original dimensions when it is within the requested bounds" do
+ FileHelper.stubs(:optimize_image!).returns(true)
+
+ Dir.mktmpdir do |dir|
+ output_path = File.join(dir, "downsized.png")
+
+ OptimizedImage.downsize(
+ "#{Rails.root.join("spec/fixtures/images/resized.png")}",
+ output_path,
+ "10x10\>",
+ )
+
+ expect(FastImage.type(output_path)).to eq(:png)
+ expect(FastImage.size(output_path)).to eq([5, 5])
+ end
+ end
end
end
@@ -257,8 +275,9 @@ RSpec.describe OptimizedImage do
optimized_new = OptimizedImage.create_for(upload, 10, 10, format: "gif")
expect(optimized_new.id).to eq(old_id)
- path = Shellwords.escape(Discourse.store.path_for(optimized_new))
- expect(`identify -format %m #{path}`.strip).to eq("GIF")
+ path = Discourse.store.path_for(optimized_new)
+ expect(FastImage.type(path)).to eq(:gif)
+ expect(FastImage.size(path)).to eq([10, 10])
# cleanup (which transaction rollback may miss)
optimized_new.destroy
diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb
index 19f8010efc8..d9f85d5885b 100644
--- a/spec/models/upload_spec.rb
+++ b/spec/models/upload_spec.rb
@@ -930,6 +930,12 @@ RSpec.describe Upload do
let(:white_image) { Fabricate(:image_upload, color: "white") }
let(:red_image) { Fabricate(:image_upload, color: "red") }
let(:high_color_image) { Fabricate(:image_upload, color: "#000A00F00", color_depth: 16) }
+ let(:tiny_image) do
+ upload = Fabricate(:upload, extension: "png")
+ file = file_from_fixtures("cropped.png")
+ upload.update!(url: Discourse.store.store_upload(file, upload))
+ upload
+ end
let(:not_an_image) do
upload = Fabricate(:upload)
@@ -967,6 +973,18 @@ RSpec.describe Upload do
# EF is closer to F00 than F0
expect(high_color_image.dominant_color(calculate_if_missing: true)).to eq("009FEF")
expect(high_color_image.dominant_color).to eq("009FEF")
+
+ uncached_tiny_color = tiny_image.dominant_color
+
+ expect(uncached_tiny_color).to eq(nil)
+
+ calculated_tiny_color = tiny_image.dominant_color(calculate_if_missing: true)
+
+ expect(calculated_tiny_color).to eq("524F40")
+
+ cached_tiny_color = tiny_image.dominant_color
+
+ expect(cached_tiny_color).to eq(calculated_tiny_color)
end
it "can be backfilled" do
@@ -1061,6 +1079,22 @@ RSpec.describe Upload do
end
end
+ describe "#target_image_quality" do
+ let(:local_path) { Rails.root.join("spec/fixtures/images/logo.jpg").to_s }
+
+ it "returns nil when the target quality is higher than the source quality" do
+ target_quality = upload.target_image_quality(local_path, 100)
+
+ expect(target_quality).to eq(nil)
+ end
+
+ it "returns the target quality when it is lower than the source quality" do
+ target_quality = upload.target_image_quality(local_path, 10)
+
+ expect(target_quality).to eq(10)
+ end
+ end
+
describe ".mark_invalid_s3_uploads_as_missing" do
it "should update all upload records with a `verification_status` of `invalid_etag` to `s3_file_missing`" do
upload_1 =
diff --git a/spec/support/image_orientation_helpers.rb b/spec/support/image_orientation_helpers.rb
new file mode 100644
index 00000000000..c5763695ba7
--- /dev/null
+++ b/spec/support/image_orientation_helpers.rb
@@ -0,0 +1,54 @@
+# frozen_string_literal: true
+
+require "chunky_png"
+
+module ImageOrientationHelpers
+ private
+
+ def with_jpeg_orientation(source_path:, orientation:)
+ exif =
+ "Exif\0\0".b + "II*\0".b + [8].pack("V") + [1].pack("v") + [0x0112].pack("v") +
+ [3].pack("v") + [1].pack("V") + [orientation].pack("v") + "\0\0".b + [0].pack("V")
+ marker = "\xFF\xE1".b + [exif.bytesize + 2].pack("n") + exif
+ source = File.binread(source_path)
+ oriented_file = Tempfile.new(["oriented-#{orientation}", ".jpg"])
+ oriented_file.binmode
+ oriented_file.write(source.byteslice(0, 2) + marker + source.byteslice(2..))
+ oriented_file.rewind
+
+ yield oriented_file
+ ensure
+ oriented_file&.close!
+ end
+
+ def stored_color_grid(upload:, rows:, columns:, palette:)
+ stored_path = Discourse.store.path_for(upload)
+
+ Dir.mktmpdir do |directory|
+ png_path = File.join(directory, "stored.png")
+ ImageMagick.magick(stored_path, png_path, read: [stored_path], write: [directory])
+ stored_image = ChunkyPNG::Image.from_file(png_path)
+
+ Array.new(rows) do |row_index|
+ Array.new(columns) do |column_index|
+ x = (column_index * stored_image.width + stored_image.width / 2) / columns
+ y = (row_index * stored_image.height + stored_image.height / 2) / rows
+
+ nearest_palette_color(pixel: stored_image[x, y], palette: palette)
+ end
+ end
+ end
+ end
+
+ def nearest_palette_color(pixel:, palette:)
+ actual_rgb = [ChunkyPNG::Color.r(pixel), ChunkyPNG::Color.g(pixel), ChunkyPNG::Color.b(pixel)]
+
+ palette
+ .min_by do |_name, expected_rgb|
+ expected_rgb
+ .zip(actual_rgb)
+ .sum { |expected_channel, actual_channel| (expected_channel - actual_channel)**2 }
+ end
+ .first
+ end
+end