1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/lib/active_storage/variant.rb

35 lines
741 B
Ruby
Raw Normal View History

2017-07-11 12:53:17 -04:00
require "active_storage/blob"
require "mini_magick"
class ActiveStorage::Variant
attr_reader :blob, :variation
delegate :service, to: :blob
2017-07-21 16:51:31 -04:00
def initialize(blob, variation)
2017-07-11 12:53:17 -04:00
@blob, @variation = blob, variation
end
2017-07-20 18:35:15 -04:00
def processed
2017-07-21 16:51:31 -04:00
process unless service.exist?(key)
2017-07-20 18:35:15 -04:00
self
end
2017-07-21 16:51:31 -04:00
def key
"variants/#{blob.key}/#{variation.key}"
2017-07-11 12:53:17 -04:00
end
2017-07-21 16:51:31 -04:00
def url(expires_in: 5.minutes, disposition: :inline)
service.url key, expires_in: expires_in, disposition: disposition, filename: blob.filename
2017-07-11 12:53:17 -04:00
end
2017-07-21 13:35:00 -04:00
2017-07-11 12:53:17 -04:00
private
2017-07-20 18:35:15 -04:00
def process
2017-07-21 16:51:31 -04:00
service.upload key, transform(service.download(blob.key))
2017-07-11 12:53:17 -04:00
end
def transform(io)
2017-07-21 16:51:31 -04:00
File.open MiniMagick::Image.read(io).tap { |image| variation.transform(image) }.path
2017-07-11 12:53:17 -04:00
end
end