Commit d1b36447093bfd8623ba838a123014a7d227bbd8
Exists in
master
and in
22 other branches
Merge commit 'refs/merge-requests/373' of git://gitorious.org/noosfero/noosfero …
…into merge-requests/373
Showing
3 changed files
with
44 additions
and
3 deletions
Show diff stats
app/models/uploaded_file.rb
| ... | ... | @@ -41,7 +41,25 @@ class UploadedFile < Article |
| 41 | 41 | end |
| 42 | 42 | |
| 43 | 43 | def self.max_size |
| 44 | - UploadedFile.attachment_options[:max_size] | |
| 44 | + default = 5.megabytes | |
| 45 | + | |
| 46 | + multipliers = { | |
| 47 | + :KB => :kilobytes, | |
| 48 | + :MB => :megabytes, | |
| 49 | + :GB => :gigabytes, | |
| 50 | + :TB => :terabytes, | |
| 51 | + } | |
| 52 | + max_upload_size = NOOSFERO_CONF['max_upload_size'] | |
| 53 | + | |
| 54 | + if max_upload_size =~ /^(\d+(\.\d+)?)\s*(KB|MB|GB|TB)?$/ | |
| 55 | + number = $1.to_f | |
| 56 | + unit = $3 || :MB | |
| 57 | + multiplier = multipliers[unit.to_sym] | |
| 58 | + | |
| 59 | + number.send(multiplier).to_i | |
| 60 | + else | |
| 61 | + default | |
| 62 | + end | |
| 45 | 63 | end |
| 46 | 64 | |
| 47 | 65 | # FIXME need to define min/max file size |
| ... | ... | @@ -52,9 +70,9 @@ class UploadedFile < Article |
| 52 | 70 | has_attachment :storage => :file_system, |
| 53 | 71 | :thumbnails => { :icon => [24,24], :thumb => '130x130>', :slideshow => '320x240>', :display => '640X480>' }, |
| 54 | 72 | :thumbnail_class => Thumbnail, |
| 55 | - :max_size => 5.megabytes # remember to update validate message below | |
| 73 | + :max_size => self.max_size | |
| 56 | 74 | |
| 57 | - validates_attachment :size => N_("%{fn} of uploaded file was larger than the maximum size of 5.0 MB").fix_i18n | |
| 75 | + validates_attachment :size => N_("%{fn} of uploaded file was larger than the maximum size of %{size}").sub('%{size}', self.max_size.to_humanreadable).fix_i18n | |
| 58 | 76 | |
| 59 | 77 | delay_attachment_fu_thumbnails |
| 60 | 78 | ... | ... |
config/noosfero.yml.dist
test/unit/uploaded_file_test.rb
| ... | ... | @@ -335,4 +335,26 @@ class UploadedFileTest < ActiveSupport::TestCase |
| 335 | 335 | assert_equal 1, ActionTracker::Record.find_all_by_verb('upload_image').count |
| 336 | 336 | end |
| 337 | 337 | |
| 338 | + { | |
| 339 | + nil => 5.megabytes, # default | |
| 340 | + '1KB' => 1.kilobytes, | |
| 341 | + '2MB' => 2.megabyte, | |
| 342 | + '3GB' => 3.gigabytes, | |
| 343 | + '4TB' => 4.terabytes, | |
| 344 | + '6 MB' => 6.megabytes, # allow whitespace between number and unit | |
| 345 | + '0.5 GB' => 512.megabytes, # allow floating point numbers | |
| 346 | + '2' => 2.megabytes, # assume MB as unit by default | |
| 347 | + 'INVALID' => 5.megabytes, # use default for invalid input | |
| 348 | + '1ZYX' => 5.megabytes, # use default for invalid input | |
| 349 | + }.each do |input,output| | |
| 350 | + test 'maximum upload size: convert %s into %s' % [input, output] do | |
| 351 | + NOOSFERO_CONF.expects(:[]).with('max_upload_size').returns(input) | |
| 352 | + assert_equal output, UploadedFile.max_size | |
| 353 | + end | |
| 354 | + end | |
| 355 | + test 'max_size should always return an integer' do | |
| 356 | + NOOSFERO_CONF.expects(:[]).with('max_upload_size').returns("0.5 GB") | |
| 357 | + assert_instance_of Fixnum, UploadedFile.max_size | |
| 358 | + end | |
| 359 | + | |
| 338 | 360 | end | ... | ... |