Parsing rails uploaded temporary file more then once - ruby-on-rails

I'm new to rails and I'm currently trying to parse an uploaded file to rails. However, after I "read" the file once I cannot read it again. From what I've read online it appears that rails immediately deletes the uploaded file. Is there a way to make the file persistent? My code is as follows
file_param = params[:sequence]
file_param.read.each do |l|
# do stuff
end
file_param.read.each do |l|
# do stuff again. this is not being called.
end
I've thought of using paperclip or some other storage gem, but I don't need to store the files, simply read their contents. Thanks!

Read it into an array, if you really need to go over it multiple times, or just save it.

Related

Carrierwave + Fog + S3 remove file without going through a model

I am building an application that has a chat component to it. The application allows users to upload files to the chat. The chat is all javascript but i wanted to use Carrierwave for the uploads because i am using it elsewhere in the application. I am doing the handling of the uploads through AJAX so that i can get into Rails land and let Carrierwave take over.
I have been able to get the chat to successfully upload the files to the correct location in my S3 bucket. The thing i can't figure out is how to delete the files. Here is my code the uploads the files - this is the method that is called from the route that the AJAX call hits.
def upload
file = File.open(params[:file_0].tempfile)
uploader = ChatUploader.new
uploader.store!(file)
end
There is little to no documentation with Carrierwave on how to upload files without going through a model and basically NO documentation on how to remove files without going through a model. I assume it is possible though - i just need to know what to call. So i guess my question is how do i delete files?
UPDATE (11/23)
I got the code to save and delete files from S3 using these methods:
# code to save the file
def upload
file = File.open(params[:file_0].tempfile)
uploader = ChatUploader.new
uploader.store!(file)
uploader.store_path()
end
# code to remove files
def remove_file
file = params[:file]
uploader = ChatUploader.new
uploader.retrieve_from_store!(file)
uploader.remove!
end
My only issue now is that the filename for the uploaded file is not correct. It saves all files with a "RackMultipart" and then some numbers which look like a date, time, and identifier? (example: RackMultipart20141123-17740-1tq4j1g) Need to try and use the original filename plus maybe a timestamp for uniqueness.
I believe it has something to do with these two lines:
file = File.open(params[:file_0].tempfile)
and
uploader.store!(file)

How to implement a user creating a file via form and then downloading it in rails?

I am building an online translation platform. When a job is done being translated and saved, I want the user to be able to download the translated version as a text file. It is currently saved in a string in the model called "target_text".
I know in ruby I can use this method:
File.open("translation.txt", 'w') {|f| f.write("my translated string") }
I am assuming I could tack the location for the file to be saved in front of the "translation.txt", but I am not sure what folder within my app I should specify?
Furthermore I want this file to be attached to the "job" object in the same way that paperclip can attach files, the difference being it's initiated server side. How should I go about this?
I have googled all over looking for an answer to this, and I want to make sure I do it in the cleanest way possible. I would really appreciate even directions to a good place to look to understand this concept.
I don't quite understand the question, but I hope this could help...
Instead of using
File.open("translation.txt", 'w') {|f| f.write("my translated string") }
try using the following
Tempfile.open(['translation', '.txt'], Rails.root.join('tmp')) do |file|
# this will create a temp file in RAILS_ROOT/tmp/ folder
# you can replace the 'translation' text part to any auto generated text for example
# Tempfile.open([#user.id.to_s, '_translation.txt'] will create
# RAILS_ROOT/tmp/1_translation.1fe2ed.txt
# the 1fe2ed is generated by Tempfile to avoid conflicting
begin
file << "my translated string"
# this creates the file
# add all the processing you need here... cause the next ensure block
# will close and delete this temp file... so that the tmp dir doesn't get big.
# you can for example add the file to paperclip attachment
#user.translation = file
# assuming that user has paperclip attachment called translation
ensure
# close and delete file
file.close
file.unlink
end
end
also check the Tempfile docs... this is the practice i've been using... not sure if it's the best or not.. but it didn't create any issues so far
(even with paperclip s3 storage)

How can I specify what files can be uploaded to the server in paperclip

Yes I know that paperclip has a validates_attachment_content_type, but I would really like to have it validate_by_file_extension ... ie ... I have an array of allowed file extensions in my app, and I'd like for paperclip to see if the file about to be uploaded has a file extension in that array, and if not I want it to not even start the upload and kick back an error.
How do I go about doing this.
you can define your own validation methods:
validate :validate_by_file_extension
def validate_by_file_extension
errors.add_to_base("Invalid file extension") unless ALLOWED_EXTENSIONS.include?(File.extname(attachment_file_name))
end
but you can't easily kick back before the start of the upload from rails, as in most cases by the time your rails controller action method gets called, the file has been streamed and a temp file has been created. You'd need to go higher on the stack to be able to stop things from even starting to upload.

can't write uploaded files in rails

I'm still fairly new to rails but I've done file uploading before. I'm trying to implement very basic file uploading - nothing fancy, just upload the file, save it, and make a record of it. So here's my method for saving the file:
def self.save(upload,name)
directory='public/uploads'
ext=File.extname(upload.original_filename)
path=File.join(directory, "#{name}#{ext}")
File.open(path,'wb') { |f| f.write(upload.read) }
end
The file is apparently valid, as extname() gets the correct extension. The new file is created at the correct place. But somehow the writing fails, as the file is always empty. Doesn't matter what type of file I use. What could be going wrong? I'm using Rails 3.0 if it matters.
Try doing File.open(path,'wb') { |f| f.write(upload.read); f.close }
The IO buffer probably isn't flushing, but closing the file should flush it.
Also, I'd strongly recommend using a plugin such as paperclip for file uploads, simply because file uploads can be annoying to manage, and paperclip provides a very nice way to abstract most of that into conventions so you can just add a couple of columns and do model.upload = params[:file].

How to delete the temporary files automatically in ruby-rails?

My Rails app has to process and generate PDF XFA files and send to the user/browser.
Its working fine. But the issue is that before sending the file to the user, it creates 2 files in the rails tmp directory.
If 10 requests come to the pdf_controller, the number of the temp files in the tmp directory will double and it will eat up the space.
After searching around I thought that Sweeper will come to the rescue. But not much knowledge about Sweeper.
So, can anyone plz suggest which way to go?
Tempfile will delete files when the object is finalized.
Tempfile on Rdoc
Example:
def get_pdf
model = Model.find(params[:id])
file = Tempfile.new
model.to_pdf(file)
send_file file.path, ...
end
I can provide a better example if you paste your code into your question.
You could use a cron task, that deletes the files every n minutes, or, you could order the deletion from the controller itself.

Resources