Skip to content

Commit

Permalink
Add imgproxy_pro_active_storage route
Browse files Browse the repository at this point in the history
  • Loading branch information
DarthSim committed Dec 22, 2023
1 parent 0fcf377 commit b926c38
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master

- Added `:imgproxy_pro_active_storage` route that uses imgproxy Pro for video and PDF preview generation. ([@DarthSim][])

## 0.2.0 (2023-10-26)

- Improved `resize_and_pad` support. ([@palkan][])
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ config.active_storage.resolve_model_to_route = :rails_storage_proxy

# production.rb
config.active_storage.resolve_model_to_route = :imgproxy_active_storage
# imgproxy Pro users can use the :imgproxy_pro_active_storage route to use
# imgproxy Pro for video and PDF preview generation
config.active_storage.resolve_model_to_route = :imgproxy_pro_active_storage
```

The following HTML snippet will generate different URLs in dev and prod:
Expand Down
10 changes: 10 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,14 @@
route_for(:rails_storage_proxy, model, options)
end
end

direct :imgproxy_pro_active_storage do |model, options|
if ImgproxyRails::Helpers.image_variation?(model) ||
ImgproxyRails::Helpers.previewable_variation?(model)
transformations = model.variation.transformations
Imgproxy.url_for(model.blob, ImgproxyRails::Transformer.call(transformations))
else
route_for(:rails_storage_proxy, model, options)
end
end
end
7 changes: 7 additions & 0 deletions lib/imgproxy-rails/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,12 @@ def self.image_variation?(model)
model.respond_to?(:variation) &&
model.try(:blob)&.content_type&.split("/")&.first == "image"
end

def self.previewable_variation?(model)
return false if !model.respond_to?(:variation)

content_type = model.try(:blob)&.content_type
content_type&.start_with?("video/") || content_type == "application/pdf"
end
end
end
Binary file added spec/avatar.mp4
Binary file not shown.
File renamed without changes.
39 changes: 35 additions & 4 deletions spec/internal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -65,8 +74,14 @@
it { is_expected.to include('<img src="http://example.com', "blobs") }
end

context "when record is previewable" do
let(:record) { user_with_previewable_image.avatar.preview(resize_to_limit: [500, 500]) }
context "when record is video" do
let(:record) { user_with_video.avatar.preview(resize_to_limit: [500, 500]) }

it { is_expected.to include('<img src="http://example.com', "representations") }
end

context "when record is PDF" do
let(:record) { user_with_pdf.avatar.preview(resize_to_limit: [500, 500]) }

it { is_expected.to include('<img src="http://example.com', "representations") }
end
Expand Down Expand Up @@ -102,6 +117,22 @@ def bucket
it { is_expected.to include('<img src="http://imgproxy.io', "s3://test-bucket", record.blob.key) }
end
end

describe "with resolve_model_to_route = :imgproxy_pro_active_storage" do
before { ActiveStorage.resolve_model_to_route = :imgproxy_pro_active_storage }

context "when record is video" do
let(:record) { user_with_video.avatar.preview(resize_to_limit: [500, 500]) }

it { is_expected.to include('<img src="http://imgproxy.io', "blobs") }
end

context "when record is PDF" do
let(:record) { user_with_pdf.avatar.preview(resize_to_limit: [500, 500]) }

it { is_expected.to include('<img src="http://imgproxy.io', "blobs") }
end
end
end

describe "url helpers" do
Expand Down

0 comments on commit b926c38

Please sign in to comment.