Christian Fazzini
Christian Fazzini

Reputation: 19723

How to assign a remote file to Carrierwave?

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?

Upvotes: 25

Views: 32204

Answers (4)

asif
asif

Reputation: 111

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.

Upvotes: 11

Benjamin Crouzier
Benjamin Crouzier

Reputation: 41905

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

Upvotes: 0

tomek
tomek

Reputation: 758

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...

Upvotes: 0

ctide
ctide

Reputation: 5257

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.

Upvotes: 49

Related Questions