]> source.dussan.org Git - redmine.git/commitdiff
Uploading a big file fails with NoMemoryError (#33752).
authorGo MAEDA <maeda@farend.jp>
Tue, 11 May 2021 05:35:19 +0000 (05:35 +0000)
committerGo MAEDA <maeda@farend.jp>
Tue, 11 May 2021 05:35:19 +0000 (05:35 +0000)
Patch by Karel Pičman and Pavel Rosický.

git-svn-id: http://svn.redmine.org/redmine/trunk@20993 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/attachments_controller.rb
config/initializers/10-patches.rb
test/integration/api_test/attachments_test.rb

index 1956f01e34178ca26c59171a5607279743eb55b6..8e28194a302f9bdb4c37d26f4587eaceba7e5d02 100644 (file)
@@ -105,7 +105,7 @@ class AttachmentsController < ApplicationController
       return
     end
 
-    @attachment = Attachment.new(:file => request.raw_post)
+    @attachment = Attachment.new(:file => request.body)
     @attachment.author = User.current
     @attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16)
     @attachment.content_type = params[:content_type].presence
index cc6113d48f449656315a05f7978f3dd9f94b84c1..643d06b7ea6a4615d45117f4f4bd8ce7decc8955 100644 (file)
@@ -215,3 +215,17 @@ module ActionView
     end
   end
 end
+
+# https://github.com/rack/rack/pull/1703
+# TODO: remove this when Rack is updated to 3.0.0
+require 'rack'
+module Rack
+  class RewindableInput
+    unless method_defined?(:size)
+      def size
+        make_rewindable unless @rewindable_io
+        @rewindable_io.size
+      end
+    end
+  end
+end
index 545dd81f2848e51c7e282d91296b91790a0161fb..538649950f7544f28e65608709b6b45d175ab28d 100644 (file)
@@ -250,4 +250,26 @@ class Redmine::ApiTest::AttachmentsTest < Redmine::ApiTest::Base
     assert attachment.digest.present?
     assert File.exist? attachment.diskfile
   end
+
+  test "POST /uploads.json should be compatible with an fcgi's input" do
+    set_tmp_attachments_directory
+    assert_difference 'Attachment.count' do
+      post(
+        '/uploads.json',
+        :headers => {
+          "CONTENT_TYPE" => 'application/octet-stream',
+          "CONTENT_LENGTH" => '12',
+          "rack.input" => Rack::RewindableInput.new(StringIO.new('File content'))
+        }.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