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

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.



13
14
15
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 13

def internal_identifier
  @internal_identifier
end

Instance Method Details

#convert_to_webp(options = {}) ⇒ Object



109
110
111
112
113
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 109

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]



192
193
194
195
196
197
198
199
200
201
202
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 192

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



212
213
214
215
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 212

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

#dimensionsObject



221
222
223
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 221

def dimensions
  [width, height]
end

#extensionObject



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

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

#filenameObject



27
28
29
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 27

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

#full_filenameObject



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

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_partObject



38
39
40
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 38

def human_part
  normalize_filename(model.send("#{mounted_as}_file_name").to_s.strip.remove(/\.\w+$/)).remove(secure_token).chomp('_').presence
end

#image?(new_file = nil) ⇒ Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 217

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

#normalize_filename(raw_filename) ⇒ Object



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

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

#options_for_webp(options) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 115

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



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 160

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



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 91

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)


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

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

#rotate(degrees = nil) ⇒ Object

Rotate image by degress process rotate: “-90”



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

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



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

def save_original_name(file)
  model.original_name ||= file.original_filename if file.respond_to?(:original_filename)
end

#secure_tokenObject



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

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

#store_dirObject



104
105
106
107
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 104

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



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

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



149
150
151
152
153
154
155
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 149

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

#version_extensionObject



46
47
48
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 46

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

#version_filename_partObject



50
51
52
53
54
55
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 50

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



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

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)


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

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