OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Fix filesize validation on prepared uploads
Oliver Güntherb94b0e630d2a
diff --git a/app/models/attachment.rb b/app/models/attachment.rbindex 5d24495e59f..89b844f4bc8 100644--- a/app/models/attachment.rb+++ b/app/models/attachment.rb@@ -262,7 +262,7 @@ class Attachment < ApplicationRecordprivatedef filesize_below_allowed_maximum- if filesize > Setting.attachment_max_size.to_i.kilobytes+ if filesize.to_i > Setting.attachment_max_size.to_i.kilobyteserrors.add(:file, :file_too_large, count: Setting.attachment_max_size.to_i.kilobytes)endenddiff --git a/app/services/attachments/prepare_upload_service.rb b/app/services/attachments/prepare_upload_service.rbindex 0f7a9546d65..abc1a55c5b6 100644--- a/app/services/attachments/prepare_upload_service.rb+++ b/app/services/attachments/prepare_upload_service.rb@@ -26,39 +26,23 @@# See docs/COPYRIGHT.rdoc for more details.#++-class Attachments::PrepareUploadService < ::BaseServices::Create- def instance(params)- binding.pry- super(params).tap do |attachment|- # We need to do it like this because `file` is an uploader which expects a File (not a string)- # to upload usually. But in this case the data has already been uploaded and we just point to it.- attachment[:file] = pending_direct_upload_filename(params[:filename])+module Attachments+ class PrepareUploadService < ::BaseServices::Create+ def persist(call)+ attachment = call.result- attachment.extend(OpenProject::ChangedBySystem)- attachment.change_by_system do- attachment.downloads = -1- attachment.content_type = params[:content_type] || 'application/octet-stream'+ if attachment.save+ attachment.reload # necessary so that the fog file uploader path is correct+ ServiceResult.new success: true, result: attachment+ else+ ServiceResult.new success: false, result: attachmentendend- end- def persist(call)- attachment = call.result+ private- if attachment.save- attachment.reload # necessary so that the fog file uploader path is correct- ServiceResult.new success: true, result: attachment- else- ServiceResult.new success: false, result: attachment+ def attributes_service_class+ SetPreparedAttributesServiceendend-- private-- # The name has to be in the same format as what Carrierwave will produce later on. If they are different,- # Carrierwave will alter the name (both local and remote) whenever the attachment is saved with the remote- # file loaded.- def pending_direct_upload_filename(filename)- CarrierWave::SanitizedFile.new(nil).send(:sanitize, filename)- endenddiff --git a/app/services/attachments/set_prepared_attributes_service.rb b/app/services/attachments/set_prepared_attributes_service.rbnew file mode 100644index 00000000000..b97f25ac3ff--- /dev/null+++ b/app/services/attachments/set_prepared_attributes_service.rb@@ -0,0 +1,64 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++module Attachments+ class SetPreparedAttributesService < SetAttributesService+ private++ def set_attributes(params)+ super++ set_prepared_attributes params+ end++ def set_prepared_attributes(params)+ # We need to do it like this because `file` is an uploader which expects a File (not a string)+ # to upload usually. But in this case the data has already been uploaded and we just point to it.+ model[:file] = pending_direct_upload_filename(params[:filename])++ # Explicitly set the filesize from metadata+ # as the provided file is not actually uploaded+ model.filesize = params[:filesize]++ model.extend(OpenProject::ChangedBySystem)+ model.change_by_system do+ model.downloads = -1+ model.content_type = params[:content_type] || 'application/octet-stream'+ end+ end++ # The name has to be in the same format as what Carrierwave will produce later on. If they are different,+ # Carrierwave will alter the name (both local and remote) whenever the attachment is saved with the remote+ # file loaded.+ def pending_direct_upload_filename(filename)+ CarrierWave::SanitizedFile.new(nil).send(:sanitize, filename)+ end+ end+enddiff --git a/spec/requests/api/v3/attachments/attachment_resource_shared_examples.rb b/spec/requests/api/v3/attachments/attachment_resource_shared_examples.rbindex 24040b2d9d6..6926c64c92e 100644--- a/spec/requests/api/v3/attachments/attachment_resource_shared_examples.rb+++ b/spec/requests/api/v3/attachments/attachment_resource_shared_examples.rb@@ -69,10 +69,15 @@ shared_examples 'it supports direct uploads' docontext 'with no filesize metadata' dolet(:metadata) { { fileName: 'cat.png' } }+ let(:json) { JSON.parse subject.body }it 'should respond with 422 due to missing file size metadata' doexpect(subject.status).to eq(422)- expect(subject.body).to include 'fileSize'+ expect(subject.body).to include 'Size'+ end++ it_behaves_like 'constraint violation' do+ let(:message) { "Size #{I18n.t('activerecord.errors.messages.blank')}" }endend