need to parse a User uploaded file, but don't care if it's saved

parsing the file's not the problem, just not sure what to do with it before or after. I was thinking about uploading the files using Paperclip, parsing it, then deleting itfrom the database. But, that all seems a little unnecessary, especially if I don't care if they are even saved.

I then thought about grabbing them from the temp directory, but honestly wasn't sure how to go about doing that. So before I really delve into the Tempfile, should I even worry about it in the first place? Each user uploaded file will be like 30kb max, and I'll only be processing a couple hundred a month. Also, I'm hosted on heroku, so the s3 transfers (if I do save the file) will be free. What ya think?

Any advice really is appreciated

Don't know if this is any use - but you could just upload it yourself (rather than using paperclip or file_column):

You can choose the directory that you want it saved in, then delete the file as soon as you are done with it.

Cheers Luke

Sorry just to add that tutorial saves it in a model - but obviously you don't need to do that.

One way would be to just process the uploaded-file data from the request as needed and not worry about saving or doing anything else with the tmp-saved uploaded-file itself, something like:

### in ./app/models/uploadable_file.rb class UploadableFile

  ### for use in testing:

  def initialize(fname, contents)     @fname = fname     @contents = contents   end

  def original_filename     return @fname   end

  def read     return @contents   end

  ### class meths:

  def self.parse(upfile)     return nil if upfile.blank? or not (upfile.respond_to? ('original_filename')\ and upfile.respond_to?('read'))

    orig_fname = upfile.original_filename     contents = upfile.read     # parse/process contents ....   end end

Then just call that class meth in your controller to parse the uploaded file for an example file form element of name "upfile":

   ### in some controller meth that handles that multipart/form-data form:    ...    parsed_contents = UploadableFile.parse(params[:upfile])    # check results ....

Jeff

@Luke: Thanks! Yeah, I saw that tutorial, too. But, also like you said, it still saves it to a model, so I'd rather just let paperclip handle all that. Thank you anyway though.

@Jeff: Thank you! For full disclosure, I'm still a little bit of a noob to Rails (and ruby) but I think I get more than the gist of your code. So, definitely a big thanks.

I do have a couple questions as to what's going on though. Like I get that the file is read, along with its contents, but where is it read from? Like, is the file actually uploaded to a temp directory, and you're saying that I just don't have to expressly mess with that directory? Or, is it just held in memory in a Rails process somewhere? Might that be worse than just storing the file, then deleting it later?

I'm just asking, I honestly don't know. Also, while the file uploads, a Rails process is not tied up, correct? It's actually being buffered by the server?

A big thank you Jeff, and to anyone else that wants to chime in. I think this is definitely a huge start down the right path.

re: where is it read from?: I use passenger to serve my rails apps these days (all running on *nix), and per the passenger docs:

http://www.modrails.com/documentation/Users%20guide.html#_passengertempdir_lt_directory_gt

... 5.10. PassengerTempDir <directory> Specifies the directory that Phusion Passenger should use for storing temporary files. This includes things such as Unix socket files, buffered file uploads, etc. ...

The default for that on *nix is the /tmp dir. So, under such a setup, when a user uploads a file via the rails app, that uploaded file is temporarily saved under the /tmp dir. That's why I was saying that if you didn't want to save the file in terms of the app's context, ie you just want to parse/process the uploaded file's content, you shouldn't have to do anything else with that /tmp file, since the underlying os will handle that clean up of /tmp files as necessary.

Jeff

So instead of saving the file somewhere, it'll just grab it from the tmp directory, parse it, then leave it up to Rails or the server to expire the file after that request is over. Great, that's exactly what I want.

I plan on knocking this out tomorrow, and although I may have a question or two once I delve into it more, I at least understand what's going on now. Big thanks Jeff, really do apreciate the help.