Skip to content

Commit

Permalink
Detect images properly in FileUpload and MultipleFileUpload field
Browse files Browse the repository at this point in the history
  • Loading branch information
mshibuya committed Aug 25, 2024
1 parent 13114e5 commit 50f52c4
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/rails_admin/config/fields/types/file_upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class FileUpload < RailsAdmin::Config::Fields::Base
end

register_instance_option :image? do
mime_type = Mime::Type.lookup_by_extension(resource_url.to_s.split('.').last)
mime_type = Mime::Type.lookup_by_extension(extension)
mime_type.to_s.match?(/^image/)
end

Expand All @@ -66,6 +66,12 @@ class FileUpload < RailsAdmin::Config::Fields::Base
}
end

def extension
URI.parse(resource_url).path.split('.').last
rescue URI::InvalidURIError
nil
end

# virtual class
def resource_url
raise 'not implemented'
Expand Down
8 changes: 7 additions & 1 deletion lib/rails_admin/config/fields/types/multiple_file_upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ def initialize(value)
end

register_instance_option :image? do
mime_type = Mime::Type.lookup_by_extension(resource_url.to_s.split('.').last)
mime_type = Mime::Type.lookup_by_extension(extension)
mime_type.to_s.match?(/^image/)
end

def resource_url(_thumb = false)
raise 'not implemented'
end

def extension
URI.parse(resource_url).path.split('.').last
rescue URI::InvalidURIError
nil
end
end

def initialize(*args)
Expand Down
53 changes: 53 additions & 0 deletions spec/rails_admin/config/fields/types/file_upload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,57 @@ def resource_url
end
end
end

describe '#image?' do
let(:filename) { 'dummy.txt' }
let :rails_admin_field do
RailsAdmin.config('FieldTest').fields.detect do |f|
f.name == :string_field
end.with(
object: FieldTest.new(string_field: filename),
view: ApplicationController.new.view_context,
)
end
before do
RailsAdmin.config FieldTest do
field :string_field, :file_upload do
def resource_url
"http://example.com/#{value}"
end
end
end
end

context 'when the file is not an image' do
let(:filename) { 'dummy.txt' }

it 'returns false' do
expect(rails_admin_field.image?).to be false
end
end

context 'when the file is an image' do
let(:filename) { 'dummy.jpg' }

it 'returns true' do
expect(rails_admin_field.image?).to be true
end
end

context 'when the file is an image but suffixed with a query string' do
let(:filename) { 'dummy.jpg?foo=bar' }

it 'returns true' do
expect(rails_admin_field.image?).to be true
end
end

context "when the filename can't be represented as a valid URI" do
let(:filename) { 'du mmy.jpg' }

it 'returns false' do
expect(rails_admin_field.image?).to be false
end
end
end
end
55 changes: 55 additions & 0 deletions spec/rails_admin/config/fields/types/multiple_file_upload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,59 @@ def value
expect(rails_admin_field.with(object: FieldTest.new(string_field: 'dummy.txt')).attachments.map(&:value)).to eq ['dummy.txt']
end
end

describe '#image?' do
let(:filename) { 'dummy.txt' }
let :rails_admin_field do
RailsAdmin.config('FieldTest').fields.detect do |f|
f.name == :string_field
end.with(
object: FieldTest.new(string_field: filename),
view: ApplicationController.new.view_context,
)
end
before do
RailsAdmin.config FieldTest do
field :string_field, :multiple_file_upload do
attachment do
def resource_url
"http://example.com/#{value}"
end
end
end
end
end

context 'when the file is not an image' do
let(:filename) { 'dummy.txt' }

it 'returns false' do
expect(rails_admin_field.attachments.first.image?).to be false
end
end

context 'when the file is an image' do
let(:filename) { 'dummy.jpg' }

it 'returns true' do
expect(rails_admin_field.attachments.first.image?).to be true
end
end

context 'when the file is an image but suffixed with a query string' do
let(:filename) { 'dummy.jpg?foo=bar' }

it 'returns true' do
expect(rails_admin_field.attachments.first.image?).to be true
end
end

context "when the filename can't be represented as a valid URI" do
let(:filename) { 'du mmy.jpg' }

it 'returns false' do
expect(rails_admin_field.attachments.first.image?).to be false
end
end
end
end

0 comments on commit 50f52c4

Please sign in to comment.