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")
Related
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')
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?
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)
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.
I have video model with the following definition:
class Video
require 'carrierwave/orm/activerecord'
mount_uploader :attachment, VideoUploader
mount_uploader :attachment_thumbnail, VideoThumbnailUploader
...
end
When I upload a video file. It also sends the file to our encoding service Zencoder, which encodes the video file and creates a thumbnail for it.
Normally, I could do something like #video.attachment.url, which will return the path of the video file. I'd like to do the same thing with the thumbnail. i.e. #video.attachment_thumbnail.url
However, since the attachment is created by our encoding service, which also uploads it to a specified S3 bucket. How do I assign the attachment to the attachment_thumbnail column for the record?
Can I simply do something like:
#video.update_attributes(
:attachment_thumbnail => 'https://bucket_name.s3.amazonaws.com/uploads/users/1/video/1/thumb.png'
)
Is it possible to assign files like this to Carrierwave?
You can do the following:
#video.remote_attachment_thumbnail_url = 'https://bucket_name.s3.amazonaws.com/uploads/users/1/video/1/thumb.png'
But that will cause Carrierwave to download + reprocess the file rather than just make it the thumbnail. If you're not going to use Carrierwave's processing, then it might make more sense to just store the URL to the thumbnail on the model rather than even using Carrierwave.
This worked for me, with CarrierWave 0.5.8
model.update_attributes(:remote_uploader_url => "http://path/to/image.jpg")
Of course, you need to set remote_uploader_url to be attr_accessible for this.
I was looking for this as well.
The blocking point in the zencoder case would be that Carrierwave doesn't track different different file type versions for the original file. It only references the original file.
So having the original file as an .mp4 a a thumbnail version as a .png doesn't work.
While you can have an 'image.png' and also track 'thumb_png_image.png', you can't also create a 'thumb_jpg_image.jpg' for the same file.
Otherwise you could create a dummy version and using conditional versioning tell CW not to process it.
Since CW would create the dummy version anyway but not upload it, you could have it reference a path matching the file returned by Zencoder. But oh well...
At the end of this episode (7:35), Ryan Bates adds a remote_image_url in a file form upload:
http://railscasts.com/episodes/253-carrierwave-file-uploads