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.



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

def internal_identifier
  @internal_identifier
end

Instance Method Details

#base_filename_partObject



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

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]



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

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



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

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

#dimensionsObject



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

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

#filenameObject



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

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



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

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

#full_original_filenameObject



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

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

#human_full_filename(for_file = filename) ⇒ Object



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

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)


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

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

#magickObject



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

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

#normalize_filename(raw_filename) ⇒ Object



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

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



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

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



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

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)


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

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”



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

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



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

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



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

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

#store_dirObject

prevent large number of subdirectories



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

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



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

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



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

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



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

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

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



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

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