Class: SiteBlog::ImageUploader

Inherits:
CarrierWave::Uploader::Base
  • Object
show all
Includes:
CarrierWave::MiniMagick
Defined in:
app/uploaders/site_blog/image_uploader.rb

Constant Summary collapse

ALLOWED_FORMAT_FILES =
['jpg', 'jpeg', 'png', 'gif']
MAX_FILE_SIZE =
Rails.env.test? ? 0.5.megabytes : 5.megabytes
MIN_RESOLUTION =

height, width in pixels

[500, 500]
MAX_QUALITY =

%

90
MIN_QUALITY =

%

70

Instance Method Summary collapse

Constructor Details

#initialize(a, b) ⇒ ImageUploader

Returns a new instance of ImageUploader.



21
22
23
24
25
26
27
28
29
# File 'app/uploaders/site_blog/image_uploader.rb', line 21

def initialize a, b
  @min_height    = MIN_RESOLUTION[0]
  @min_width     = MIN_RESOLUTION[1]
  @max_file_size = MAX_FILE_SIZE
  @min_quality   = MIN_QUALITY
  @max_quality   = MAX_QUALITY
  @attribute = :images
  super a, b
end

Instance Method Details

#check_file_sizeObject



39
40
41
42
43
44
45
# File 'app/uploaders/site_blog/image_uploader.rb', line 39

def check_file_size
  return unless @max_file_size
  return unless File.size?(current_path)&.> @max_file_size
  model.errors.add @attribute, I18n.t('errors.messages.max_size_error',
                                      max_size: @max_file_size.unmegabyte,
                                      filename: original_filename)
end

#check_resolutionObject



47
48
49
50
51
52
53
54
55
# File 'app/uploaders/site_blog/image_uploader.rb', line 47

def check_resolution
  @img = ::MiniMagick::Image.open current_path
  if (@max_height&.< @img.height) || (@max_width&.< @img.width)
    model.errors.add @attribute, I18n.t('errors.messages.max_resolution', height: @max_height, width: @max_width)
  end
  if (@min_height&.> @img.height) || (@min_width&.> @img.width)
    model.errors.add @attribute, I18n.t('errors.messages.min_resolution', height: @min_height, width: @min_width)
  end
end

#content_type_allowlistObject



17
18
19
# File 'app/uploaders/site_blog/image_uploader.rb', line 17

def content_type_allowlist
  ALLOWED_FORMAT_FILES.map {|ext| "image/#{ext}"}
end

#extension_allowlistObject



13
14
15
# File 'app/uploaders/site_blog/image_uploader.rb', line 13

def extension_allowlist
  ALLOWED_FORMAT_FILES
end

#filenameObject



69
70
71
72
73
# File 'app/uploaders/site_blog/image_uploader.rb', line 69

def filename
  if original_filename
    "#{model.class.name.downcase}.#{Digest::MD5.hexdigest(original_filename)[0..7]}.#{file.extension}"
  end
end

#qualityObject



57
58
59
60
61
62
63
64
65
66
67
# File 'app/uploaders/site_blog/image_uploader.rb', line 57

def quality
  # Для маленьких файлов качество приближается к max (%), по мере роста размера файла, качество линейно
  # падает. Для максимально разрешенного размера файла это значение составляет min (%)
  image = MiniMagick::Image.open current_path
  out_quality = (@max_quality - (@max_quality - @min_quality) * image.size / @max_file_size).round
  if image.data['quality'].blank? || image.data['quality'] > out_quality
    `mogrify -quality #{out_quality} #{current_path}`
  end
rescue
  nil
end

#store_dirObject



75
76
77
78
# File 'app/uploaders/site_blog/image_uploader.rb', line 75

def store_dir
  return "test/files/db#{ENV['TEST_ENV_NUMBER']}/#{model.class.name.downcase}/#{model.id}" if Rails.env.test?
  "files/blog/articles/#{model.id}"
end