aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_storage/variant.rb
blob: 4145ee644d1799bad881711529a59df02c9543fa (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require "active_storage/blob"
require "active_support/core_ext/object/inclusion"
require "mini_magick"

class ActiveStorage::Variant
  class_attribute :verifier

  ALLOWED_TRANSFORMATIONS = %i(
    resize rotate format flip fill monochrome orient quality roll scale sharpen shave shear size thumbnail
    transparent transpose transverse trim background bordercolor compress crop
  )

  attr_reader :blob, :variation
  delegate :service, to: :blob

  def self.find_or_process_by!(blob_key:, encoded_variant_key:)
    new(ActiveStorage::Blob.find_by!(key: blob_key), variation: verifier.verify(encoded_variant_key)).processed
  end

  def self.encode_key(variation)
    verifier.generate(variation)
  end

  def initialize(blob, variation:)
    @blob, @variation = blob, variation
  end

  def processed
    process unless processed?
    self
  end

  def url(expires_in: 5.minutes, disposition: :inline)
    service.url blob_variant_key, expires_in: expires_in, disposition: disposition, filename: blob.filename
  end

  def key
    verifier.generate(variation)
  end


  private
    def processed?
      service.exist?(blob_variant_key)
    end

    def process
      upload_variant transform(download_blob)
    end

    def download_blob
      service.download(blob.key)
    end

    def upload_variant(variant)
      service.upload blob_variant_key, variant
    end

    def blob_variant_key
      "variants/#{blob.key}/#{key}"
    end

    def transform(io)
      File.open \
        MiniMagick::Image.read(io).tap { |transforming_image|
          variation.each do |(method, argument)|
            if method = allowed_transformational_method(method.to_sym)
              if argument.blank? || argument == true
                transforming_image.public_send(method)
              else
                # FIXME: Consider whitelisting allowed arguments as well?
                transforming_image.public_send(method, argument)
              end
            end
          end
        }.path
    end

    def allowed_transformational_method(method)
      method.presence_in(ALLOWED_TRANSFORMATIONS)
    end
end