Skip to content

Commit

Permalink
Merge pull request #5873 from avalonmediasystem/no_no_limit
Browse files Browse the repository at this point in the history
Add max_upload_size to config/settings.yml and all for disabling limit
  • Loading branch information
cjcolvar authored Jun 17, 2024
2 parents 98e0a1e + 8a20bf4 commit 98fee19
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 17 deletions.
8 changes: 2 additions & 6 deletions app/models/master_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,8 @@ def error

after_processing :post_processing_file_management

# First and simplest test - make sure that the uploaded file does not exceed the
# limits of the system. For now this is hard coded but should probably eventually
# be set up in a configuration file somewhere
#
# 250 MB is the file limit for now
MAXIMUM_UPLOAD_SIZE = Settings.max_upload_size || 2.gigabytes
# Make sure that the uploaded file does not exceed the limits of the system
MAXIMUM_UPLOAD_SIZE = Settings.max_upload_size

WORKFLOWS = ['fullaudio', 'avalon', 'pass_through', 'avalon-skip-transcoding', 'avalon-skip-transcoding-audio'].freeze
AUDIO_TYPES = ["audio/vnd.wave", "audio/mpeg", "audio/mp3", "audio/mp4", "audio/wav", "audio/x-wav"]
Expand Down
2 changes: 1 addition & 1 deletion app/services/master_file_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def self.create_upload_notice(format)
module FileUpload
def self.build(params)
params[:Filedata].collect do |file|
if (file.size > MasterFile::MAXIMUM_UPLOAD_SIZE)
if (MasterFile::MAXIMUM_UPLOAD_SIZE.is_a? Numeric) && (file.size > MasterFile::MAXIMUM_UPLOAD_SIZE)
raise BuildError, "The file you have uploaded is too large"
end
Spec.new(file, file.original_filename, file.content_type, params[:workflow])
Expand Down
6 changes: 5 additions & 1 deletion app/views/media_objects/_file_upload.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,11 @@ Unless required by applicable law or agreed to in writing, software distributed
<div class="row">
<div class="col-sm-7">
<!-- web-upload -->
<p class="muted">Upload through the web (files must not exceed <%= number_to_human_size MasterFile::MAXIMUM_UPLOAD_SIZE %>)</p>
<% if MasterFile::MAXIMUM_UPLOAD_SIZE.is_a? Numeric %>
<p class="muted">Upload through the web (files must not exceed <%= number_to_human_size MasterFile::MAXIMUM_UPLOAD_SIZE %>)</p>
<% else %>
<p class="muted">Upload through the web</p>
<% end %>
<fieldset id='uploader'>
<%= form_tag(master_files_path, :enctype=>"multipart/form-data", class: upload_form_classes, data: upload_form_data) do -%>
<input type="hidden" name="container_id" value="<%= @media_object.id %>" />
Expand Down
2 changes: 2 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,5 @@ recaptcha:
derivative:
# Choose whether collection managers and admins can download high quality derivatives
allow_download: true
# Maximum size for uploaded files in bytes (default is disabled)
#max_upload_size: 2147483648 # Use :none or comment out to disable limit
7 changes: 7 additions & 0 deletions spec/controllers/master_files_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@
end

context "cannot upload a file over the defined limit" do
around do |example|
@old_maximum_upload_size = MasterFile::MAXIMUM_UPLOAD_SIZE
MasterFile::MAXIMUM_UPLOAD_SIZE = 2.gigabytes
example.run
MasterFile::MAXIMUM_UPLOAD_SIZE = @old_maximum_upload_size
end

it "provides a warning about the file size" do
request.env["HTTP_REFERER"] = "/"

Expand Down
34 changes: 25 additions & 9 deletions spec/services/master_file_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,33 @@
end

describe 'FileUpload.build' do
it "should raise error if file is too big" do
file = double("file", size: MasterFile::MAXIMUM_UPLOAD_SIZE + 1)
params = { Filedata: [file] }
expect { MasterFileBuilder::FileUpload.build(params) }.to raise_error MasterFileBuilder::BuildError
context "with upload limit enabled" do
around do |example|
@old_maximum_upload_size = MasterFile::MAXIMUM_UPLOAD_SIZE
MasterFile::MAXIMUM_UPLOAD_SIZE = 2.gigabytes
example.run
MasterFile::MAXIMUM_UPLOAD_SIZE = @old_maximum_upload_size
end

it "should raise error if file is too big" do
file = double("file", size: MasterFile::MAXIMUM_UPLOAD_SIZE + 1)
params = { Filedata: [file] }
expect { MasterFileBuilder::FileUpload.build(params) }.to raise_error MasterFileBuilder::BuildError
end

it "should return a Spec for legit file" do
file = double("file", size: MasterFile::MAXIMUM_UPLOAD_SIZE - 1, original_filename: "aname", content_type: "mp4")
params = { Filedata: [file], workflow: double("workflow") }
s = MasterFileBuilder::FileUpload.build(params)
expect(s).to eq [MasterFileBuilder::Spec.new(file, file.original_filename, file.content_type, params[:workflow])]
end
end

it "should return a Spec for legit file" do
file = double("file", size: MasterFile::MAXIMUM_UPLOAD_SIZE - 1, original_filename: "aname", content_type: "mp4")
params = { Filedata: [file], workflow: double("workflow") }
s = MasterFileBuilder::FileUpload.build(params)
expect(s).to eq [MasterFileBuilder::Spec.new(file, file.original_filename, file.content_type, params[:workflow])]
it "should not raise error when no limit" do
expect(MasterFile::MAXIMUM_UPLOAD_SIZE.is_a? Numeric).to eq false
file = double("file", size: 2.gigabytes + 1)
params = { Filedata: [file] }
expect { MasterFileBuilder::FileUpload.build(params) }.not_to raise_error MasterFileBuilder::BuildError
end
end

Expand Down

0 comments on commit 98fee19

Please sign in to comment.