Project

General

Profile

Defect #35715 » 35715-v2.patch

Go MAEDA, 2021-08-13 02:55

View differences:

app/controllers/attachments_controller.rb
105 105
      return
106 106
    end
107 107

  
108
    @attachment = Attachment.new(:file => request.body)
108
    @attachment = Attachment.new(:file => raw_request_body)
109 109
    @attachment.author = User.current
110 110
    @attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16)
111 111
    @attachment.content_type = params[:content_type].presence
......
303 303
  def update_all_params
304 304
    params.permit(:attachments => [:filename, :description]).require(:attachments)
305 305
  end
306

  
307
  # Get an IO-like object for the request body which is usable to create a new
308
  # attachment. We try to avoid having to read the whole body into memory.
309
  def raw_request_body
310
    if request.body.respond_to?(:size)
311
      request.body
312
    else
313
      request.raw_post
314
    end
315
  end
306 316
end
test/integration/api_test/attachments_test.rb
272 272
    assert_equal 12, attachment.filesize
273 273
    assert File.exist? attachment.diskfile
274 274
  end
275

  
276
  test "POST /uploads.json should be compatible with a uwsgi's input" do
277
    set_tmp_attachments_directory
278
    assert_difference 'Attachment.count' do
279
      request_body = Rack::RewindableInput.new(StringIO.new('File content'))
280
      # Uwsgi_IO object does not have size method
281
      request_body.instance_eval('undef :size', __FILE__, __LINE__)
282
      post(
283
        '/uploads.json',
284
        :headers => {
285
          "CONTENT_TYPE" => 'application/octet-stream',
286
          "CONTENT_LENGTH" => '12',
287
          "rack.input" => request_body
288
        }.merge(credentials('jsmith'))
289
      )
290
      assert_response :created
291
    end
292
    json = ActiveSupport::JSON.decode(response.body)
293
    assert_kind_of Hash, json['upload']
294
    token = json['upload']['token']
295
    assert token.present?
296
    assert attachment = Attachment.find_by_token(token)
297
    assert_equal 12, attachment.filesize
298
    assert File.exist? attachment.diskfile
299
  end
275 300
end
(2-2/2)