SammyLin / redactor-rails

This repo is deprecated. Please check out official gem for Redactor 2. https://github.com/Redactor2/redactor2_rails
MIT License
389 stars 254 forks source link

Image and File Uploading works only 10% of the time. #102

Closed betjaminrichards closed 10 years ago

betjaminrichards commented 10 years ago

I'm experiencing an intermittent error when uploading images or files (to clarify, it is the same file that it intermittently works with).

Sometimes the upload process works fine and I see this in the logs:

Started POST "/redactor_rails/pictures?authenticity_token=GErdxBU0dPRINvw8ZSj6VfBpPQ7hMbwtL0B4YWu94Mg%3D" for 127.0.0.1 at 2013-11-29 12:30:19 +0100
Processing by RedactorRails::PicturesController#create as HTML
  Parameters: {"file"=>#<ActionDispatch::Http::UploadedFile:0x007f8142445258 @original_filename="fish.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"fish.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/folders/kq/6krgq50x6y3dsx4c4mf19f_40000gn/T/RackMultipart20131129-24880-availq>>, "authenticity_token"=>"GErdxBU0dPRINvw8ZSj6VfBpPQ7hMbwtL0B4YWu94Mg="}
  User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 4 LIMIT 1
   (0.5ms)  BEGIN
  SQL (1.0ms)  INSERT INTO "redactor_assets" ("assetable_id", "assetable_type", "created_at", "data_content_type", "data_file_name", "data_file_size", "height", "type", "updated_at", "user_id", "width") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING "id"  [["assetable_id", 4], ["assetable_type", "User"], ["created_at", Fri, 29 Nov 2013 06:30:21 EST -05:00], ["data_content_type", "image/jpeg"], ["data_file_name", "fish.jpg"], ["data_file_size", 1961898], ["height", 2448], ["type", "RedactorRails::Picture"], ["updated_at", Fri, 29 Nov 2013 06:30:21 EST -05:00], ["user_id", 4], ["width", 3264]]
   (1.9ms)  COMMIT
  Rendered text template (0.0ms)
Completed 200 OK in 5943ms (Views: 1.0ms | ActiveRecord: 4.0ms)

Other times in the upload hangs and I see this in the logs:

Started POST "/redactor_rails/pictures?authenticity_token=GErdxBU0dPRINvw8ZSj6VfBpPQ7hMbwtL0B4YWu94Mg%3D" for 127.0.0.1 at 2013-11-29 12:30:43 +0100
Processing by RedactorRails::PicturesController#create as HTML
  Parameters: {"authenticity_token"=>"GErdxBU0dPRINvw8ZSj6VfBpPQ7hMbwtL0B4YWu94Mg="}
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 4 LIMIT 1
   (0.2ms)  BEGIN
   (0.2ms)  ROLLBACK
  Rendered text template (0.0ms)
Completed 200 OK in 5ms (Views: 0.4ms | ActiveRecord: 0.9ms)
As you can see it looks like items are missing from the parameters in the failed upload action.

The only error I seem to get is in my browser's console: Uncaught SyntaxError: Unexpected end of input

Did some further investigation into the actual HTTP request that is being sent and it looks like the client isn't correctly sending the file in the request:

A successful POST request:

"rack.request.form_input"=>
<StringIO:0x007ff68cf47f38>, "rack.request.form_hash"=>{"file"=>{:filename=>"fish.jpg", :type=>"image/jpeg", :name=>"file", :tempfile=>
<File:/var/folders/kq/6krgq50x6y3dsx4c4mf19f_40000gn/T/RackMultipart20131129-26808-1jw83tu>, :head=>"Content-Disposition: form-data; name=\"file\"; filename=\"fish.jpg\"\r\nContent-Type: image/jpeg\r\n"}}, "action_dispatch.request_id"=>"db53425e5252ab4045b69e61e076b9d5", "action_dispatch.remote_ip"=>127.0.0.1, "rack.session"=>{"session_id"=>"f5f4102fc79e015efd5abd2ab49156a8", "_csrf_token"=>"Yfvf/glbqZ8RVApod+wFswv84y1sFuiBmtxd9vCG37Q=", "warden.user.user.key"=>["User", [4], "$2a$10$PvP8UOZ27XwZo2fUfQh0H."]}, "rack.session.options"=>{:path=>"/", :domain=>nil, :expire_after=>nil, :secure=>false, :httponly=>true, :defer=>false, :renew=>false, :secret=>"b929602597766546d091f3312aff29d4eb3057308b332bd3dc97be55a810",...

An unsuccessful POST request:

"rack.request.form_input"=>
<StringIO:0x007ff6905e3ec0>, "action_dispatch.request_id"=>"2c37966c55458361c4246178d353fd67", "action_dispatch.remote_ip"=>127.0.0.1, "rack.session"=>{"session_id"=>"f5f4102fc79e015efd5abd2ab49156a8", "_csrf_token"=>"Yfvf/glbqZ8RVApod+wFswv84y1sFuiBmtxd9vCG37Q=", "warden.user.user.key"=>["User", [4], "$2a$10$PvP8UOZ27XwZo2fUfQh0H."]}, "rack.session.options"=>{:path=>"/", :domain=>nil, :expire_after=>nil, :secure=>false, :httponly=>true, :defer=>false, :renew=>false, :secret=>"b929602597766546d091f3312aff29d4eb3057308b332bd3dc97be55a810",...

Is this something to do with the way the HTTP request is being formed on the client side?

betjaminrichards commented 10 years ago

I found the culprit for this issue. It wasn't redactor-rails.

It was the Pow server I was using in development