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

#call_method_or_proc_on, #evaluate_method

Instance Attribute Details

#internal_identifierObject

Returns the value of attribute internal_identifier.



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

def internal_identifier
  @internal_identifier
end

Instance Method Details

#base_filename_partObject



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

def base_filename_part
  return if version_name == :default
  return secure_token unless version_name
  version_name.to_s.start_with?('retina_') ? "#{version_name.to_s.sub(/^retina_/, '')}@2x" : version_name.to_s
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]



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

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

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

#default_urlObject



194
195
196
197
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 194

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

#dimensionsObject



203
204
205
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 203

def dimensions
  [magick[:width], magick[:height]]
end

#filenameObject



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

def filename
  internal_identifier || model.send("#{mounted_as}_file_name") || (store_filename && "#{secure_token}#{File.extname(store_filename).downcase}")
end

#full_filename(for_file = filename) ⇒ Object



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

def full_filename(for_file=filename)
  human_filenames ? human_full_filename(for_file) : strict_filename(for_file)
end

#full_original_filenameObject



54
55
56
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 54

def full_original_filename
  "#{base_filename_part}#{File.extname(store_filename)}"
end

#human_full_filename(for_file = filename) ⇒ Object



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

def human_full_filename(for_file=filename)
  ext = File.extname(for_file)
  system_part = base_filename_part
  human_filename_part = for_file.chomp(ext)
  return "#{system_part || version_name}#{ext}" if human_filename_part == secure_token
  system_part ? "#{human_filename_part}_#{system_part}#{ext}" : "#{human_filename_part}#{ext}"
end

#image?(new_file = nil) ⇒ Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 199

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

#magickObject



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

def magick
  #@magick ||= ::MiniMagick::Image.new(current_path)
  ::MiniMagick::Image.new(current_path)
end

#normalize_filename(raw_filename) ⇒ Object



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

def normalize_filename(raw_filename)
  parameterize_args = ActiveSupport::VERSION::MAJOR > 4 ? {separator: '_'} : '_'
  I18n.transliterate(raw_filename).parameterize(**parameterize_args).gsub(/[\-_]+/, '_').downcase
end

#quality(percentage) ⇒ Object

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



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 142

def quality(percentage)
  percentage = normalize_param(percentage)

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

#rename_via_move(new_file_name) ⇒ Object

rename files via move



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 95

def rename_via_move(new_file_name)
  if human_filenames
    dir = File.dirname(path)

    moves = []
    versions.values.unshift(self).each do |v|
      from_path = File.join(dir, v.full_filename)
      to_path = File.join(dir, v.full_filename(new_file_name))
      next if from_path == to_path || !File.exists?(from_path)
      moves << [from_path, to_path]
    end
    moves.each { |move| FileUtils.mv(*move) }
  end

  write_internal_identifier new_file_name
  model.send("write_#{mounted_as}_identifier")
  retrieve_from_store!(new_file_name) if human_filenames

  new_file_name
end

#rmagick_included?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 118

def rmagick_included?
  self.class.included_modules.map(&:to_s).include?('CarrierWave::RMagick')
end

#rotate(degrees = nil) ⇒ Object

Rotate image by degress process rotate: “-90”



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 157

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

  unless degrees.blank?
    manipulate! do |img|
      rmagick_included? ? 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



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

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

#secure_tokenObject

use secure token in the filename for non processed image



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

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

#store_dirObject

prevent large number of subdirectories



123
124
125
126
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 123

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



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

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

#strict_filename(for_file = filename) ⇒ Object



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

def strict_filename(for_file=filename)
  "#{version_name || secure_token}#{File.extname(for_file).downcase}"
end

#stripObject

Strips out all embedded information from the image process :strip



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

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

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



186
187
188
189
190
191
192
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 186

def watermark(watermark_path, gravity='SouthEast')
  manipulate! 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