Class: Decidim::ImageUploader
- Inherits:
-
ApplicationUploader
- Object
- CarrierWave::Uploader::Base
- ApplicationUploader
- Decidim::ImageUploader
- Includes:
- CarrierWave::MiniMagick
- Defined in:
- app/uploaders/decidim/image_uploader.rb
Overview
This class deals with uploading hero images to ParticipatoryProcesses.
Direct Known Subclasses
AvatarUploader, NewsletterTemplateImageUploader, OrganizationFaviconUploader, RecordImageUploader
Instance Method Summary collapse
-
#content_type_whitelist ⇒ Object
CarrierWave automatically calls this method and validates the content type fo the temp file to match against any of these options.
-
#dimensions_info ⇒ Object
Fetches info about different versions, their processors and dimensions.
-
#extension_whitelist ⇒ Object
Add a white list of extensions which are allowed to be uploaded.
- #manipulate! ⇒ Object
- #max_image_height_or_width ⇒ Object
-
#strip ⇒ Object
Strips out all embedded information from the image.
-
#validate_dimensions ⇒ Object
A simple check to avoid DoS with maliciously crafted images, or just to avoid reckless users that upload gigapixels images.
- #validate_size ⇒ Object
Methods inherited from ApplicationUploader
#skip_ssrf_protection?, #store_dir
Instance Method Details
#content_type_whitelist ⇒ Object
CarrierWave automatically calls this method and validates the content type fo the temp file to match against any of these options.
13 14 15 |
# File 'app/uploaders/decidim/image_uploader.rb', line 13 def content_type_whitelist extension_whitelist.map { |ext| "image/#{ext}" } end |
#dimensions_info ⇒ Object
Fetches info about different versions, their processors and dimensions
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/uploaders/decidim/image_uploader.rb', line 26 def dimensions_info if versions.any? versions.map do |version, info| [ version, { processor: info.processors[0][0], dimensions: info.processors[0][1] } ] end.to_h else processors.map do |info| [:default, { processor: info[0], dimensions: info[1] }] end.to_h end end |
#extension_whitelist ⇒ Object
Add a white list of extensions which are allowed to be uploaded. For images you might use something like this:
46 47 48 |
# File 'app/uploaders/decidim/image_uploader.rb', line 46 def extension_whitelist Decidim.organization_settings(model).upload_allowed_file_extensions_image end |
#manipulate! ⇒ Object
72 73 74 75 76 77 |
# File 'app/uploaders/decidim/image_uploader.rb', line 72 def manipulate! super rescue CarrierWave::ProcessingError => e Rails.logger.error(e) raise CarrierWave::ProcessingError, I18n.t("carrierwave.errors.general") end |
#max_image_height_or_width ⇒ Object
68 69 70 |
# File 'app/uploaders/decidim/image_uploader.rb', line 68 def max_image_height_or_width 3840 end |
#strip ⇒ Object
Strips out all embedded information from the image
18 19 20 21 22 23 |
# File 'app/uploaders/decidim/image_uploader.rb', line 18 def strip manipulate! do |img| img.strip img end end |
#validate_dimensions ⇒ Object
A simple check to avoid DoS with maliciously crafted images, or just to avoid reckless users that upload gigapixels images.
54 55 56 57 58 59 |
# File 'app/uploaders/decidim/image_uploader.rb', line 54 def validate_dimensions manipulate! do |image| validation_error!(I18n.t("carrierwave.errors.image_too_big")) if image.dimensions.any? { |dimension| dimension > max_image_height_or_width } image end end |
#validate_size ⇒ Object
61 62 63 64 65 66 |
# File 'app/uploaders/decidim/image_uploader.rb', line 61 def validate_size manipulate! do |image| validation_error!(I18n.t("carrierwave.errors.image_too_big")) if image.size > maximum_upload_size image end end |