diff options
| author | erdgeist <erdgeist@erdgeist.org> | 2026-07-21 21:02:16 +0200 |
|---|---|---|
| committer | erdgeist <erdgeist@erdgeist.org> | 2026-07-21 21:02:16 +0200 |
| commit | 5b951f012e04e6ef97e4ef645d53bdf433a9eb7f (patch) | |
| tree | 207addbdbb48df480f491d2b4b860c18171b3549 /app | |
| parent | 1791d8067d00dc1381b9cdc3cc4ad44c1f6c7197 (diff) | |
Implement model side of PDF raster preview generation
Diffstat (limited to 'app')
| -rw-r--r-- | app/models/concerns/file_attachment.rb | 24 |
1 files changed, 18 insertions, 6 deletions
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 | |||
| 31 | 31 | ||
| 32 | IMAGE_CONTENT_TYPES = %w[image/jpeg image/gif image/png image/webp].freeze | 32 | IMAGE_CONTENT_TYPES = %w[image/jpeg image/gif image/png image/webp].freeze |
| 33 | VECTOR_CONTENT_TYPES = %w[image/svg+xml].freeze | 33 | VECTOR_CONTENT_TYPES = %w[image/svg+xml].freeze |
| 34 | RASTERIZED_CONTENT_TYPES = %w[application/pdf].freeze | ||
| 34 | DISPLAYABLE_AS_IMAGE = IMAGE_CONTENT_TYPES + VECTOR_CONTENT_TYPES | 35 | DISPLAYABLE_AS_IMAGE = IMAGE_CONTENT_TYPES + VECTOR_CONTENT_TYPES |
| 35 | 36 | ||
| 36 | included do | 37 | included do |
| @@ -60,6 +61,11 @@ module FileAttachment | |||
| 60 | has_variant?(:medium) | 61 | has_variant?(:medium) |
| 61 | end | 62 | end |
| 62 | 63 | ||
| 64 | def variant_filename(style) | ||
| 65 | return upload_file_name if style == :original || !RASTERIZED_CONTENT_TYPES.include?(upload_content_type) | ||
| 66 | File.basename(upload_file_name, ".*") + ".png" | ||
| 67 | end | ||
| 68 | |||
| 63 | private | 69 | private |
| 64 | 70 | ||
| 65 | def build_upload_proxy | 71 | def build_upload_proxy |
| @@ -89,11 +95,7 @@ module FileAttachment | |||
| 89 | FileUtils.mkdir_p(File.dirname(original_path)) | 95 | FileUtils.mkdir_p(File.dirname(original_path)) |
| 90 | FileUtils.cp(uploaded_file.tempfile.path, original_path) | 96 | FileUtils.cp(uploaded_file.tempfile.path, original_path) |
| 91 | 97 | ||
| 92 | if IMAGE_CONTENT_TYPES.include?(upload_content_type) | 98 | generate_all_variants(original_path) |
| 93 | generate_variants(original_path) | ||
| 94 | elsif VECTOR_CONTENT_TYPES.include?(upload_content_type) | ||
| 95 | generate_svg_variants(original_path) | ||
| 96 | end | ||
| 97 | end | 99 | end |
| 98 | 100 | ||
| 99 | def generate_variants(original_path) | 101 | def generate_variants(original_path) |
| @@ -112,6 +114,16 @@ module FileAttachment | |||
| 112 | end | 114 | end |
| 113 | end | 115 | end |
| 114 | 116 | ||
| 117 | def generate_all_variants(original_path) | ||
| 118 | if IMAGE_CONTENT_TYPES.include?(upload_content_type) | ||
| 119 | generate_variants(original_path) | ||
| 120 | elsif VECTOR_CONTENT_TYPES.include?(upload_content_type) | ||
| 121 | generate_svg_variants(original_path) | ||
| 122 | elsif RASTERIZED_CONTENT_TYPES.include?(upload_content_type) | ||
| 123 | generate_variants("#{original_path}[0]") | ||
| 124 | end | ||
| 125 | end | ||
| 126 | |||
| 115 | def delete_upload_files | 127 | def delete_upload_files |
| 116 | dir = upload_root.join(id.to_s) | 128 | dir = upload_root.join(id.to_s) |
| 117 | FileUtils.rm_rf(dir) if Dir.exist?(dir) | 129 | FileUtils.rm_rf(dir) if Dir.exist?(dir) |
| @@ -135,7 +147,7 @@ module FileAttachment | |||
| 135 | 147 | ||
| 136 | def url(style = :original) | 148 | def url(style = :original) |
| 137 | return "" if @record.upload_file_name.blank? | 149 | return "" if @record.upload_file_name.blank? |
| 138 | "/system/uploads/#{@record.id}/#{style}/#{@record.upload_file_name}" | 150 | "/system/uploads/#{@record.id}/#{style}/#{@record.variant_filename(style)}" |
| 139 | end | 151 | end |
| 140 | 152 | ||
| 141 | def content_type | 153 | def content_type |
