Skip to content

Commit

Permalink
Merge pull request #6 from imgproxy/feature/imgproxy_pro_active_storage
Browse files Browse the repository at this point in the history
Add imgproxy_pro_active_storage route
  • Loading branch information
DarthSim authored Dec 22, 2023
2 parents 5a79951 + b5137e7 commit bc0eafb
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 24 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 support for `video/*` and `application/pdf` content types. ([@DarthSim][])

## 0.2.0 (2023-10-26)

- Improved `resize_and_pad` support. ([@palkan][])
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions lib/imgproxy-rails/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Binary file added spec/avatar.mp4
Binary file not shown.
File renamed without changes.
27 changes: 22 additions & 5 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,10 +74,18 @@
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
before { ActiveStorage.variable_content_types << "video/mp4" }
let(:record) { user_with_video.avatar.variant(resize_to_limit: [500, 500]) }

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

context "when record is PDF" do
before { ActiveStorage.variable_content_types << "application/pdf" }
let(:record) { user_with_pdf.avatar.variant(resize_to_limit: [500, 500]) }

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

context "when using short S3 urls with imgproxy", skip: ActiveStorage::VERSION::MAJOR < 7 do
Expand Down
15 changes: 0 additions & 15 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,6 @@ class User < ActiveRecord::Base
Rails.application.routes.default_url_options[:host] = "http://example.com"
Imgproxy.configure { |config| config.endpoint = "http://imgproxy.io" }

# Custom previewer for PDFs to avoid requiring os libs
# Based on: https://github.com/PacktPublishing/Layered-Design-for-Ruby-on-Rails-Applications/blob/main/Chapter03/06-active-storage-dummy-previewer.rb
class DummyPDFPreviewer < ActiveStorage::Previewer
def self.accept?(blob)
blob.content_type == "application/pdf"
end

def preview(**options)
output = File.open(File.join(__dir__, "avatar.png"))
yield io: output, filename: "#{blob.filename.base}.png", content_type: "image/png", metadata: {"dummy" => true}, **options
end
end

ActiveStorage.previewers << DummyPDFPreviewer

# Run activestorage migrations
active_storage_path = Gem::Specification.find_by_name("activestorage").gem_dir
ActiveRecord::MigrationContext.new(
Expand Down

0 comments on commit bc0eafb

Please sign in to comment.