summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGo MAEDA <maeda@farend.jp>2021-08-15 04:19:30 +0000
committerGo MAEDA <maeda@farend.jp>2021-08-15 04:19:30 +0000
commit41ca940f66e6f683d45f78dbb63e89f5a17e1e80 (patch)
tree654f47a1e0f95d4cd82245894aa6c8556c111ddf
parent2af371526c89d9c5b3d1d435188d2b2473eafa4c (diff)
downloadredmine-41ca940f66e6f683d45f78dbb63e89f5a17e1e80.tar.gz
redmine-41ca940f66e6f683d45f78dbb63e89f5a17e1e80.zip
Merged r21173 from trunk to 4.1-stable (#35715).
git-svn-id: http://svn.redmine.org/redmine/branches/4.1-stable@21175 e93f8b46-1217-0410-a6f0-8f06a7374b81
-rw-r--r--app/controllers/attachments_controller.rb12
-rw-r--r--test/integration/api_test/attachments_test.rb25
2 files changed, 36 insertions, 1 deletions
diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb
index c2f6481f3..366bad58b 100644
--- a/app/controllers/attachments_controller.rb
+++ b/app/controllers/attachments_controller.rb
@@ -101,7 +101,7 @@ class AttachmentsController < ApplicationController
return
end
- @attachment = Attachment.new(:file => request.body)
+ @attachment = Attachment.new(:file => raw_request_body)
@attachment.author = User.current
@attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16)
@attachment.content_type = params[:content_type].presence
@@ -265,4 +265,14 @@ class AttachmentsController < ApplicationController
def update_all_params
params.permit(:attachments => [:filename, :description]).require(:attachments)
end
+
+ # Get an IO-like object for the request body which is usable to create a new
+ # attachment. We try to avoid having to read the whole body into memory.
+ def raw_request_body
+ if request.body.respond_to?(:size)
+ request.body
+ else
+ request.raw_post
+ end
+ end
end
diff --git a/test/integration/api_test/attachments_test.rb b/test/integration/api_test/attachments_test.rb
index 7b4d60b80..6f3972ca5 100644
--- a/test/integration/api_test/attachments_test.rb
+++ b/test/integration/api_test/attachments_test.rb
@@ -251,4 +251,29 @@ class Redmine::ApiTest::AttachmentsTest < Redmine::ApiTest::Base
assert_equal 12, attachment.filesize
assert File.exist? attachment.diskfile
end
+
+ test "POST /uploads.json should be compatible with a uwsgi's input" do
+ set_tmp_attachments_directory
+ assert_difference 'Attachment.count' do
+ request_body = Rack::RewindableInput.new(StringIO.new('File content'))
+ # Uwsgi_IO object does not have size method
+ request_body.instance_eval('undef :size', __FILE__, __LINE__)
+ post(
+ '/uploads.json',
+ :headers => {
+ "CONTENT_TYPE" => 'application/octet-stream',
+ "CONTENT_LENGTH" => '12',
+ "rack.input" => request_body
+ }.merge(credentials('jsmith'))
+ )
+ assert_response :created
+ end
+ json = ActiveSupport::JSON.decode(response.body)
+ assert_kind_of Hash, json['upload']
+ token = json['upload']['token']
+ assert token.present?
+ assert attachment = Attachment.find_by_token(token)
+ assert_equal 12, attachment.filesize
+ assert File.exist? attachment.diskfile
+ end
end