Commit 88dba088c9d6c6feca16dea139e332a8a2794e28

Authored by Braulio Bhavamitra
1 parent 20c9a186

Protect extension method against nil

app/models/uploaded_file.rb
... ... @@ -67,7 +67,7 @@ class UploadedFile < Article
67 67 'upload-file'
68 68 end
69 69 end
70   -
  70 +
71 71 def mime_type
72 72 content_type
73 73 end
... ... @@ -129,6 +129,12 @@ class UploadedFile < Article
129 129 end
130 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 138 def allow_children?
133 139 false
134 140 end
... ... @@ -144,4 +150,5 @@ class UploadedFile < Article
144 150 def uploaded_file?
145 151 true
146 152 end
  153 +
147 154 end
... ...
app/views/search/_image.rhtml
1 1 <div class="search-image-container">
2 2  
3 3 <% if image.is_a? UploadedFile and image.filename %>
4   - <% extension = image.filename[(image.filename.rindex('.')+1)..-1].downcase %>
  4 + <% extension = image.extension %>
5 5 <% if ['jpg', 'jpeg', 'gif', 'png', 'tiff', 'svg'].include? extension %>
6 6 <%= link_to '', image.view_url, :class => "search-image-pic", :style => 'background-image: url(%s)'% image.public_filename(:thumb) %>
7 7 <% if image.width && image.height %>
... ...
test/unit/uploaded_file_test.rb
... ... @@ -306,6 +306,11 @@ class UploadedFileTest &lt; ActiveSupport::TestCase
306 306 uses_sqlite
307 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 314 should 'upload to path prefix folder if database is not postgresql' do
310 315 uses_sqlite
311 316 file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain'), :profile => @profile)
... ...