Skip to content

Commit 5cedb87

Browse files
committed
Illustrator files are previewable with Poppler as well
Extends rails#51235's MuPDF support to Poppler. Add test coverage for Blob previews and representation.
1 parent bf8e0f5 commit 5cedb87

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

activestorage/lib/active_storage/previewer/poppler_pdf_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::PopplerPDFPreviewer < Previewer
55
class << self
66
def accept?(blob)
7-
blob.content_type == "application/pdf" && pdftoppm_exists?
7+
pdf?(blob.content_type) && pdftoppm_exists?
8+
end
9+
10+
def pdf?(content_type)
11+
Marcel::Magic.child? content_type, "application/pdf"
812
end
913

1014
def pdftoppm_path

activestorage/test/models/preview_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ class ActiveStorage::PreviewTest < ActiveSupport::TestCase
3030
assert_equal 145, image.height
3131
end
3232

33+
test "previewing a PDF-based Illustrator file" do
34+
blob = create_file_blob(fixture: "report.pdf", filename: "file.ai", content_type: "application/illustrator")
35+
preview = blob.preview(resize_to_limit: [640, 280]).processed
36+
37+
assert_predicate preview.image, :attached?
38+
assert_equal "file.png", preview.image.filename.to_s
39+
assert_equal "image/png", preview.image.content_type
40+
41+
image = read_image(preview.image)
42+
assert_equal 612, image.width
43+
assert_equal 792, image.height
44+
end
45+
3346
test "previewing an MP4 video" do
3447
blob = create_file_blob(filename: "video.mp4", content_type: "video/mp4")
3548
preview = blob.preview(resize_to_limit: [640, 280]).processed

activestorage/test/models/representation_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ class ActiveStorage::RepresentationTest < ActiveSupport::TestCase
2222
assert_equal 792, image.height
2323
end
2424

25+
test "representing a PDF-based Illustrator file" do
26+
blob = create_file_blob(fixture: "report.pdf", filename: "file.ai", content_type: "application/illustrator")
27+
representation = blob.representation(resize_to_limit: [640, 280]).processed
28+
29+
image = read_image(representation.image)
30+
assert_equal 612, image.width
31+
assert_equal 792, image.height
32+
end
33+
2534
test "representing an MP4 video" do
2635
blob = create_file_blob(filename: "video.mp4", content_type: "video/mp4")
2736
representation = blob.representation(resize_to_limit: [640, 280]).processed

activestorage/test/previewer/poppler_pdf_previewer_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ class ActiveStorage::Previewer::PopplerPDFPreviewerTest < ActiveSupport::TestCas
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::PopplerPDFPreviewer.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)