How to check if image version exists on S3 with Carrierwave and Fog?
Asked Answered
G

2

10

I'm uploading my images with Carrierwave and Fog to S3. On the upload I also create a thumbnail version of the image:

version :thumb do
  process :resize_to_limit => [90, 80], if: :is_resizable?
end

Now I need a method to check if thumbnail version exists.

The Documentation lists the exists? method. This actually works, if I want to check the existence of the original version:

asset.file.exists? # => true

But when I use the "thumb" version like this:

asset.url(:thumb).file.exists?

it get:

undefined method 'exists?' for #<String:0x007fcd9f9d9620>:

Goldiegoldilocks answered 10/5, 2014 at 7:40 Comment(0)
G
18

Use this:

asset.thumb.file.exists?

instead of: asset.url(:thumb).file.exists?

Goldiegoldilocks answered 10/5, 2014 at 7:54 Comment(2)
you can use version_exists? method also. see github.com/carrierwaveuploader/carrierwave/blob/master/lib/…Inimitable
Using version_exists? is not the same thing. version_exists? only checks that the version has been defined on the uploader class itself; it does not check to see if the file is actually available.Pilsen
F
3

The correct answer is:

asset.file.thumb.file.exists?

where file = mounted_uploader and asset = model

Fictive answered 19/7, 2016 at 0:48 Comment(1)
If this code solves the problem, you should add some text to provide explanations of what each piece is doing. This way, you can help out those with similar problems in the future. In addition, you are more likely to get up votes to your answer.Polysemy

© 2022 - 2024 — McMap. All rights reserved.