Unable to use Mongoid::GridFs put to upload files - ruby-on-rails

I'm just trying to use the mongoid-grid_fs gem in order to upload images and files to MongoDB.
The controller code in question currently looks like this:
grid_fs = Mongoid::GridFs
encFile = File.open(attFile)
File.open(encFile, 'wb') do |f|
f.write(encData)
end
grid_file = grid_fs.put(encFile.path)
I'm getting this error:
undefined method `destroy' for nil:NilClass
/GEMS/bundler/gems/mongoid-grid_fs-2f0e12e85a24/lib/mongoid/grid_fs.rb:150:in `put'
(eval):2:in `put'
/x/app/controllers/runtime/attachments_api_controller.rb:126:in `upload'
FYI line 126 in attachments_api_controller is the grid_fs.put line.
I'm not sure what the issue is.
I have also declared require 'mongoid/grid_fs'
I have put bindings around the issue, I can confirm that encFile.path is correct and it contains valid data.
EDIT 1: I've tried different combinations of trying to put the file like
grid_fs.put(encFile) - grid_fs.put(File.open(encFile.path)) - grid_fs.put(File.read(encFile.path)) but all of these still return the same error.

Related

Rails 5 gem gon not recognize jbuilder file

I have implemented rails gem gon in index method and it works fine. But when I try to implement in controller#new it is not working this is my error output:
No such file or directory # rb_sysopen - app/views/cuenta_proveedors/new.json.jbuilder
but i have already create this file.
this is my controller
def new
#cuenta_proveedor = CuentaProveedor.new
#cuentas = #negocio.cuenta_proveedors.order(:created_at)
gon.jbuilder // THIS LINE CAUSE THE ERROR SHOWN BY APPLICATION TRACE
end
i have the created the file new.json.jbuiler
json.cuenta_proveedores #cuentas do |cc|
json.proveedor cc.proveedor.nombre
json.inicial cc.monto_inicial
json.saldo cc.monto_adeudado
json.observacion cc.observacion
json.token cc.token
end
Why is this happening? I can not found any logic to this error. If you need more information ask me . Thanks

Invalid argument # rb_sysopen in Ruby on Rails application

I know this is the common question in this forum, but I tried last nearly 4 hours one by one not working for me. Worked before another method & another resource but not in this method.
Error below:
Invalid argument # rb_sysopen - https://example.s3.amazonaws.com/uploads/Data.pdf
When I use like below: Static File
#file = File.open("#{Rails.root}/public/Data.pdf")
Then
File.new(#file)
That is working but when like below:
Dynamic File
#file = File.open("#{papers.paper.url}") #=> output url: https://example.s3.amazonaws.com/uploads/Data.pdf
Then
File.new(#file)
That is showing error. I have tried string like w w+ r r+ etc.
What I'm doing wrong with this code?
Thanks

rubyzip undefined method `to_binary_dos_time' for

The below message getting logged when I try to open zip file with write mode.
Full Error Message :
undefined method `to_binary_dos_time' for 2017-05-30 15:07:21 +0530:Time
Backtrace :
["/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_entry.rb:286:in `write_local_entry'",
"/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_output_stream.rb:147:in `block in update_local_headers'",
"/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_entry_set.rb:35:in `each'",
"/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_entry_set.rb:35:in `each'",
"/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_output_stream.rb:145:in `update_local_headers'",
"/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_output_stream.rb:64:in `close'",
"/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_output_stream.rb:50:in `ensure in open'",
"/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_output_stream.rb:50:in `open'",
"/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_file.rb:216:in `block in commit'",
"/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_file.rb:324:in `on_success_replace'",
"/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_file.rb:214:in `commit'",
"/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_file.rb:242:in `close'",
"/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_file.rb:92:in `open'",
"/app/zipper_job.rb:36:in `perform'",
My code is as below.
path="#{Rails.root}"
new_zip_name= "#{Time.now.to_i.to_s}"
archive = File.join(path,new_zip_name)+'.zip'
Zip::ZipFile.open(archive, 'w') do |zipfile| #Code breaking on this line
#MY Code
end
Any help appriciated!!
Thanks in advance.
Similar problem in both rubyzip and rzip, when trying to change the datetime of the zip entry to the original file datetime:
zf = Zip::File.open("myzipfile.zip", Zip::File::CREATE)
e = zf.add("myfiletozip.txt","myfiletozip.txt") # will set the zipfile date to now
e.time = File.mtime("myfiletozip.txt") # will set it to the original file's
So far so well, but when closing the zip file, the proc fails with the same error.
The problem is that "time=" creates the "extra" structure in the entry, which is then processed further, and that processing uses the missing method.
But why does it work without setting the time? The extra structure is not built, and the missing methods not used. As simple as that.
The to_binary_dos_time / date is present in the Gem: dos_time.rb
However, they seem to be incorrectly referenced by the gem procedures, in my case entry.rb
My circumvention. I simply copied the two methods into my code, extracted form the gem module dos_time.rb. And - miracle - it works. The gem procedure finds them locally and is happy.
However, this bug should be reported to the author, but i do not know how. Maybe someone can do this?
def to_binary_dos_time
(sec / 2) +
(min << 5) +
(hour << 11)
end
def to_binary_dos_date
day +
(month << 5) +
((year - 1980) << 9)
end
# source copied out of entry.rb in rubizyp gem, in my case:
# c:\Ruby27-x64\lib\ruby\gems\2.7.0\gems\rubyzip-2.3.0\lib\zip\dos_time.rb
I have created new file named config/initializers/patch.rb.
Added below code in it, this solved the issue.
Zip::DOSTime.instance_eval do
def now ; Zip::DOSTime.new() ; end
end
The patch I have taken from here

Rails access a file stored on S3 (`method_missing': undefined method `[]')

I am trying to instantiate a file stored on s3 with rails to parse it. The first step is to access the file. I have built a quick script to get familiar with amazon aws-sdk gem.
require 'aws-sdk'
s3 = Aws::S3::Resource.new(
access_key_id: 'XXXX',
secret_access_key: 'XXXX',
region: 'us-east-1')
bucket = s3.bucket('my_bucket')
bucket.objects.each do |obj|
puts obj.key
end
This code will give me the list of all the objects (files) on my drive. It looks like
uploads/doc1.pdf
uploads/doc2.pdf
Now I want to understand how I can retrieve one single object. I tried
object = bucket.objects['doc2.pdf']
puts object.key
But I get the error method_missing': undefined method[]' for Aws::Resources::Collection:0x0055e82a514ab0> (NoMethodError)
I also tried
object = bucket.objects.find(1)
puts object.name
But I get the error undefined method `name' for Enumerator:0x0055855a098de8> (NoMethodError)
Could anyone give me the right syntax to retrieve one single object (file)? Once I get it, I intend to use the "open" method to instantiate the file and then a pdf parser. Thanks!
This has worked for me
object = bucket.object('path to file')

Ruby Soundcloud Gem: Cannot Test Uploaded File

Haven't seen much on testing for the soundcloud gem. Most everything has been splendid - however I found that uploading files to SoundCloud through my specs is a bit tricky.
Here's the problem:
spec:
file = "#{Rails.root}/spec/factories/files/sample.mp3"
title = "From the test suite"
track = #client.post('/tracks', track: {title: title, asset_data: File.new(file)})
Whether or not I use
File.new
or
File.expand_path
the error is the same: NoMethodError:
undefined method `map' for #String:0x007fa1ce8425c0
The successful code in the controller is:
file = params["soundcloud_file"]
title = params["title"]
track = client.post('/tracks', track: {
title: title
asset_data: File.new(file.tempfile, 'rb')
})
Now I understand that params["soundcloud_file"] is an ActionDispatch::Http::UploadedFile, so that's what I've been attempting to work with for the past hour or so with no luck.
I'm open to any way to successfully test an upload.
The problem was with the 'webmock' gem. Even using the required version for VCR (<1.10.0) it throws:
NoMethodError: undefined method `map' for <#String: ... >
Replacing with the 'fakeweb' gem corrects the error.

Resources