Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix site upload validations #22479

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion app/models/form/admin_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}")
Expand All @@ -116,4 +122,16 @@ def typecast_value(key, value)
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