Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ gem but are what an operator runs against their own image.

## next / unreleased

### ActiveStorage::HotCell::Client

#### Fixed

* A JPEG uploaded as `.jfif`, `.jif` or `.jfi` is transformed instead of failing permanently as `unreadable`. Rails names a web image's variant format after the upload's extension. The ImageMagick transformer has no coder for these extensions, and the Vips transformer has no saver for `.jif` or `.jfi`. The transformers now send the cell the canonical extension of the format's content type, such as `jpg`, which is the extension Rails falls back to. (#84)

## v1.0.0 / 2026-10-05

### Documentation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "tempfile"
require "marcel"
require "active_storage"
require "active_support/core_ext/class/attribute"
require "active_support/core_ext/hash/deep_transform_values"
Expand Down Expand Up @@ -33,14 +34,28 @@ def process(file, format:)
output = Tempfile.new([ "hotcell", ".#{format}" ], binmode: true)

begin
convert file, output, format: format.to_s, operations: stringified(transformations)
convert file, output, format: canonical(format), operations: stringified(transformations)
output.tap(&:rewind)
rescue StandardError
output.close!
raise
end
end

# Rails names a web image's variant format after its upload's extension, so a JPEG uploaded as
# `photo.jfif` asks for `jfif`. ImageMagick has no coder for `jfif`, `jif` or `jfi`, and libvips has no
# saver for the last two (#84). So a web image's format crosses as its content type's canonical
# extension, the one Rails itself names when the upload's extension does not match.
def canonical(format)
content_type = Marcel::MimeType.for(extension: format.to_s)

if ActiveStorage.web_image_content_types.include?(content_type)
Marcel::Magic.new(content_type).extensions.first
else
format.to_s
Comment thread
flavorjones marked this conversation as resolved.
Resolved
end
end

# Both handles are reopened by path, narrowly, and that is the protocol rather than fussiness. Rails
# hands out Tempfiles, which are read-write; an input descriptor must be read-only and an output
# write-only. An access mode is fixed at open, so a cell handed the wrong one cannot narrow it — it can
Expand Down
10 changes: 8 additions & 2 deletions activestorage-hotcell-client/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ def with_cell
# For the cases about classification, where booting a cell to produce one verdict would be theatre.
def with_canned_response(response)
HotCell.root = "/nowhere"
transport = CannedTransport.new(response)
HotCell.register ActiveStorage::HotCell::Client::CELL, permanent: Unprocessable,
transient: TemporarilyUnavailable, transport: CannedTransport.new(response)
transient: TemporarilyUnavailable, transport: transport
transport
end

def failed(code)
Expand Down Expand Up @@ -107,11 +109,15 @@ def identify(path)
end

class CannedTransport
attr_reader :lines

def initialize(response)
@response = response
@lines = []
end

def call(_cell, _line, _descriptors, socket: nil, timeout: nil)
def call(_cell, line, _descriptors, socket: nil, timeout: nil)
@lines << line
@response
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ def test_an_imagemagick_shape_the_vips_path_refuses_is_run
end
end

# Rails names a web image's variant format after its upload's extension, so a JPEG uploaded as `photo.jfif`
# asks for format `jfif` (#84).
%w[ jfif JFIF jif jfi ].each do |format|
define_method :"test_a_jpeg_uploaded_as_#{format}_comes_back_as_a_jpeg" do
with_cell do
with_active_storage_setting :web_image_content_types, %w[ image/png image/jpeg image/gif ] do
transform({ resize_to_limit: [ 20, 20 ] }, "colour.jpg", format: format) do |output|
assert_equal "JPEG", identify(output.path)[:format]
assert_operator identify(output.path)[:width], :<=, 20
end
end
end
end
end

# Rails' ImageMagick allowlist runs here, in the application, before anything reaches the cell: a method
# outside supported_image_processing_methods raises the same error Rails raises, and no request is sent.
def test_a_transformation_outside_rails_allowlist_is_refused_before_the_cell
Expand Down
27 changes: 27 additions & 0 deletions activestorage-hotcell-client/test/transformers/vips_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,33 @@ def test_it_converts_the_format
end
end

# Rails names a web image's variant format after its upload's extension, so a JPEG uploaded as `photo.jfif`
# asks for format `jfif` (#84).
%w[ jfif JFIF jif jfi ].each do |format|
define_method :"test_a_jpeg_uploaded_as_#{format}_comes_back_as_a_jpeg" do
with_cell do
with_active_storage_setting :web_image_content_types, %w[ image/png image/jpeg image/gif ] do
transform({ resize_to_limit: [ 20, 20 ] }, "colour.jpg", format: format) do |output|
assert_equal "JPEG", identify(output.path)[:format]
assert_operator identify(output.path)[:width], :<=, 20
end
end
end
end
end

# Rails takes a variant's format from the upload's filename only for a web image, so any other format crosses
# as Rails named it. `tif` would otherwise cross as `tiff`, the canonical extension for image/tiff.
def test_a_format_outside_the_web_image_types_crosses_unchanged
transport = with_canned_response failed("unreadable")

with_active_storage_setting :web_image_content_types, %w[ image/png image/jpeg image/gif ] do
assert_raises(Unprocessable) { transform({}, "colour.png", format: "tif") { flunk "no" } }
end

assert_equal "tif", JSON.parse(transport.lines.last).dig("payload", "format")
end

def test_the_tempfile_is_closed_and_unlinked_afterwards
with_cell do
leaked = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ module Transformers
module Image
# What `ActiveStorage::Transformers::ImageProcessingTransformer` does, moved out of the application,
# and deliberately nothing more: the transformations and the format reach ImageProcessing exactly as
# Rails hands them over.
# Rails hands them over, except that the client sends a web image's format as its content type's
# canonical extension, so `jfif` arrives as `jpg` (#84).
#
# There is no allowlist here, and that mirrors Rails: the vips path validates exactly one thing —
# `combine_options` is refused — and `ActiveStorage.supported_image_processing_methods` is enforced by
Expand Down
Loading