54 lines
2 KiB
Ruby
54 lines
2 KiB
Ruby
# This is a subclass of Shrine base that will be further configured for it's requirements.
|
|
# This will be included in the model to manage the file.
|
|
|
|
class ImageUploader < Shrine
|
|
ALLOWED_TYPES = %w[image/jpeg image/png image/webp]
|
|
MAX_SIZE = 5*1024*1024 # 5 MB
|
|
MAX_DIMENSIONS = [2000, 2000] # 2000x2000
|
|
|
|
plugin :remove_attachment
|
|
plugin :pretty_location
|
|
plugin :validation_helpers
|
|
plugin :store_dimensions, log_subscriber: nil
|
|
# plugin :derivation_endpoint, prefix: "derivations/image"
|
|
|
|
# File validations (requires `validation_helpers` plugin)
|
|
Attacher.validate do
|
|
validate_size 0..MAX_SIZE
|
|
|
|
if validate_mime_type ALLOWED_TYPES, message: "must be a valid JPEG or PNG file"
|
|
validate_max_dimensions MAX_DIMENSIONS, message: "must be not larger than 2000x2000 pixels"
|
|
end
|
|
end
|
|
|
|
# Thumbnails processor (requires `derivatives` plugin)
|
|
Attacher.derivatives do |original|
|
|
puts "original=#{original}"
|
|
magick = ImageProcessing::MiniMagick.source(original)
|
|
{
|
|
thumb: magick.resize_to_limit!(160,120),
|
|
small: magick.resize_to_limit!(320, 240),
|
|
#medium: magick.resize_to_limit!(800, 600),
|
|
large: magick.composite!('public/logo/watermark.png', gravity: 'east')
|
|
}
|
|
# GenerateThumbnail.call(original, width, height) # lib/generate_thumbnail.rb
|
|
|
|
# THUMBNAILS.transform_values do |(width, height)|
|
|
# GenerateThumbnail.call(original, width, height) # lib/generate_thumbnail.rb
|
|
# end
|
|
end
|
|
|
|
# Default to dynamic thumbnail URL (requires `default_url` plugin)
|
|
# Attacher.default_url do |derivative: nil, **|
|
|
# file&.derivation_url(:thumbnail, *THUMBNAILS.fetch(derivative)) if derivative
|
|
# end
|
|
|
|
Attacher.default_url do |derivative: nil, **|
|
|
'/images/dummy/no-screenshots-upload-one.svg'
|
|
end
|
|
|
|
# Dynamic thumbnail definition (requires `derivation_endpoint` plugin)
|
|
# derivation :thumbnail do |file, width, height|
|
|
# GenerateThumbnail.call(file, width.to_i, height.to_i) # lib/generate_thumbnail.rb
|
|
# end
|
|
end
|