(Multipart) zip upload in ruby webmachine handled by rack - ruby

I'm making an upload form for zips in a ruby webmachine app. My idea is to have an upload through my backend where I can add some extra params and then upload it to amazons s3 service with RestClient.
I did successfully create a direct upload (web based form post) to a s3bucket, but in that way I'm unable to handle the variables which are needed in the request, the way I want.
I've tried several things but I can't figure out, how to handle the request, as soon as it gets in my backend. I've created a resource and I'm debugging directly in the process_post method.
My #request variable represents a Webmachine::Request, with a Webmachine::Adapters::Rack::RequestBody and a Rack::Request, but I can't get the file out of it to use it as input for my RestClient request.
I think; #request.body.to_s and #request.body.to_io, represent the uploaded file in some way, and I tried to use them as input for Rack::Multipart methods, but that doesn't give me the file.
I also tried to work with the rack-raw-upload gem, but I can't get the mime-type something else than "application/x-www-form-urlencoded" or multipart. I do explicitly set it to; application/octet-stream
Things like File.new(filename, 'rb') gave me `rrno::ENOENT: No such file or directory # rb_sysopen'. For filename I just used 'example.zip'.
I guess I'm missing something which has to do with the Rack::Request call(env) method.
Does somebody have an idea, on how to handle the Rack uploads? Or give me any hints for a new direction? Thanks.

I've created a gist which shows how to retrieve the multipart stream. You'll need further parsing in order to get the uploaded file.
https://gist.github.com/jewilmeer/eb40abd665b70f53e6eb60801de24342

Related

wami-recorder and Sinatra

I need my users to record a voice clip. I'm using wami-recorder to take care of the voice recording on the client side. I am using the default javascript functions:
Wami.startRecording('/upload?name=mysound.wav');
Wami.stopRecording();
In the chrome console, I can see the post request being sent successfully because, when I use the wami-recorder default url "https://wami-recorder.appspot.com/audio", it works fine. But I can't seem to receive the file on the backend, Sinatra. How do I get the file and store it? Can someone point me in the right direction?
This is my controller:
post "/upload" do
p params
end
The output is just:
{"name"=>"file.wav"}
The audio is posted as part of the raw body. I was able to save the file in rails using
request.raw_post
on some brief googling it appears that you should be able to do the same in sinatra with
request.env["rack.input"].read
You can then save this directly to a file
file = File.new("audio.wav", "w+b")
file.write request.env["rack.input"].read
file.close
This should save out a wav file within the same directory

Ajax file upload in node.js

I want to upload ajax file upload which uses xhr to send file data,
at client m using this
http://valums.com/ajax-upload/
how i will accept this data on node and save the file to server by node.js , which module i need to use in node.js?
I've created an uploader with progress bar using the formidable module, it's really easy to use and provides a lot of useful callbacks.
Have a look here:
https://github.com/felixge/node-formidable (scroll down to get the Docs)
http://debuggable.com/posts/parsing-file-uploads-at-500-mb-s-with-node-js:4c03862e-351c-4faa-bb67-4365cbdd56cb
due to the lack of an example file in valums ajax-uploader, I've just created one.
It catches up the XHR upload if possible, alternatively falling back to the old form-based method.
All in conclusion to valums ajax-uploader.
https://github.com/aldipower/file-uploader/blob/master/server/nodejs.js
Maybe Valums will accept the pull request some time and the sample file gets merged in the standard repository.

Replacing the body of a proxied subrequest with the contents of a file

I'm using the upload module to write the uploaded file to disk as soon as it arrives in nginx. In addition, I'd like to create 2 subrequests:
POST to a URL containing the uploaded file
POST to another URL without the uploaded file
The second request is easy to do because the upload module has already stripped out the upload. My problem is with the first request: How do I get the uploaded file back into the the subrequest.
A solution for my question has been committed to the echo module.
The module you linked to has the upload_set_form_field directive and a few special variables (listed in that directive), which you can use to pass the file details to the backend as a POST variables. The example given appears to put the upload back in the POST data. Can you adapt your backend script to make that work?

Ruby Oauth File upload/Multipart POST request

I've been looking at this for a couple of days now and haven't
found a solution. Is there a way to upload a file using OAuth-Ruby?
I am working with a REST system that protects their resource with oauth. I am building a test tool using ruby and oauth-ruby to make it easier to upload test data to the system. But I can't get around to upload files to the resources.
When I send a normal request, everything works but adding a file as a
parameter makes the signature invalid.
Example:
#access_token.post("http://.../imageresource", {:name=>"awesome cat"}, {'Content-Type' => 'multipart/form-data'})
works but gives me:
<error>
<message>images/POST: Request has no file data</message>
</error>
I am not sure how to add a file to the post.
Any thoughts on this?
Thanks,
I know this is old but I'm looking to do this too, this looks like it could do the trick.
Actually there's a question ruby-how-to-post-a-file-via-http-as-multipart-form-data that has an example.
This is either impossible to do with the oauth gem or exceedingly difficult. Either way, I don't know of any way to do it using that gem.
It can be done trivially with my signet gem as long as you have a handy way to construct a valid multipart request body. The construction of such a request body is out-of-scope of an OAuth gem, but should be pretty easy to do with most HTTP clients. The httpadapter gem can then translate the request into a form that signet can sign. Let me know if your preferred HTTP client isn't supported by httpadapter and I'll get that resolved immediately.
See the second example on the fetch_protected_resource method to get an idea for how this might be done.

Downloading CSV via AJAX

Can you use AJAX to download a generated csv file from a web application? If so does anyone have any kind of reference that I could be pointed towards?
EDIT: Sorry I should have mentioned I am using Prototype's Ajax.Request and I looked in firebug's response tool and the generated CSV is the response, I just need to get it to pop up with the save file option after has been generated by the Ajax.Request
This is a known limitation of Ajax requests, you will need to use JS like:
window.location='download-csv.rb';
Instead of using an Ajax request. Another way is to change the location of a hidden Iframe, but this has it's own pro's/con's.
You will never get an Ajax request to display the 'file save' dialog, no matter what HTTP headers you send.
In light of your latest edit, to make your CSV file trigger a file download (instead of rendering in the browser), there's no need for Ajax.
Instead, the solution is to have your back-end system add this HTTP header when the CSV file is requested:
Content-disposition: attachment; filename=<your_filename.csv>;
Your implementation here depends on the back-end system you're using. If you're using Rails (as your username suggests), here's a start:
filename = 'your_filename.csv'
headers['Content-Type'] = 'text/plain'
headers['Content-Disposition'] = "attachment; filename=\"#{filename}\""
render :layout => false
Downloading it isn't the problem; you can download any data you like via XmlHttpRequest. The hard part is parsing it. There are several ways to parse it, from regexs to string indexing.
You can use "AJAX" to download anything .. Some people would say you shouldn't call it AJAX in that case since that term is rigorously devoted to downloading XML. But really it's just a mechanism to get data into the client w/o reloading a page. If you were loading HTML it'd be called AHAH, for CSV i guess you'd call it AHAC or AJAC? ..

Resources