Commit 88dba088c9d6c6feca16dea139e332a8a2794e28
1 parent
20c9a186
Exists in
master
and in
28 other branches
Protect extension method against nil
Showing
3 changed files
with
14 additions
and
2 deletions
Show diff stats
app/models/uploaded_file.rb
@@ -67,7 +67,7 @@ class UploadedFile < Article | @@ -67,7 +67,7 @@ class UploadedFile < Article | ||
67 | 'upload-file' | 67 | 'upload-file' |
68 | end | 68 | end |
69 | end | 69 | end |
70 | - | 70 | + |
71 | def mime_type | 71 | def mime_type |
72 | content_type | 72 | content_type |
73 | end | 73 | end |
@@ -129,6 +129,12 @@ class UploadedFile < Article | @@ -129,6 +129,12 @@ class UploadedFile < Article | ||
129 | end | 129 | end |
130 | end | 130 | end |
131 | 131 | ||
132 | + def extension | ||
133 | + dotindex = self.filename.rindex('.') | ||
134 | + return nil unless dotindex | ||
135 | + self.filename[(dotindex+1)..-1].downcase | ||
136 | + end | ||
137 | + | ||
132 | def allow_children? | 138 | def allow_children? |
133 | false | 139 | false |
134 | end | 140 | end |
@@ -144,4 +150,5 @@ class UploadedFile < Article | @@ -144,4 +150,5 @@ class UploadedFile < Article | ||
144 | def uploaded_file? | 150 | def uploaded_file? |
145 | true | 151 | true |
146 | end | 152 | end |
153 | + | ||
147 | end | 154 | end |
app/views/search/_image.rhtml
1 | <div class="search-image-container"> | 1 | <div class="search-image-container"> |
2 | 2 | ||
3 | <% if image.is_a? UploadedFile and image.filename %> | 3 | <% if image.is_a? UploadedFile and image.filename %> |
4 | - <% extension = image.filename[(image.filename.rindex('.')+1)..-1].downcase %> | 4 | + <% extension = image.extension %> |
5 | <% if ['jpg', 'jpeg', 'gif', 'png', 'tiff', 'svg'].include? extension %> | 5 | <% if ['jpg', 'jpeg', 'gif', 'png', 'tiff', 'svg'].include? extension %> |
6 | <%= link_to '', image.view_url, :class => "search-image-pic", :style => 'background-image: url(%s)'% image.public_filename(:thumb) %> | 6 | <%= link_to '', image.view_url, :class => "search-image-pic", :style => 'background-image: url(%s)'% image.public_filename(:thumb) %> |
7 | <% if image.width && image.height %> | 7 | <% if image.width && image.height %> |
test/unit/uploaded_file_test.rb
@@ -306,6 +306,11 @@ class UploadedFileTest < ActiveSupport::TestCase | @@ -306,6 +306,11 @@ class UploadedFileTest < ActiveSupport::TestCase | ||
306 | uses_sqlite | 306 | uses_sqlite |
307 | end | 307 | end |
308 | 308 | ||
309 | + should 'return extension' do | ||
310 | + file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => @profile) | ||
311 | + assert_equal 'png', file.extension | ||
312 | + end | ||
313 | + | ||
309 | should 'upload to path prefix folder if database is not postgresql' do | 314 | should 'upload to path prefix folder if database is not postgresql' do |
310 | uses_sqlite | 315 | uses_sqlite |
311 | file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain'), :profile => @profile) | 316 | file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain'), :profile => @profile) |