[CarrierWave] One uploader for different file types

197 views
Skip to first unread message

oltbaba

unread,
Apr 27, 2010, 9:47:21 AM4/27/10
to carrierwave
My uploader handles images, audio and video files.

For images I create different versions via:

version :size_350 do
...
end

However, when the uploader is handling an audio or video file it must
not create these versions.
I want to handle audio and video files differently in my encoding
backend.

How can I restrict the execution of a defined version?

--
You received this message because you are subscribed to the Google Groups "carrierwave" group.
To post to this group, send email to carri...@googlegroups.com.
To unsubscribe from this group, send email to carrierwave...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/carrierwave?hl=en.

Jonas Nicklas

unread,
Apr 28, 2010, 6:23:51 AM4/28/10
to carri...@googlegroups.com
There's currently no good (read: non-hackish) way of doing this,
patches welcome!

maybe something like:

class MyUploader
version :size_350, :if => :image? do
...
end

def image?
...
end

/Jonas

Tomasz

unread,
Apr 28, 2010, 9:40:01 AM4/28/10
to carrierwave
Hi, firstly thanks for clean and good CarrierWave API :)

My proposition is to give more flexible way, maybe of doing the proc
or lambda way. What do Your think?

version :size_350, :if => Proc.new{|uploader| uploader.image? ||
uploader.model.image? } do
...
end

Tomasz
> > For more options, visit this group athttp://groups.google.com/group/carrierwave?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups "carrierwave" group.
> To post to this group, send email to carri...@googlegroups.com.
> To unsubscribe from this group, send email to carrierwave...@googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/carrierwave?hl=en.

Jonas Nicklas

unread,
Apr 28, 2010, 9:56:46 AM4/28/10
to carri...@googlegroups.com
I don't see what the proc gives you in terms of flexibility that the
callback doesn't, you don't really have a use for the closure here, do
you? I find procs kind of ugly as an API to be honest, and since
version already takes a block, a callback seems like the logical
solution.

/Jonas

Tomasz

unread,
Apr 28, 2010, 2:41:47 PM4/28/10
to carrierwave
I've given myself a while and now I see that I'm with You about adding
a logical option.

For now I'm struggling myself with situation where my Uploader is kind
of an asset, so is it serves for different kind of files.
The :if would be great addition for solving lot of issues and it will
greatly enrich Uploader.

:)

Tomasz

oltbaba

unread,
Apr 29, 2010, 2:57:52 AM4/29/10
to carrierwave
Being able to decide whether to create a version or not based on the
models attributes would be perfect.

iwar...@gmail.com

unread,
May 4, 2010, 2:54:16 PM5/4/10
to carrierwave
I am looking to do this exact same thing. Has anyone made any progress
into implementing it?

Ian

oltbaba

unread,
May 19, 2010, 6:01:59 AM5/19/10
to carrierwave
I haven't really solved this issue, but here's my workaround.

I'm using two different uploaders within the same model, like this:
mount_uploader :media, MediaUploader # upload handler for video &
audio files
mount_uploader :image_media, ImageMediaUploader # upload handler for
image files

Upon creation of the record via my controller I decide which uploader
to user, like this:

def create
mime_type =
MIME::Types.type_for(params[:Filedata].original_filename)
if mime_type.size >= 1 && mime_type[0].media_type.eql?("image") #
image
@media_asset.image_media = params[:Filedata]
else # video or audio
@media_asset.media = params[:Filedata]
end
params[:Filedata].content_type = mime_type.to_s
@media_asset.save!
...
end

Its ugly and it would be better if this would be solved via the
Carrierwave uploader.
Sometimes time does matter and I currently don't have the time to
implement a cleaner solution.
However I hope this helps you.
Reply all
Reply to author
Forward
0 new messages