Migration to Shrine attachment handling
This commit is contained in:
parent
2d53397dda
commit
c4f5741b0c
13 changed files with 175 additions and 181 deletions
49
app/uploaders/image_uploader.rb
Normal file
49
app/uploaders/image_uploader.rb
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
# 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue