Uploading pdf document with carrier wave from remote url - ruby-on-rails

I m trying to generate some test data on the test environment, and have some random PDF urls that we can use for testing. My AR class looks like this :
class PDF::Template < ActiveRecord::Base
mount_uploader :document, PdfUploader
end
Using ruby code or code that I can run via rake task or via rails console? How can I upload something from random pdf url?

From Carrierwave wiki:
If you're looking to quickly generate some seed data for your application and don't want to upload images already stored in your
application you can easily upload remote images by using
remote_[your_image_attribute_name]_url
Galaxy.destroy_all
Galaxy.create!(name: 'Andromeda', remote_photo_url: 'http://apod.nasa.gov/apod/image/1407/m31_bers_960.jpg', address: 'next to the Milky Way')

Related

Seeding multiple image attachments from Amazon S3 in Rails

I am trying to seed multiple image attachments to a model. I have been using this link but I am still sort of stuck since what I aim to do differs a little since:
I am trying to attach multiple images to each object (which I seed) in the model
I want to retrieve these images from my S3 bucket and attach them to the objects (is this possible?)
Here's my seed.rb:
shirt = Item.create(name:"Basic Shirt",price:19.99)
skirt = Item.create(name:"Basic Skirt",price:29.99)
sweater = Item.create(name:"Basic Sweater",price:39.99)
kid_hood = Item.create(name:"Basic Kid Hoodie",price:19.99)
# somehow attach images here?
I am using the aws-sdk-s3 gem in order to connect Active Storage to my S3 bucket. Please tell me if any additional files are needed for viewing. I will happily edit this post to include it.
ActiveStorage work on plain byte streams, so you can download the file (using open-uri for instance) and assign the stream as the content of the attachment.
Assuming you have the following (adapt if different)
class Item < ApplicationRecord
has_one_attached :photo
end
you can have your seeds as:
require 'open-uri'
shirt = Item.create(name:"Basic Shirt",price:19.99)
shirt.photo.attach(io: open('your-s3-nonexpiring-url'), filename: 'foo.bar')
# ...
Just a note: as of Ruby 3.0, you will need to call URI.open instead of open. See the reference to open-uri here.

CarrierWave: allowing one-by-one file uploads as well as a bulk zip upload

I'm using CarrierWave to upload and manage resources on my ActiveRecord models. I've defined my own Uploader and mounted it to a bunch of properties on one of my models as shown below:
class Theme < ActiveRecord::Base
...
mount_uploader :masthead, ThemeResourceUploader
mount_uploader :background, ThemeResourceUploader
mount_uploader :footer, ThemeResourceUploader
...
end
This works as expected when creating a new Theme from the params in my Rails controller, but in addition to allowing the user to upload one image at a time I also want to allow them to upload an zip file containing all these images and then use this zip to construct the Theme.
To try and accomplish this I created a new Uploader for the zip file and a controller method which uses Rubyzip to extract the uploaded zip in memory and then tries to assign the resultant stream to my ActiveRecord model's properties.
def import
require 'zip'
#theme = Theme.new
zip_upload = params.require(:theme).require(:zip)
uploader = ThemeImportUploader.new
uploader.cache!(zip_upload)
Zip::File.open(uploader.file.path) do |zip_file|
#theme.masthead = zip_file.get_input_stream('masthead.png')
#theme.background = zip_file.get_input_stream('background.png')
#theme.footer = zip_file.get_input_stream('footer.png')
end
#theme.save
end
Unfortunately this doesn't work. I don't receive any error or failure, but the Theme is saved with empty values for the resources and the files are not created in my upload folder.
I believe I can get this working by extracting the zip to temporary files and then reading those files into the CarrierWave properties, but this seems like a very round the bush way of solving the problem.
How can I upload and extract a zip in memory and assign its contents to my CarrierWave enhanced models?

Paperclip - Upload from URL rather than a form

I'm in the process of migrating a set of files from an old Drupal application to a Rails app.
Using paperclip, I want to upload a file to this model:
class Video < ActiveRecord::Base
has_attached_file :video_file
end
But I want to upload the file from a URL in code rather than using a form.
Apparently since Paperclip 2.1.4, you are able to do this like so:
video.video_file = URI.parse('http://path/to/video.mp4')
When I run this, there is a noticeable delay while the file is downloaded but none of the fields for the file are populated and the file has not been uploaded. What step am I missing?
You can do this
video.video_file = File.open("http://path/to/video.mp4")

Rails - Run an external program on a Paperclip attachment for processing and save the output attachment back to the model

In my rails project, I need the user to upload a file (input_file) which I will process using an external application. Once, it is completed, I want to attach the processed file to the same model as a different attachment (output file).
I have been able to create a form and use paperclip to allow the user to upload the input_file to my model FileProcessor. Im not sure on the next step as to how do I call an executable on the input_file and save it as output_file.
Based on paperclip, once the file is upload, I can access the path via input_file.path
output_file = %w{external_app input_file.path out_file_name}
Class FileProcessor
has_attached_file :input_file
has_attached_file :output_file
Im confused as to where this call to run the external app be placed? in the model or in the controller (def create). Also, how do I work with paperclip to associate the output_file with the model without actually uploading.
The location for such code depends on what kind of business your external process does. With the requirements as depicted in the question, it would be as simple as this:
class FileProcessor < ActiveRecord
...
after_validation do |fp|
tmp_file = "/tmp/#{rand}"
system "/usr/bin/awesome.sh #{fp.input_file.path} > #{tmp_file}"
fp.output_file = File.open(tmp_file)
end
...
end
I hope, this is what you are looking for.

Paperclip: How to store a picture in a Rails console?

I tried storing a local image in a rails console.
Because I have many pictures in my local storage (I use crawler to download tons of pictures), I want to store them into a database, with the benefit of paperclip to do some image job, like thumbnail etc.
If I use a webpage to save new pictures to database one by one, it will cost a lot of time. So I want to find a way in rails console (some code) that can batch save-picture-into-database.
To further clarify #andrea's answer:
YourPaperclippedModelHere.new(:your_paperclip_field => File.new(path, "r"))
So if your model is called Image and your paperclip field is data:
Image.new(:data => File.new(path_to_your_file, "r"))
If this is the model:
class User < ActiveRecord::Base
has_attached_file :avatar
end
then the following should work from the console:
>> User.create(:avatar => File.open('/path/to/image.jpg', 'rb'))
I dont know if it is what you want ... but
to save an paperclip asset from console
You could simple use a File instance .
a.e.
Image.new :data=>File.new("/path/to/image.jpg","r")
Late Answer but hopefully it will work for others.
You need to include.
File.new("#{Rails.root}/public/images/default_avatar.png", "r")

Resources