Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Versioning bug #2126

Closed
miguelpeniche opened this issue Feb 17, 2017 · 4 comments
Closed

Versioning bug #2126

miguelpeniche opened this issue Feb 17, 2017 · 4 comments

Comments

@miguelpeniche
Copy link

Carrierwave is having a lot of bugs when versioning images, this is another issue that may have priority. When you recreate avatars, it uploads the correct files to S3, but then the avatar version urls for some strange reason changes. This is what happens working in console:

class UserAvatarUploader < CarrierWave::Uploader::Base

    include CarrierWave::MiniMagick

    version :large do
        process resize_to_fit: [ 600, 600 ], convert: :jpg
    end

    version :small do
        process resize_to_fill: [ 216, 216 ], convert: :jpg
    end

    version :thumb do
        process resize_to_fill: [ 80, 80 ], convert: :png
    end

    version :tiny do
        process resize_to_fill: [ 50, 50 ], convert: :png
    end

    def filename
    "#{secure_token}.#{file.extension}" if original_filename.present?
    end

    protected

    def secure_token
        var = :"@#{mounted_as}_secure_token"
        model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid)
    end

end
> user = User.find(id)
> user.avatar.recreate_versions!
> user.save!
> user.avatar.large.url
=> "http://bucket.s3.amazonaws.../large_96a1f5a4-cf07-490f-9711-618e04071950.jpg"
> user.avatar.small.url
=> "http://bucket.s3.amazonaws.../small_96a1f5a4-cf07-490f-9711-618e04071950.jpg"
> user.avatar.thumb.url
=> "http://bucket.s3.amazonaws.../thumb_96a1f5a4-cf07-490f-9711-618e04071950.png"
> user.avatar.tiny.url
=> "http://bucket.s3.amazonaws.../tiny_96a1f5a4-cf07-490f-9711-618e04071950.png"
> reload!
> user = User.find(id)
> user.avatar.large.url
=> "http://bucket.s3.amazonaws.../large_96a1f5a4-cf07-490f-9711-618e04071950.jpg"
> user.avatar.small.url
=> "http://bucket.s3.amazonaws.../small_96a1f5a4-cf07-490f-9711-618e04071950.jpg"
> user.avatar.thumb.url
=> "http://bucket.s3.amazonaws.../thumb_96a1f5a4-cf07-490f-9711-618e04071950.jpg"
> user.avatar.tiny.url
=> "http://bucket.s3.amazonaws.../tiny_96a1f5a4-cf07-490f-9711-618e04071950.jpg"
@ThomasBush
Copy link

@bengalamx - I am experiencing a similar issue with nearly identical code. All my images are uploaded to S3 in the properly converted format and extension, but the .url method returns only the extension of the original file, so all conversions to a format that differs from the original are incorrect. I am using the following versions: rails (5.0.2), carrierwave (1.1.0), ruby (2.3.1), fog-core (1.45.0), fog-aws (1.4.1). Did you ever figured out a solution here? I have found quite a few examples of this issue, but never solutions that work in my case.

@miguelpeniche
Copy link
Author

@ThomasBush did you find a solution? I still have this problem.

@ThomasBush
Copy link

@bengalamx yes, the only way I could get this to work was to override the file name inside each version to replace the incorrect extension with the correct one. I have included an example below.

version :square do
    process resize_and_pad: [400, 400, '#FFFFFF', 'Center']
    process :convert => 'jpg'

    def full_filename(for_file = model.file_name.file)
      "square_#{for_file.sub('png', 'jpg')}"
    end
end

@miguelpeniche
Copy link
Author

I just added some scopes to my User model, so I do User.random.avatar_small instead of User.random.avatar.small.url

def avatar_large
  self.has_avatar? ? self.avatar.large.url.gsub(/.png|.jpeg/, '.jpg') : self.avatar.large
end

def avatar_small
  self.has_avatar? ? self.avatar.small.url.gsub(/.png|.jpeg/, '.jpg') : self.avatar.small
end

def avatar_thumb
  self.has_avatar? ? self.avatar.thumb.url.gsub(/.jpg|.jpeg/, '.png') : self.avatar.thumb
end

def avatar_tiny
  self.has_avatar? ? self.avatar.tiny.url.gsub(/.jpg|.jpeg/, '.png') : self.avatar.tiny
end

But yours seems like a good solution. I'll give it a try. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants