From b5137e763b0b70f942d6810a0c58367167025602 Mon Sep 17 00:00:00 2001 From: DarthSim Date: Fri, 22 Dec 2023 19:20:42 +0300 Subject: [PATCH] Add imgproxy_pro_active_storage route --- CHANGELOG.md | 2 ++ README.md | 11 +++++++++++ config/routes.rb | 2 +- lib/imgproxy-rails/helpers.rb | 10 +++++++--- spec/avatar.mp4 | Bin 0 -> 2418 bytes spec/{previewable.pdf => avatar.pdf} | Bin spec/internal_spec.rb | 27 ++++++++++++++++++++++----- spec/rails_helper.rb | 15 --------------- 8 files changed, 43 insertions(+), 24 deletions(-) create mode 100644 spec/avatar.mp4 rename spec/{previewable.pdf => avatar.pdf} (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 551d7e0..e067ed2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## master +- Added support for `video/*` and `application/pdf` content types. ([@DarthSim][]) + ## 0.2.0 (2023-10-26) - Improved `resize_and_pad` support. ([@palkan][]) diff --git a/README.md b/README.md index 60fa3c0..3be2d9b 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,17 @@ You can also specify imgproxy-specific parameters in `imgproxy_options` attribut Current.user.avatar.variant(resize: "100x100", imgproxy_options: {height: 50, width: 50}) ``` +### Generating video and PDF previews + +If you are an imgproxy Pro user and you want to use it to generate previews for your videos and PDFs, just add their content types to the `variable_content_types` list: + +```ruby +config.active_storage.variable_content_types << "application/pdf" +config.active_storage.variable_content_types << "video/mp4" +config.active_storage.variable_content_types << "video/mov" +# ...etc +``` + ## Contributing Bug reports and pull requests are welcome on GitHub at [https://github.com/imgproxy/imgproxy-rails](https://github.com/imgproxy/imgproxy-rails). diff --git a/config/routes.rb b/config/routes.rb index 23f9acc..a4079fd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,7 +2,7 @@ Rails.application.routes.draw do direct :imgproxy_active_storage do |model, options| - if ImgproxyRails::Helpers.image_variation?(model) + if ImgproxyRails::Helpers.applicable_variation?(model) transformations = model.variation.transformations Imgproxy.url_for(model.blob, ImgproxyRails::Transformer.call(transformations)) else diff --git a/lib/imgproxy-rails/helpers.rb b/lib/imgproxy-rails/helpers.rb index c96a365..2b07b96 100644 --- a/lib/imgproxy-rails/helpers.rb +++ b/lib/imgproxy-rails/helpers.rb @@ -2,9 +2,13 @@ module ImgproxyRails module Helpers - def self.image_variation?(model) - model.respond_to?(:variation) && - model.try(:blob)&.content_type&.split("/")&.first == "image" + def self.applicable_variation?(model) + return false if !model.respond_to?(:variation) + + content_type = model.try(:blob)&.content_type + content_type&.start_with?("image/") || + content_type&.start_with?("video/") || + content_type == "application/pdf" end end end diff --git a/spec/avatar.mp4 b/spec/avatar.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..ecd955570790fee0312e9a9dd2adcef9d05952ce GIT binary patch literal 2418 zcmZuz3se;66`ln_6VWIl55X`6wQ6PAmprx^MX8ELD?uU2QL~+$nPtbF*%@bMm&FIc z6HqtkQBx&VdlW@0@c{y2eV_uxSRoZ!n+iUtJd8$!M0^w|Nbl^5A)el||9`*xod3Uf zckUtxf|{gso)$S4K|Bzd!_we(Le26zHG&``OaetAi1!?p#3k@4^!6GUICf{=_ZPow zO!#x|j0UtZyz>W#CQ64!padsSs2ZT4RjXo9t(sEDnqt9_pah4=C5iJEMCi~tDe>S* zQUn;{IX<1COcJV5sk9Lql|~Cnvn28Q$jG#`G^L#;DUQLdN=`_Pl%JwBODqFE952zF zRn(&dZo~;pjS7?r)1oA0WH`ctsr4$o3dOBBlP*%2%AwV(9BQ=|Whu-|IZ)AN1Qd<( zVmgGvZV*UJtyDn}Y$!`RDAFKjR6~S8z^$nirj9}hv%s;q0YcTNBv1@Pi-2MrF(e@Y zAl9&$3XXu2YdI^0Y1A{+YSe^_l7Sa3G%tI&6RhD4oXI3o5*DFBC9?n#BBnzbj&3z z90gqVF$p+JiBK$~flmialb8lDPU5_L3!}kE zt>hupLZw4)OfysU3L0423L+{Jl$9cE5~fqh#}sh6qyi#22&}c0P=ET7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/previewable.pdf b/spec/avatar.pdf similarity index 100% rename from spec/previewable.pdf rename to spec/avatar.pdf diff --git a/spec/internal_spec.rb b/spec/internal_spec.rb index 1760c8c..6e85125 100644 --- a/spec/internal_spec.rb +++ b/spec/internal_spec.rb @@ -15,10 +15,19 @@ ) end end - let(:user_with_previewable_image) do + let(:user_with_video) do User.create.tap do |user| user.avatar.attach( - io: File.open("spec/previewable.pdf"), + io: File.open("spec/avatar.mp4"), + filename: "avatar.png", + content_type: "video/mp4" + ) + end + end + let(:user_with_pdf) do + User.create.tap do |user| + user.avatar.attach( + io: File.open("spec/avatar.pdf"), filename: "avatar.pdf", content_type: "application/pdf" ) @@ -65,10 +74,18 @@ it { is_expected.to include(' true}, **options - end -end - -ActiveStorage.previewers << DummyPDFPreviewer - # Run activestorage migrations active_storage_path = Gem::Specification.find_by_name("activestorage").gem_dir ActiveRecord::MigrationContext.new(