Paperclip generating crap urls on server, works on dev

Hi,

I have the following:

has_attached_file :data,
:styles => {:theora => {:format => "ogv"}, :still => {:format =>
"jpg"},
    :small => {:format => "jpg"}},
:url => "/uploads/products/:basename:suffix.:extension",
:path => ":rails_root/uploads/products/:basename:suffix.:extension"

Let's say I upload a video called video.mp4, now I want paperclip to
generate the url:

object.data.url(:still) => /some/url/video.jpg

But for some reason on my server, paperclip generates:
/some/url/video.mp4

Moreover, if I update code and restart the server, paperclip will
correctly generate the .jpg extension the first time, but if I reload
the page, it messes up and goes back to .mp4.

Everything works fine on my dev machine (mac mongrel+nginx), and this
strange behavior only happens on the server (freebsd passenger+nginx).

Did anyone experience such weird thing? It's driving me crazy!.

Okay so it has to do with caching. On my dev machine, if I switch to
production environment I get the same behavior. I don't know what's the
solution.

Fernando Perez wrote:

Okay so it has to do with caching. On my dev machine, if I switch to
production environment I get the same behavior. I don't know what's the
solution.

You're encountering this because Paperclip was originally built to
process a file into different sizes of itself, original, medium,
thumbnail etc. In that scenario the format stays the same, so if you use
a jpg you'll have a thumbnail jpg as well.

When you're looking at converting to a different format or, in your
case, capturing a still it doesn't check the mime-type for every style
it has. There is at least one branch on github where somebody has
attempted to solve this, but here is the approach I've followed:

1. Create an initializer at

# config/initializers/paperclip.rb

Paperclip.interpolates :style_extension do |attachment, style|
  default = File.extname(attachment.original_filename).gsub(/^\.+/, "")

  case style.to_s
  when /theora/ then 'ogv'
  when /still/ then 'jpg'
  else
    default
  end
end

2. Use that interpolation in your url and path options:
  :url => "/uploads/products/:basename:suffix.:style_extension",
  :path =>
":rails_root/uploads/products/:basename:suffix.:style_extension"

For reference on interpolations:
http://rdoc.info/projects/thoughtbot/paperclip

Hope it helps!

- Parker

Parker Selbert wrote:

attempted to solve this, but here is the approach I've followed:

Hi Parker, thanks for your message, I was considering doing it that way,
so I'll go for it.

Best regards,

thumbnail etc. In that scenario the format stays the same, so if you use
a jpg you'll have a thumbnail jpg as well.

Still what's strange is that it should use the :format option I
provided, that's what's written in the docs, so if a jpeg is upload, one
of its style can be a png. And by looking at its source code that's what
expected to occur.

In dev env it works, but once class caching is turned on, it works once,
then craps out. I'll see what the devs think about it.

We encountered this problem too and decided to dig deeper to fix it.
You are right about Rails class caching. The Style initializer removes
the :format, :geometry and :processors options from the options hash
and stores them as an instance variable. When Rails class caching is
on, every Style initialized after the first one will not have
the :format, :geometry and :processors options passed in, because they
were deleted from the class.

We've created an issue on Paperclip's Github:
http://github.com/thoughtbot/paperclip/issues/issue/277
We forked the repository and made the fix here:
git://github.com/pivotal-creationmix/paperclip.git

Ok so there is definitely a bug, thanks for sharing your experience. I
hope it gets fixed quickly.

Nice, thanks for sharing!