From 654f17a944eeda67088ceee8b5de763a6f7fda06 Mon Sep 17 00:00:00 2001 From: Antonio Terceiro Date: Sun, 15 Sep 2013 01:10:53 -0300 Subject: [PATCH] UploadedFile: support site-wide configuration of max upload size --- app/models/uploaded_file.rb | 24 +++++++++++++++++++++--- config/noosfero.yml.dist | 1 + test/unit/uploaded_file_test.rb | 22 ++++++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/app/models/uploaded_file.rb b/app/models/uploaded_file.rb index 5be009e..42e757d 100644 --- a/app/models/uploaded_file.rb +++ b/app/models/uploaded_file.rb @@ -41,7 +41,25 @@ class UploadedFile < Article end def self.max_size - UploadedFile.attachment_options[:max_size] + default = 5.megabytes + + multipliers = { + :KB => :kilobytes, + :MB => :megabytes, + :GB => :gigabytes, + :TB => :terabytes, + } + max_upload_size = NOOSFERO_CONF['max_upload_size'] + + if max_upload_size =~ /^(\d+(\.\d+)?)\s*(KB|MB|GB|TB)?$/ + number = $1.to_f + unit = $3 || :MB + multiplier = multipliers[unit.to_sym] + + number.send(multiplier).to_i + else + default + end end # FIXME need to define min/max file size @@ -52,9 +70,9 @@ class UploadedFile < Article has_attachment :storage => :file_system, :thumbnails => { :icon => [24,24], :thumb => '130x130>', :slideshow => '320x240>', :display => '640X480>' }, :thumbnail_class => Thumbnail, - :max_size => 5.megabytes # remember to update validate message below + :max_size => self.max_size - validates_attachment :size => N_("%{fn} of uploaded file was larger than the maximum size of 5.0 MB").fix_i18n + 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 delay_attachment_fu_thumbnails diff --git a/config/noosfero.yml.dist b/config/noosfero.yml.dist index 5a9fcd1..6a09724 100644 --- a/config/noosfero.yml.dist +++ b/config/noosfero.yml.dist @@ -8,6 +8,7 @@ development: gravatar: wavatar googlemaps_initial_zoom: 4 exception_recipients: [admin@example.com] + max_upload_size: 5MB test: diff --git a/test/unit/uploaded_file_test.rb b/test/unit/uploaded_file_test.rb index 55f774f..b6b9bfd 100644 --- a/test/unit/uploaded_file_test.rb +++ b/test/unit/uploaded_file_test.rb @@ -352,4 +352,26 @@ class UploadedFileTest < ActiveSupport::TestCase assert_equal 1, ActionTracker::Record.find_all_by_verb('upload_image').count end + { + nil => 5.megabytes, # default + '1KB' => 1.kilobytes, + '2MB' => 2.megabyte, + '3GB' => 3.gigabytes, + '4TB' => 4.terabytes, + '6 MB' => 6.megabytes, # allow whitespace between number and unit + '0.5 GB' => 512.megabytes, # allow floating point numbers + '2' => 2.megabytes, # assume MB as unit by default + 'INVALID' => 5.megabytes, # use default for invalid input + '1ZYX' => 5.megabytes, # use default for invalid input + }.each do |input,output| + test 'maximum upload size: convert %s into %s' % [input, output] do + NOOSFERO_CONF.expects(:[]).with('max_upload_size').returns(input) + assert_equal output, UploadedFile.max_size + end + end + test 'max_size should always return an integer' do + NOOSFERO_CONF.expects(:[]).with('max_upload_size').returns("0.5 GB") + assert_instance_of Fixnum, UploadedFile.max_size + end + end -- libgit2 0.21.2