about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
authorClaire <claire.github-309c@sitedethib.com>2023-01-05 13:42:03 +0100
committerGitHub <noreply@github.com>2023-01-05 13:42:03 +0100
commitacec1fb745456dd3093ac31291c3757e55f39160 (patch)
tree0ef5d18c26e0bfb867b9d57b0c3f3663c4f63d24 /app
parent42f9693d0032b5e165568f89d4bf611016a88955 (diff)
Fix site upload validations (#22479)
* Fix site settings media upload handling of DimensionsValidationError

Fixes #22234

* Fix underlying validations not being performed for site uploads
Diffstat (limited to 'app')
-rw-r--r--app/models/form/admin_settings.rb20
1 files changed, 19 insertions, 1 deletions
diff --git a/app/models/form/admin_settings.rb b/app/models/form/admin_settings.rb
index 431d33bcd..dc6cc5ed3 100644
--- a/app/models/form/admin_settings.rb
+++ b/app/models/form/admin_settings.rb
@@ -66,6 +66,7 @@ class Form::AdminSettings
   validates :show_domain_blocks_rationale, inclusion: { in: %w(disabled users all) }, if: -> { defined?(@show_domain_blocks_rationale) }
   validates :media_cache_retention_period, :content_cache_retention_period, :backups_retention_period, numericality: { only_integer: true }, allow_blank: true, if: -> { defined?(@media_cache_retention_period) || defined?(@content_cache_retention_period) || defined?(@backups_retention_period) }
   validates :site_short_description, length: { maximum: 200 }, if: -> { defined?(@site_short_description) }
+  validate :validate_site_uploads
 
   KEYS.each do |key|
     define_method(key) do
@@ -87,11 +88,16 @@ class Form::AdminSettings
     define_method("#{key}=") do |file|
       value = public_send(key)
       value.file = file
+    rescue Mastodon::DimensionsValidationError => e
+      errors.add(key.to_sym, e.message)
     end
   end
 
   def save
-    return false unless valid?
+    # NOTE: Annoyingly, files are processed and can error out before
+    # validations are called, and `valid?` clears errors…
+    # So for now, return early if errors aren't empty.
+    return false unless errors.empty? && valid?
 
     KEYS.each do |key|
       next unless instance_variable_defined?("@#{key}")
@@ -116,4 +122,16 @@ class Form::AdminSettings
       value
     end
   end
+
+  def validate_site_uploads
+    UPLOAD_KEYS.each do |key|
+      next unless instance_variable_defined?("@#{key}")
+      upload = instance_variable_get("@#{key}")
+      next if upload.valid?
+
+      upload.errors.each do |error|
+        errors.import(error, attribute: key)
+      end
+    end
+  end
 end