Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Illustrator files are previewable with Poppler as well #51236

Merged
merged 1 commit into from Mar 1, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -4,7 +4,11 @@ module ActiveStorage
class Previewer::PopplerPDFPreviewer < Previewer
class << self
def accept?(blob)
blob.content_type == "application/pdf" && pdftoppm_exists?
pdf?(blob.content_type) && pdftoppm_exists?
end

def pdf?(content_type)
Marcel::Magic.child? content_type, "application/pdf"
end

def pdftoppm_path
Expand Down
13 changes: 13 additions & 0 deletions activestorage/test/models/preview_test.rb
Expand Up @@ -30,6 +30,19 @@ class ActiveStorage::PreviewTest < ActiveSupport::TestCase
assert_equal 145, image.height
end

test "previewing a PDF-based Illustrator file" do
blob = create_file_blob(fixture: "report.pdf", filename: "file.ai", content_type: "application/illustrator")
preview = blob.preview(resize_to_limit: [640, 280]).processed

assert_predicate preview.image, :attached?
assert_equal "file.png", preview.image.filename.to_s
assert_equal "image/png", preview.image.content_type

image = read_image(preview.image)
assert_equal 612, image.width
assert_equal 792, image.height
end

test "previewing an MP4 video" do
blob = create_file_blob(filename: "video.mp4", content_type: "video/mp4")
preview = blob.preview(resize_to_limit: [640, 280]).processed
Expand Down
9 changes: 9 additions & 0 deletions activestorage/test/models/representation_test.rb
Expand Up @@ -22,6 +22,15 @@ class ActiveStorage::RepresentationTest < ActiveSupport::TestCase
assert_equal 792, image.height
end

test "representing a PDF-based Illustrator file" do
blob = create_file_blob(fixture: "report.pdf", filename: "file.ai", content_type: "application/illustrator")
representation = blob.representation(resize_to_limit: [640, 280]).processed

image = read_image(representation.image)
assert_equal 612, image.width
assert_equal 792, image.height
end

test "representing an MP4 video" do
blob = create_file_blob(filename: "video.mp4", content_type: "video/mp4")
representation = blob.representation(resize_to_limit: [640, 280]).processed
Expand Down
13 changes: 13 additions & 0 deletions activestorage/test/previewer/poppler_pdf_previewer_test.rb
Expand Up @@ -32,6 +32,19 @@ class ActiveStorage::Previewer::PopplerPDFPreviewerTest < ActiveSupport::TestCas
end
end

test "previewing an Illustrator document that's a PDF subtype" do
blob = create_file_blob(fixture: "report.pdf", filename: "file.ai", content_type: "application/illustrator")

ActiveStorage::Previewer::PopplerPDFPreviewer.new(blob).preview do |attachable|
assert_equal "image/png", attachable[:content_type]
assert_equal "file.png", attachable[:filename]

image = MiniMagick::Image.read(attachable[:io])
assert_equal 612, image.width
assert_equal 792, image.height
end
end

test "previewing a PDF that can't be previewed" do
blob = create_file_blob(filename: "video.mp4", content_type: "application/pdf")

Expand Down