rubyzip extracts empty file in activejob - ruby-on-rails

I have a function in my activejob that extracts a specific file from a zip file. Following code extracted empty file.
def extract_file(from, name)
to = get_local_dest(name)
Zip::File.open(from) do |zip_file|
entry = zip_file.glob(name).first
puts entry.get_input_stream.read
entry.extract(to)
end
return to
end
I added a debugger and ran following line of code in console then the extracted file was not empty.
entry.extract(to)
Can anyone help me with this issue? Why this function is extract empty file when it runs in activejob?

your code seems to be fine. Please make sure your file is not saving again in code that proceeds.
There are chances that it extracted fine but later in code it was save again in empty form. This had happened to me in past :)
Let me know if this resolved your issue. :)

Related

Writing a file to a specific path in ruby taking the file name from excel

I am having some trouble writing a file to a specific path taking the file name from excel. Here is the code which I am using
out_file = File.new (#temp_path/ "#{obj_info[3].to_s}","w")
"#{obj_info[3].to_s}" = sample.txt
The value sample.txt comes from Excel during run time
#temp_path = "C:/Users/Somefolder/"
The error displayed is:
NoMethodError: undefined method `[]' for nil:NilClass
However, if the code is:
out_file = File.new ("#{obj_info[3].to_s}","w")
it successfully creates a file called sample.txt in the default directory. However, I want it to be stored in a specific directory and the file name needs to be passed from Excel.
Any help is much appreciated.
I believe your problem is because there a space between / and "
#temp_path/ "#{obj_info[3].to_s}
and I guess you want to build a path.
My advice is that you use File.join
f_path = File.join(#temp_path,obj_info[3].to_s)
out_file = File.new (f_path,"w")
Let me know if that solved the problem
You have 2 problems:
obj_info is nil so you make an error reading the value from excel, the error you get indicates it is on an array, in the code you published the only thing that's an array is what you read from excel.
Print the contents with p obj_info right before your code to check.
#temp_path and {obj_info[3].to_s} need to be concatenated to make a path.
You can do that with File.join like Mauricio suggests or like this
out_file = File.new ("#{#temp_path}/#{obj_info[3]}","w")
You can drop the to_s in this case.
It would be better if you publish the whole of your script that is meaningful.

Not able to create a file for writing

I am uploading txt files using carrierwave. The files are not small (80 MB - 500 MB) and I want to remove some of the lines to reduce this size (about 80% of the file size is going to be reduced).
I have created a model method in order to clear these lines:
require 'fileutils'
def clear_unnecessary_lines
old_file_path = Rails.root.join('public').to_s + log_file.to_s
new_file_path = old_file_path.sub! '.txt', '_temp.txt'
File.open(old_file_path, 'r') do |old_file|
File.open(new_file_path, 'w') do |new_file|
old_file.each_line do |line|
new_file.write(line) unless line.grep(/test/).size > 0
end
end
end
FileUtils.mv new_file_path, old_file_path
end
but I am getting error when I am trying to open the new file saying there is no such file. As I have read opening a file with the w option should create an empty file for writing. Then why I am getting such error?
Also, since log_file column is holding the path to the original file, and I am changing it, could you tell how to rename the new file with the old name? As I have checked I should specify only old and new names, not paths.
Errno::ENOENT: No such file or directory - /home/gotqn/Rails/LogFilesAnalyser/LogFilesAnalyser/public/uploads/log_file/log_file/3/log_debug_temp.txt
It is strange that If I execute the following command in rails console, it is not throwing an error and the file is created.
File.open('/home/gotqn/Rails/LogFilesAnalyser/LogFilesAnalyser/public/uploads/log_file/log_file/3/log_debug_temp.txt','w')
Ah, i see your problem now. When you do this
new_file_path = old_file_path.sub! '.txt', '_temp.txt'
you call the "self-altering" version of sub, ie sub!. This will actually change the value of old_file_path as a side effect. Then, in the next line, you try to open this file, which hasn't been created yet. Take out the exclamation mark and you should be fine.

Rails - trying to open a file, but getting MiniMagick::Invalid

I am trying load a file in controller, like
Avatar.all.each do |avatar|
if avatar.avatar_file_name
file = "lib/data/#{avatar.avatar_file_name}"
image = MiniMagick::Image.open("#{file}")
...
end
end
But whenever I run this code, I get the error message
MiniMagick::Invalid
I already tried to reinstall the imagemagick as is mentioned here, but it didn't really help me.
Where could be a problem? Am I missing a component or something?
Thank you
Maybe you should check if file exists first
if File.exist?(file)
image = MiniMagick::Image.open(file)
end
To get path to the file you should do something like this:
file = "#{Rails.root}/lib/data/#{avatar.avatar_file_name}"
btw lib is not the best place to save your images.

CSV.open(..) block does not execute, and ends immediately

I am opening a CSV file for writing with the following code, as indicated in CSV documentation here (http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV.html):
CSV.open( csv_file_out, 'wb' ) { |csv_line_out|
#stuff happens here
}
the block does not execute, i.e. after the CSV.open command debugger goes directly to the end of block.
thinking "just in case", I try the same with IO modes w+b wt w+t ab a+b. I also tried creating the file before the in case there was a problem with opening it. nothing changes.
when I stop the debugger at the CSV.open line, I am able to execute a block manually and create a csv file myself. I can also create the file frmo the exact same command from console. But it won't work when executed in the server.
Any ideas on what's going on are very welcome. Thanks in advance!
You don't need the {} brackets before your |csv_line_out|
Your trying to iterate sth. (the files content) that does not exist so its logical that it ends imediatly! Try it this way:
CSV.open(csv_file_out, "wb") do |csv|
csv << [column1, column2,...]
end

File read fails when using Delayed Job

How to check that my params['Filedata'] is corrupted or not?
I have function it's reading file from params['Filedata'] and writing it to the other file.
File.open(upload_file, "wb") { |f| f.write(params['Filedata'].read) }
this line working fine for me..
But when i am calling this function with delayed job function send_later than I am getting error with params['Filedata'].read.
I'm guessing params['Filedata'] refers to a uploaded file.
Rails handles uploaded data as temporary files, which are deleted after the request is completed. If you want to access the file later, you need to copy it to a more permanent location.

Resources