Question of uploading large file using carrierwave, uploadify and nginx upload module

Hey ROR users,
I have a small experimental web app which is for uploading files. The
web app uses carrierwave, uploadify. It works fine on small size
files. However, it can not handle large size files (>1G) efficiently.
Then I decided using nginx upload module, it worked well on small size
files after modification of codes,. But It fails on uploading large
files (>1G) because of http error 499 (client closed connection before
nginx response). The problem here is that, when the uploadify script
finishes sending file to nginx upload module, the uploadify javascript
expects a quick response because the uploadify has finished uploading
files and onAllComplete function will have to execute, however, the
saving object takes much longer time and return late, and the
javascript terminate prematurely, and issues a IO error. In fact, the
file is successfully uploaded and placed in the correct place.

attachments_controller

def create
@attachment = Attachment.new(:fast_asset => params[:fast_asset])
if @attachment.save # the line takes long time to finish
render :nothing => true
else
render ‘new’
end
end

attachment model

class Attachment < ActiveRecord::Base
attr_accessible :file, :fast_asset
mount_uploader :file, FileUploader

def fast_asset=(file)
if file && file.respond_to?(‘[]’)
tmp_upload_dir = “#{file[‘filepath’]}_1”
tmp_file_path = “#{tmp_upload_dir}/#{file[‘@original_name’]}”
FileUtils.mkdir_p(tmp_upload_dir)
FileUtils.mv(file[‘filepath’], tmp_file_path)
self.file = File.open(tmp_file_path)
end
end

end

the new uploading form uses the uploadify basic example javascript
code. (GitHub - RonnieSan/uploadify: A jQuery plugin for file uploads.)

_form.html.haml

= javascript_include_tag ‘swfobject.js’,
‘jquery.uploadify.v2.1.4.min.js’
:javascript
$(document).ready(function() {
$(‘#attachment_file’).click(function(event){
event.preventDefault();
});
$(‘#attachment_file’).uploadify({
buttonText: ‘Select files’,
uploader: ‘/uploadify/uploadify.swf’,
cancelImg: ‘/uploadify/cancel.png’,
multi: true,
auto: false,
script: ‘/files/fast_upload’,
onComplete: function(event, queueID, fileObj, response, data) {
var dat = eval(‘(’ + response + ‘)’);
$.getScript(dat.upload);},
onAllComplete: function(event, data) {
alert(data.filesUploaded + ’ files uploaded successfully!\nThe
average speed is ’ + data.speed);
window.location = ‘#{root_path}’;
},
});
$(‘#attachment_submit’).click(function(event){
event.preventDefault();
$(‘#attachment_file’).uploadifyUpload();
});
});
= form_for @attachment, :html => { :multipart => true } do |f|
.field
=f.label :file
%br
=f.file_field :file
.action
=f.submit “Upload”

How should I fix the problem? Thanks in advance,

–ZZ