How to upload file and save it directly to MongoDB using GridFS - ruby

I have a Sinatra application hosted on heroku and I'm trying to enable file uploading. I know heroku doesn't allow saving to the file system so I'm trying to save the image to MongoDB using GridFS directly. But I don’t know how.
Using the code below, I'm able to save to file system
base_dir = Dir.pwd + "/static/images/channels/"
File.open("#{base_dir}" + params['logo'][:filename], "w") do |f|
f.write(params['logo'][:tempfile].read)
end
How do I save the file directly to MongoDB without first saving it to the file system?

You can use the GridFS API to basically do what you're doing above, but write to MongoDB: http://api.mongodb.org/ruby/current/Mongo/GridFileSystem.html#open-instance_method.

I think you need to upload the file as binary data to a database.
You can use PaperClip to upload files and then store them as binary to MangoDB.
here this link might help you out:

If your files are actually less than 16 mb, please try using this Converter that changes the image of format jpeg / png to a format of saving to mongodb, and you can see this as an easy alternative for gridfs ,
please follow this github repo for more details
https://github.com/saran-surya/Mongo-Image-Converter

Related

How to write to get content from Google Cloud Storage in Ruby?

Is it possible to use the Google Cloud Function to retrieve the contents of a file placed in Google Cloud Storage?
Also, I would like to know how to do this.
I tried this,
file = bucket.file(path)
file.download.read
but I didn't read response
\xA3\xB4\x97..
file = bucket.file(path)
downloaded_file = file.download
File.read(downloaded_file, encoding: 'ENCODE') # utf8 etc.
Also read this answer.
GL.

Parse Server - Where does it store uploaded files

I am new to Parse Server (implementing it on Heroku and locally).
I have a basic question, when I upload a file using the ParseFile class, it provides me a URL and a fileobject. Where is this File being stored?
Is it being stored physically on a file system? Or in Mongodb?
Thank you!
I found a collection in Mongodb named fs.files. The files I uploaded were located there. I assume the Parse URL is generated as a redirect.

How to check that Image is already exist or not on given path in GWT + MVP4G

I want to check if the Image exists or not on the fileSystem. I am using GWT 2.4 with the MVP4G framework. When I use IO.File on the client side to check file.exist(), it gives me an error because we cant use IO on client side, so How can I check this..?
How did you get the image?
The only way you should get the image is by using some file upload mechanism and any one of those will only let the user choose an already existing file; so there is actually no need to check for the file existence.
For file upload you can use the gwt library gwtupload. or use apache File Upload library.

Listing files in GridFS directory using Ruby driver

Is it possible to list all files in given GridFS directory using ruby mongo driver?
I've searched through documentation and have not found anything satisfying. What is more I cannot find any way to search files by metadata too.
I have checked java driver's documentation and both of these functionalities are available out of the box using GridFS class.
Best regards,
Michał
grid = Mongo::Grid.new(Mongoid.database)
files = grid.instance_variable_get :#files
files.find("filename" => "uploads/143-normal_regular2.jpg").as_json
# files.remove - deletes alle files
same works for chunks

Ruby - Working with Mechanize::File response without saving to disk

I'm working on my first ORM project and am using Mechanize. Here's the situation:
I'm downloading a zip file from my website into a Mechanize::File object. Inside the zip is a file buried three folders deep (folder_1/folder_2/file.txt). I'd like to pull file.txt out of the zip file and return that instead of the zip file itself.
My first thought was to use zip/zipfilesystem. I can do this fine if I save the file to the disk first and use Zip::ZipFile.open(src) but can anyone tell me how/if it is possible to send it over straight from the Mechanize::File.body.
My gut says this has to be possible and I'm just missing something basic. I tried...
zipfile = Mechanize::File.body
Zip::ZipFile.open(zipfile)
...but from what I can tell Zip::ZipFile is only set up to locate a source from a filesystem.
Any direction would be very appreciated and let me know if there are any questions
Thanks in advance
Rob
It seems what you want to do is not possible with rubyzip. From rubyzip library's TODO file:
SUggestion: ZipInputStream/ZipOutputStream should accept an IO object in addition to a filename.

Resources