-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathimage_uploader.rb
49 lines (40 loc) · 1.46 KB
/
image_uploader.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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.
require "./config/shrine"
require "./lib/generate_thumbnail"
class ImageUploader < Shrine
ALLOWED_TYPES = %w[image/jpeg image/png image/webp]
MAX_SIZE = 10*1024*1024 # 10 MB
MAX_DIMENSIONS = [5000, 5000] # 5000x5000
THUMBNAILS = {
small: [300, 300],
medium: [600, 600],
large: [800, 800],
}
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|
THUMBNAILS.transform_values do |(width, height)|
GenerateThumbnail.call(original, width, height)
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)
end
end