Class: AbAdmin::CarrierWave::BaseUploader

Inherits:
CarrierWave::Uploader::Base
  • Object
show all
Includes:
Utils::EvalHelpers, CarrierWave::MiniMagick
Defined in:
lib/ab_admin/carrierwave/base_uploader.rb

Constant Summary collapse

CACHE_ID_REGEXP =
/\A(?:\d+[-_]){4}/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::EvalHelpers

#evaluate_method

Instance Attribute Details

#internal_identifierObject

Returns the value of attribute internal_identifier.



15
16
17
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 15

def internal_identifier
  @internal_identifier
end

Instance Method Details

#convert_to_webp(options = {}) ⇒ Object



136
137
138
139
140
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 136

def convert_to_webp(options = {})
  webp_path = "#{File.dirname(path)}/#{full_filename}"
  WebP.encode(path, webp_path, options_for_webp(options))
  @file = ::CarrierWave::SanitizedFile.new(tempfile: webp_path, filename: webp_path, content_type: 'image/webp')
end

#cropper(*geometry) ⇒ Object

Crop image by specific coordinates www.imagemagick.org/script/command-line-processing.php?ImageMagick=6ddk6c680muj4eu2vr54vdveb7#geometry process cropper: [size, offset] process cropper: [800, 600, 10, 20]



219
220
221
222
223
224
225
226
227
228
229
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 219

def cropper(*geometry)
  geometry = normalize_param(geometry[0]) if geometry.size == 1

  if geometry && geometry.size == 4
    minimagick! do |img|
      img.crop '%ix%i+%i+%i' % geometry
      img = yield(img) if block_given?
      img
    end
  end
end

#default_urlObject



239
240
241
242
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 239

def default_url
  image_name = [model.class.to_s.underscore, version_name].compact.join('_')
  "/assets/defaults/#{image_name}.png"
end

#dimensionsObject



248
249
250
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 248

def dimensions
  [width, height]
end

#extensionObject



68
69
70
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 68

def extension
  File.extname(model.original_name).downcase
end

#filenameObject



29
30
31
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 29

def filename
  "#{[human_part, secure_token].compact.join('_')}#{extension}"
end

#full_filenameObject



39
40
41
42
43
44
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 39

def full_filename(*)
  return filename unless version_name
  base = "#{version_filename_part}#{version_extension}"
  return base unless human_filenames
  [human_part, base].compact.join('_')
end

#human_part(index = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 46

def human_part(index = nil)
  raw = model.public_send("#{mounted_as}_file_name").to_s.strip
  raw = strip_extension(raw)
  raw = strip_cache_id(raw)
  normalized = normalize_filename(raw)
  normalized = normalized.remove(secure_token).chomp('_')
  normalized = strip_index(normalized) if index
  [normalized, index].compact.join('_')
end

#identifierObject



33
34
35
36
37
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 33

def identifier
  raw = super
  return raw unless raw.is_a?(String) && raw.match?(CACHE_ID_REGEXP)
  raw.sub(CACHE_ID_REGEXP, '')
end

#image?(new_file = nil) ⇒ Boolean

Returns:

  • (Boolean)


244
245
246
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 244

def image?(new_file = nil)
  AbAdmin.image_types.include?((file || new_file).content_type)
end

#normalize_filename(raw_filename) ⇒ Object



114
115
116
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 114

def normalize_filename(raw_filename)
  I18n.transliterate(raw_filename.unicode_normalize).parameterize(separator: '_').gsub(/[\-_]+/, '_').downcase
end

#options_for_webp(options) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 142

def options_for_webp(options)
  w, h = width, height
  options = options.dup
  ratio = w.to_f / h
  if options[:resize_to_fill]
    res_w, res_h = options[:resize_to_fill]
    res_ratio = res_w.to_f / res_h
    options.update(resize_w: res_w, resize_h: res_h) unless w == res_w && h == res_h
    if ratio > res_ratio
      crop_res_w = h * res_ratio
      crop_res_h = h
      options.update(crop_x: ((w - crop_res_w) / 2).to_i, crop_y: 0, crop_w: crop_res_w.to_i, crop_h: crop_res_h.to_i)
    elsif ratio < res_ratio
      crop_res_w = w
      crop_res_h = w / res_ratio
      options.update(crop_x: 0, crop_y: ((h - crop_res_h) / 2).to_i, crop_w: crop_res_w.to_i, crop_h: crop_res_h.to_i)
    end
  elsif options[:resize_to_fit]
    res_w, res_h = options[:resize_to_fit]
    res_ratio = res_w.to_f / res_h
    if ratio == res_ratio
      options.update(resize_w: res_w, resize_h: res_h) unless w == res_w && h == res_h
    elsif ratio > res_ratio
      options.update(resize_w: res_h)
    elsif ratio < res_ratio
      options.update(resize_h: res_w)
    end
  end
  options.except(:resize_to_fill, :resize_to_fit)
end

#quality(percentage) ⇒ Object

Reduces the quality of the image to the percentage given process quality: 85



187
188
189
190
191
192
193
194
195
196
197
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 187

def quality(percentage)
  percentage = normalize_param(percentage)

  unless percentage.blank?
    minimagick! do |img|
      img.quality percentage.to_s
      img = yield(img) if block_given?
      img
    end
  end
end

#rename_via_move(new_filename) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 118

def rename_via_move(new_filename)
  dir = File.dirname(path)
  old_names = versions.values.unshift(self).map(&:full_filename)
  model.send("#{mounted_as}_file_name=", "#{[new_filename.presence, secure_token].compact.join('_')}#{extension}")
  new_names = versions.values.unshift(self).map(&:full_filename)
  old_names.zip(new_names).each do |old_name, new_name|
    old_path, new_path = File.join(dir, old_name), File.join(dir, new_name)
    next if old_path == new_path || !File.exist?(old_path)
    FileUtils.mv(old_path, new_path)
  end
  retrieve_from_store!(model.send("#{mounted_as}_file_name"))
end

#retina?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 87

def retina?
  version_name.to_s.start_with?('retina_')
end

#rotate(degrees = nil) ⇒ Object

Rotate image by degress process rotate: “-90”



202
203
204
205
206
207
208
209
210
211
212
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 202

def rotate(degrees = nil)
  degrees = normalize_param(degrees)

  unless degrees.blank?
    manipulate! do |img|
      self.class.included_modules.map(&:to_s).include?('CarrierWave::RMagick') ? img.rotate!(degrees.to_i) : img.rotate(degrees.to_s)
      img = yield(img) if block_given?
      img
    end
  end
end

#save_original_name(file) ⇒ Object



102
103
104
105
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 102

def save_original_name(file)
  return unless file.respond_to?(:original_filename)
  model.original_name ||= strip_cache_id(file.original_filename.to_s)
end

#secure_tokenObject



83
84
85
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 83

def secure_token
  model.data_secure_token ||= AbAdmin.friendly_token(20).downcase
end

#store_dirObject



131
132
133
134
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 131

def store_dir
  str_id = model.id.to_s.rjust(4, '0')
  [AbAdmin.uploads_dir, model.class.to_s.underscore, str_id[0..2], str_id[3..-1]].join('/')
end

#store_model_filename(record) ⇒ Object



95
96
97
98
99
100
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 95

def store_model_filename(record)
  old_file_name = filename
  new_file_name = model_filename(old_file_name, record)
  return if new_file_name.blank? || new_file_name == old_file_name
  rename_via_move(new_file_name)
end

#stripObject

Strips out all embedded information from the image process :strip



176
177
178
179
180
181
182
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 176

def strip
  minimagick! do |img|
    img.strip
    img = yield(img) if block_given?
    img
  end
end

#strip_cache_id(name) ⇒ Object



56
57
58
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 56

def strip_cache_id(name)
  name.remove(CACHE_ID_REGEXP)
end

#strip_extension(name) ⇒ Object



60
61
62
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 60

def strip_extension(name)
  name.remove(/\.\w+$/)
end

#strip_index(name) ⇒ Object



64
65
66
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 64

def strip_index(name)
  name.remove(/_\d{3}$/)
end

#version_extensionObject



72
73
74
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 72

def version_extension
  webp? ? '.webp' : extension
end

#version_filename_partObject



76
77
78
79
80
81
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 76

def version_filename_part
  return secure_token unless version_name
  strict_version_name = version_name.to_s.remove('retina_').remove('_webp')
  strict_version_name = nil if strict_version_name.to_sym == :default
  "#{strict_version_name}#{'@2x' if retina?}"
end

#watermark(watermark_path, gravity = 'SouthEast') ⇒ Object



231
232
233
234
235
236
237
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 231

def watermark(watermark_path, gravity='SouthEast')
  minimagick! do |img|
    resolved_path = watermark_path.is_a?(Symbol) ? send(watermark_path) : watermark_path
    watermark_image = ::MiniMagick::Image.open(resolved_path)
    img.composite(watermark_image) { |c| c.gravity gravity }
  end
end

#webp?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 91

def webp?
  version_name.to_s.end_with?('_webp')
end