blob: 8be51eba92fda429d2df8634545f31ad221fff67 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
require "active_storage/blob"
require "mini_magick"
class ActiveStorage::Variant
attr_reader :blob, :variation
delegate :service, to: :blob
class << self
def find_or_process_by!(blob_key:, variation_key:)
new(ActiveStorage::Blob.find_by!(key: blob_key), variation: ActiveStorage::Variation.decode(variation_key)).processed
end
end
def initialize(blob, variation)
@blob, @variation = blob, variation
end
def processed
process unless service.exist?(key)
self
end
def key
"variants/#{blob.key}/#{variation.key}"
end
def url(expires_in: 5.minutes, disposition: :inline)
service.url key, expires_in: expires_in, disposition: disposition, filename: blob.filename
end
private
def process
service.upload key, transform(service.download(blob.key))
end
def transform(io)
File.open MiniMagick::Image.read(io).tap { |image| variation.transform(image) }.path
end
end
|