Skip to content

Commit bf8e0f5

Browse files
committed
Illustrator .ai files are previewable as PDFs
This happened to work with Marcel 1.0.2 and earlier since magic byte sniffing sees that Illustrator files are PDFs internally, causing these files to be treated as `application/pdf` despite having a declared content type of `application/illustrator` and an `.ai` file extension. Marcel 1.0.3 corrected this to the more specific `application/illustrator` subtype of `application/pdf`, but the MuPDF previewer only accepts the parent `application/pdf` type. Changing it to accept PDF and any child types allows the previewer to explicitly work with Illustrator files again, which was only a happy accident previously.
1 parent 4f0f344 commit bf8e0f5

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

activestorage/lib/active_storage/previewer/mupdf_previewer.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ module ActiveStorage
44
class Previewer::MuPDFPreviewer < Previewer
55
class << self
66
def accept?(blob)
7-
blob.content_type == "application/pdf" && mutool_exists?
7+
pdf?(blob.content_type) && mutool_exists?
8+
end
9+
10+
def pdf?(content_type)
11+
Marcel::Magic.child? content_type, "application/pdf"
812
end
913

1014
def mutool_path

activestorage/test/previewer/mupdf_previewer_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ class ActiveStorage::Previewer::MuPDFPreviewerTest < ActiveSupport::TestCase
3232
end
3333
end
3434

35+
test "previewing an Illustrator document that's a PDF subtype" do
36+
blob = create_file_blob(fixture: "report.pdf", filename: "file.ai", content_type: "application/illustrator")
37+
38+
ActiveStorage::Previewer::MuPDFPreviewer.new(blob).preview do |attachable|
39+
assert_equal "image/png", attachable[:content_type]
40+
assert_equal "file.png", attachable[:filename]
41+
42+
image = MiniMagick::Image.read(attachable[:io])
43+
assert_equal 612, image.width
44+
assert_equal 792, image.height
45+
end
46+
end
47+
3548
test "previewing a PDF that can't be previewed" do
3649
blob = create_file_blob(filename: "video.mp4", content_type: "application/pdf")
3750

0 commit comments

Comments
 (0)