Class: AbAdmin::CarrierWave::BaseUploader
- Inherits:
-
CarrierWave::Uploader::Base
- Object
- CarrierWave::Uploader::Base
- AbAdmin::CarrierWave::BaseUploader
show all
- Includes:
- Utils::EvalHelpers, CarrierWave::MiniMagick
- Defined in:
- lib/ab_admin/carrierwave/base_uploader.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
#call_method_or_proc_on, #evaluate_method
Instance Attribute Details
#internal_identifier ⇒ Object
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_part ⇒ Object
37
38
39
40
41
42
43
|
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 37
def base_filename_part
if version_name
version_name.to_s.start_with?('retina_') ? "#{version_name.to_s.sub(/^retina_/, '')}@2x" : version_name.to_s
else
secure_token
end
end
|
#cropper(*geometry) ⇒ Object
176
177
178
179
180
181
182
183
184
185
186
|
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 176
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_url ⇒ Object
196
197
198
199
|
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 196
def default_url
image_name = [model.class.to_s.underscore, version_name].compact.join('_')
"/assets/defaults/#{image_name}.png"
end
|
#dimensions ⇒ Object
205
206
207
|
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 205
def dimensions
[magick[:width], magick[:height]]
end
|
74
75
76
|
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 74
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
45
46
47
|
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 45
def full_filename(for_file=filename)
human_filenames ? human_full_filename(for_file) : strict_filename(for_file)
end
|
#full_original_filename ⇒ Object
56
57
58
|
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 56
def full_original_filename
"#{base_filename_part}#{File.extname(store_filename)}"
end
|
#human_full_filename(for_file = filename) ⇒ Object
49
50
51
52
53
54
|
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 49
def human_full_filename(for_file=filename)
ext = File.extname(for_file).downcase
human_filename_part = for_file.chomp(ext)
tech_filename_part = "#{base_filename_part}#{ext}"
human_filename_part == secure_token ? tech_filename_part : "#{human_filename_part}_#{tech_filename_part}"
end
|
#image?(new_file = nil) ⇒ Boolean
201
202
203
|
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 201
def image?(new_file = nil)
AbAdmin.image_types.include?((file || new_file).content_type)
end
|
209
210
211
212
|
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 209
def magick
::MiniMagick::Image.new(current_path)
end
|
#normalize_filename(raw_filename) ⇒ Object
91
92
93
94
|
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 91
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
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 144
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 97
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
120
121
122
|
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 120
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”
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 159
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_token ⇒ Object
use secure token in the filename for non processed image
61
62
63
|
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 61
def secure_token
model.data_secure_token ||= AbAdmin.friendly_token(20).downcase
end
|
#store_dir ⇒ Object
prevent large number of subdirectories
125
126
127
128
|
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 125
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
65
66
67
68
69
70
|
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 65
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
|
Strips out all embedded information from the image process :strip
133
134
135
136
137
138
139
|
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 133
def strip
manipulate! do |img|
img.strip
img = yield(img) if block_given?
img
end
end
|
#watermark(watermark_path, gravity = 'SouthEast') ⇒ Object
188
189
190
191
192
193
194
|
# File 'lib/ab_admin/carrierwave/base_uploader.rb', line 188
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
|