From 5b951f012e04e6ef97e4ef645d53bdf433a9eb7f Mon Sep 17 00:00:00 2001 From: erdgeist Date: Tue, 21 Jul 2026 21:02:16 +0200 Subject: Implement model side of PDF raster preview generation --- app/models/concerns/file_attachment.rb | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'app') diff --git a/app/models/concerns/file_attachment.rb b/app/models/concerns/file_attachment.rb index 548531f..6221312 100644 --- a/app/models/concerns/file_attachment.rb +++ b/app/models/concerns/file_attachment.rb @@ -31,6 +31,7 @@ module FileAttachment IMAGE_CONTENT_TYPES = %w[image/jpeg image/gif image/png image/webp].freeze VECTOR_CONTENT_TYPES = %w[image/svg+xml].freeze + RASTERIZED_CONTENT_TYPES = %w[application/pdf].freeze DISPLAYABLE_AS_IMAGE = IMAGE_CONTENT_TYPES + VECTOR_CONTENT_TYPES included do @@ -60,6 +61,11 @@ module FileAttachment has_variant?(:medium) end + def variant_filename(style) + return upload_file_name if style == :original || !RASTERIZED_CONTENT_TYPES.include?(upload_content_type) + File.basename(upload_file_name, ".*") + ".png" + end + private def build_upload_proxy @@ -89,11 +95,7 @@ module FileAttachment FileUtils.mkdir_p(File.dirname(original_path)) FileUtils.cp(uploaded_file.tempfile.path, original_path) - if IMAGE_CONTENT_TYPES.include?(upload_content_type) - generate_variants(original_path) - elsif VECTOR_CONTENT_TYPES.include?(upload_content_type) - generate_svg_variants(original_path) - end + generate_all_variants(original_path) end def generate_variants(original_path) @@ -112,6 +114,16 @@ module FileAttachment end end + def generate_all_variants(original_path) + if IMAGE_CONTENT_TYPES.include?(upload_content_type) + generate_variants(original_path) + elsif VECTOR_CONTENT_TYPES.include?(upload_content_type) + generate_svg_variants(original_path) + elsif RASTERIZED_CONTENT_TYPES.include?(upload_content_type) + generate_variants("#{original_path}[0]") + end + end + def delete_upload_files dir = upload_root.join(id.to_s) FileUtils.rm_rf(dir) if Dir.exist?(dir) @@ -135,7 +147,7 @@ module FileAttachment def url(style = :original) return "" if @record.upload_file_name.blank? - "/system/uploads/#{@record.id}/#{style}/#{@record.upload_file_name}" + "/system/uploads/#{@record.id}/#{style}/#{@record.variant_filename(style)}" end def content_type -- cgit v1.3