debshots/app/uploaders/image_uploader.rb

49 lines
1.7 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 = [3000, 3000] # 3000x3000
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
validate_max_dimensions MAX_DIMENSIONS
end
end
# Thumbnails processor (requires `derivatives` plugin)
Attacher.derivatives do |original|
puts "original=#{original}"
magick = ImageProcessing::MiniMagick.source(original)
{
small: magick.resize_to_limit!(160, 120),
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
# 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