Module: Resizing::CarrierWave

Defined in:
lib/resizing/carrier_wave.rb,
lib/resizing/carrier_wave/storage/file.rb,
lib/resizing/carrier_wave/callback_file.rb,
lib/resizing/carrier_wave/storage/remote.rb

Overview

rubocop:disable Metrics/ModuleLength

Defined Under Namespace

Modules: ClassMethods, Storage Classes: CallbackFile, Railtie

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



25
26
27
28
# File 'lib/resizing/carrier_wave.rb', line 25

def self.included(base)
  base.storage Resizing::CarrierWave::Storage::Remote
  base.extend ClassMethods
end

Instance Method Details

#build_url ⇒ Object



72
73
74
# File 'lib/resizing/carrier_wave.rb', line 72

def build_url
  "#{Resizing.configure.image_host}#{model.read_attribute(serialization_column)}"
end

#cache!(new_file) ⇒ Object

Resizing has no cache storage, so the file is uploaded to Resizing directly here instead of being copied to a local workfile and handed to cache_storage.

The :cache callbacks are still run, so that the uploader settings hooked on them (extension_allowlist / extension_denylist / content_type_allowlist / content_type_denylist / size_range) are honoured before the upload happens. The callbacks that do not fit the Resizing flow (process! and cache_versions!) are disabled below.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/resizing/carrier_wave.rb', line 116

def cache!(new_file)
  return if new_file.nil?

  # CallbackFile responds to #extension / #content_type / #size, which the
  # before :cache callbacks need, with the extension derived from the content type
  # (see CallbackFile). The original object is handed to the storage, because it
  # carries the original filename sent to Resizing.
  sanitized = CallbackFile.new(new_file)
  return if sanitized.empty?

  with_callbacks(:cache, sanitized) do
    file = storage.store!(new_file)
    # do not assign @cache_id, bacause resizing do not support cache
    # save to resizing directly
    # @cache_id = file.public_id

    @filename = file.public_id.to_s
    @file = file
  end
end

#cache_versions!(*args) ⇒ Object

cache_versions! is called after :cache Disable on Resizing, because each version would upload the same image again and the resulting public_id would be stored nowhere, leaving orphan images behind. https://github.com/carrierwaveuploader/carrierwave/blob/28190e99299a6131c0424a5d10205f471e39f3cd/lib/carrierwave/uploader/versions.rb#L17



150
151
152
# File 'lib/resizing/carrier_wave.rb', line 150

def cache_versions!(*args)
  # NOP
end

#default_format ⇒ Object



182
183
184
# File 'lib/resizing/carrier_wave.rb', line 182

def default_format
  @default_format
end

#default_url ⇒ Object

need override this. if you want to return some url when target_column is nil



77
78
79
# File 'lib/resizing/carrier_wave.rb', line 77

def default_url
  nil
end

#file ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/resizing/carrier_wave.rb', line 45

def file
  file_identifier = @retrieved_identifier || identifier || read_column

  return nil if file_identifier.blank?

  @file ||= Resizing::CarrierWave::Storage::File.new(self)
  @file.retrieve(file_identifier)
  @file
end

#filename ⇒ Object



154
155
156
# File 'lib/resizing/carrier_wave.rb', line 154

def filename
  read_column
end

#format ⇒ Object



186
187
188
# File 'lib/resizing/carrier_wave.rb', line 186

def format
  requested_format || default_format
end

#initialize(*args) ⇒ Object



30
31
32
33
34
35
# File 'lib/resizing/carrier_wave.rb', line 30

def initialize(*args)
  @requested_format = nil
  @default_format = nil
  @retrieved_identifier = nil
  super
end

#process!(*args) ⇒ Object

process! is called before :cache Disable on Resizing, because process is repurposed as a definition of the transformation applied when the browser fetches the image URL (see #transform_string), so the processors must not be run as actual image processing on upload. https://github.com/carrierwaveuploader/carrierwave/blob/28190e99299a6131c0424a5d10205f471e39f3cd/lib/carrierwave/uploader/processing.rb#L12



142
143
144
# File 'lib/resizing/carrier_wave.rb', line 142

def process!(*args)
  # NOP
end

#read_column ⇒ Object



68
69
70
# File 'lib/resizing/carrier_wave.rb', line 68

def read_column
  model.read_attribute(serialization_column)
end

#remove_versions!(*args) ⇒ Object

store_versions! is called after delete Disable on Resizing, because transform the image when browser fetch the image URL https://github.com/carrierwaveuploader/carrierwave/blob/28190e99299a6131c0424a5d10205f471e39f3cd/lib/carrierwave/uploader/versions.rb#L18



172
173
174
# File 'lib/resizing/carrier_wave.rb', line 172

def remove_versions!(*args)
  # NOP
end

#rename ⇒ Object

Raises:

  • (NotImplementedError)


89
90
91
# File 'lib/resizing/carrier_wave.rb', line 89

def rename
  raise NotImplementedError, 'rename is not implemented'
end

#requested_format ⇒ Object



176
177
178
179
180
# File 'lib/resizing/carrier_wave.rb', line 176

def requested_format
  # TODO
  # The request with uploading format parameter is not working on the Resizing until 2020/09/25
  @requested_format
end

#resize_to_fill(*args) ⇒ Object



98
99
100
101
# File 'lib/resizing/carrier_wave.rb', line 98

def resize_to_fill(*args)
  @transform ||= []
  @transform.push(:resize_to_fill, *args)
end

#resize_to_fit(*args) ⇒ Object



103
104
105
106
# File 'lib/resizing/carrier_wave.rb', line 103

def resize_to_fit(*args)
  @transform ||= []
  @transform.push(:resize_to_fit, *args)
end

#resize_to_limit(*args) ⇒ Object



93
94
95
96
# File 'lib/resizing/carrier_wave.rb', line 93

def resize_to_limit(*args)
  @transform ||= []
  @transform.push(:resize_to_limit, *args)
end

#retrieve_from_store!(identifier) ⇒ Object

Override to store the identifier and set up @file for later use



38
39
40
41
42
43
# File 'lib/resizing/carrier_wave.rb', line 38

def retrieve_from_store!(identifier)
  @retrieved_identifier = identifier
  super
  # Ensure @file is set up so that remove! can call @file.delete
  file
end

#serialization_column ⇒ Object



158
159
160
# File 'lib/resizing/carrier_wave.rb', line 158

def serialization_column
  model.send(:_mounter, mounted_as).send(:serialization_column)
end

#store_versions!(*args) ⇒ Object

store_versions! is called after store! Disable on Resizing, because transform the image when browser fetch the image URL https://github.com/carrierwaveuploader/carrierwave/blob/28190e99299a6131c0424a5d10205f471e39f3cd/lib/carrierwave/uploader/versions.rb#L18



165
166
167
# File 'lib/resizing/carrier_wave.rb', line 165

def store_versions!(*args)
  # NOP
end

#transform_string ⇒ Object



81
82
83
84
85
86
87
# File 'lib/resizing/carrier_wave.rb', line 81

def transform_string
  transforms = processors.map do |processor|
    transform_string_from processor
  end

  transforms.join('/')
end

#url(*args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/resizing/carrier_wave.rb', line 55

def url(*args)
  return default_url unless read_column.present?

  transforms = args.map do |version|
    version = version.intern
    raise "No version is found: #{version}, #{versions.keys} are exists." unless versions.key? version

    versions[version].transform_string
  end.compact

  "#{build_url}/#{transforms.join('/')}"
end