Skip to content

Instantly share code, notes, and snippets.

@airled
Created December 5, 2019 14:18
Show Gist options
  • Save airled/911cd9bd819f89155e2ddadae0740f66 to your computer and use it in GitHub Desktop.
Save airled/911cd9bd819f89155e2ddadae0740f66 to your computer and use it in GitHub Desktop.
# Carrierwave works incorrectly with file formatting. If you do not override
# path and url methods it will return you incorrect path and url to webp file
# respectively. Also you should add removing callback because Carrierwave could
# not delete webp file by itself because of incorrect path.
# Remove this hacks if this issue will be fixed.
class ItemGalleryUploader < ApplicationUploader
WEBP_VERSION_NAME = :webpver
def filename
splitted = original_filename.split('.')
splitted.pop
"#{splitted.join('.')}.#{file.extension}"
end
before :remove, :clear_webp_file
version WEBP_VERSION_NAME do
process convert: :webp
def path
incorrect_path = super.split('.')
incorrect_path.pop
incorrect_path.join('.') + '.webp'
end
def url
incorrect_path = super.split('.')
incorrect_path.pop
incorrect_path.join('.') + '.webp'
end
end
def clear_webp_file
path = self.try(WEBP_VERSION_NAME).try(:path)
return unless path && File.exist?(path)
File.unlink(path)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment