Skip to content

Commit 35c8702

Browse files
committed
Detect images properly in FileUpload and MultipleFileUpload field
Closes #3633
1 parent 13114e5 commit 35c8702

File tree

4 files changed

+122
-2
lines changed

4 files changed

+122
-2
lines changed

lib/rails_admin/config/fields/types/file_upload.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class FileUpload < RailsAdmin::Config::Fields::Base
5252
end
5353

5454
register_instance_option :image? do
55-
mime_type = Mime::Type.lookup_by_extension(resource_url.to_s.split('.').last)
55+
mime_type = Mime::Type.lookup_by_extension(extension)
5656
mime_type.to_s.match?(/^image/)
5757
end
5858

@@ -66,6 +66,12 @@ class FileUpload < RailsAdmin::Config::Fields::Base
6666
}
6767
end
6868

69+
def extension
70+
URI.parse(resource_url).path.split('.').last
71+
rescue URI::InvalidURIError
72+
nil
73+
end
74+
6975
# virtual class
7076
def resource_url
7177
raise 'not implemented'

lib/rails_admin/config/fields/types/multiple_file_upload.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,19 @@ def initialize(value)
4747
end
4848

4949
register_instance_option :image? do
50-
mime_type = Mime::Type.lookup_by_extension(resource_url.to_s.split('.').last)
50+
mime_type = Mime::Type.lookup_by_extension(extension)
5151
mime_type.to_s.match?(/^image/)
5252
end
5353

5454
def resource_url(_thumb = false)
5555
raise 'not implemented'
5656
end
57+
58+
def extension
59+
URI.parse(resource_url).path.split('.').last
60+
rescue URI::InvalidURIError
61+
nil
62+
end
5763
end
5864

5965
def initialize(*args)

spec/rails_admin/config/fields/types/file_upload_spec.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,57 @@ def resource_url
8383
end
8484
end
8585
end
86+
87+
describe '#image?' do
88+
let(:filename) { 'dummy.txt' }
89+
let :rails_admin_field do
90+
RailsAdmin.config('FieldTest').fields.detect do |f|
91+
f.name == :string_field
92+
end.with(
93+
object: FieldTest.new(string_field: filename),
94+
view: ApplicationController.new.view_context,
95+
)
96+
end
97+
before do
98+
RailsAdmin.config FieldTest do
99+
field :string_field, :file_upload do
100+
def resource_url
101+
"http://example.com/#{value}"
102+
end
103+
end
104+
end
105+
end
106+
107+
context 'when the file is not an image' do
108+
let(:filename) { 'dummy.txt' }
109+
110+
it 'returns false' do
111+
expect(rails_admin_field.image?).to be false
112+
end
113+
end
114+
115+
context 'when the file is an image' do
116+
let(:filename) { 'dummy.jpg' }
117+
118+
it 'returns true' do
119+
expect(rails_admin_field.image?).to be true
120+
end
121+
end
122+
123+
context 'when the file is an image but suffixed with a query string' do
124+
let(:filename) { 'dummy.jpg?foo=bar' }
125+
126+
it 'returns true' do
127+
expect(rails_admin_field.image?).to be true
128+
end
129+
end
130+
131+
context "when the filename can't be represented as a valid URI" do
132+
let(:filename) { 'du mmy.jpg' }
133+
134+
it 'returns false' do
135+
expect(rails_admin_field.image?).to be false
136+
end
137+
end
138+
end
86139
end

spec/rails_admin/config/fields/types/multiple_file_upload_spec.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,59 @@ def value
146146
expect(rails_admin_field.with(object: FieldTest.new(string_field: 'dummy.txt')).attachments.map(&:value)).to eq ['dummy.txt']
147147
end
148148
end
149+
150+
describe '#image?' do
151+
let(:filename) { 'dummy.txt' }
152+
let :rails_admin_field do
153+
RailsAdmin.config('FieldTest').fields.detect do |f|
154+
f.name == :string_field
155+
end.with(
156+
object: FieldTest.new(string_field: filename),
157+
view: ApplicationController.new.view_context,
158+
)
159+
end
160+
before do
161+
RailsAdmin.config FieldTest do
162+
field :string_field, :multiple_file_upload do
163+
attachment do
164+
def resource_url
165+
"http://example.com/#{value}"
166+
end
167+
end
168+
end
169+
end
170+
end
171+
172+
context 'when the file is not an image' do
173+
let(:filename) { 'dummy.txt' }
174+
175+
it 'returns false' do
176+
expect(rails_admin_field.attachments.first.image?).to be false
177+
end
178+
end
179+
180+
context 'when the file is an image' do
181+
let(:filename) { 'dummy.jpg' }
182+
183+
it 'returns true' do
184+
expect(rails_admin_field.attachments.first.image?).to be true
185+
end
186+
end
187+
188+
context 'when the file is an image but suffixed with a query string' do
189+
let(:filename) { 'dummy.jpg?foo=bar' }
190+
191+
it 'returns true' do
192+
expect(rails_admin_field.attachments.first.image?).to be true
193+
end
194+
end
195+
196+
context "when the filename can't be represented as a valid URI" do
197+
let(:filename) { 'du mmy.jpg' }
198+
199+
it 'returns false' do
200+
expect(rails_admin_field.attachments.first.image?).to be false
201+
end
202+
end
203+
end
149204
end

0 commit comments

Comments
 (0)