A platform for community discussion. Free, open, simple.
FIX: Allow attr updates of over-size-limit uploads (#18986)
Jarek Radoszdc8a7e74f4c3
diff --git a/app/models/upload.rb b/app/models/upload.rbindex d11e79d0733..ad004f6e668 100644--- a/app/models/upload.rb+++ b/app/models/upload.rb@@ -34,6 +34,7 @@ class Upload < ActiveRecord::Baseattr_accessor :for_exportattr_accessor :for_site_settingattr_accessor :for_gravatar+ attr_accessor :validate_file_sizevalidates_presence_of :filesizevalidates_presence_of :original_filename@@ -92,6 +93,11 @@ class Upload < ActiveRecord::Base.where("ur.upload_id IS NULL")end+ def initialize(*args)+ super+ self.validate_file_size = true+ end+def to_sself.urlenddiff --git a/lib/tasks/uploads.rake b/lib/tasks/uploads.rakeindex cc3dc72c228..9fea1372bbb 100644--- a/lib/tasks/uploads.rake+++ b/lib/tasks/uploads.rake@@ -1189,6 +1189,10 @@ task "uploads:downsize" => :environment dolog "After: #{w}x#{h} #{ww}x#{hh} (#{upload.filesize})"dimensions_count += 1++ # Don't validate the size - max image size setting might have+ # changed since the file was uploaded, so this could fail+ upload.validate_file_size = falseupload.save!enddiff --git a/lib/validators/upload_validator.rb b/lib/validators/upload_validator.rbindex 7dd4f757812..5fb084c8354 100644--- a/lib/validators/upload_validator.rb+++ b/lib/validators/upload_validator.rb@@ -2,10 +2,7 @@require "file_helper"-module Validators; end-class UploadValidator < ActiveModel::Validator-def validate(upload)# staff can upload any file in PMif (upload.for_private_message && SiteSetting.allow_staff_to_upload_any_file_in_pm)@@ -141,6 +138,8 @@ class UploadValidator < ActiveModel::Validatorenddef maximum_file_size(upload, type)+ return if !upload.validate_file_size+max_size_kb = if upload.for_exportSiteSetting.max_export_file_size_kbelse@@ -157,5 +156,4 @@ class UploadValidator < ActiveModel::Validatorupload.errors.add(:filesize, message)endend-enddiff --git a/spec/tasks/uploads_spec.rb b/spec/tasks/uploads_spec.rbindex 620f26b9179..600e4800f0f 100644--- a/spec/tasks/uploads_spec.rb+++ b/spec/tasks/uploads_spec.rb@@ -235,5 +235,12 @@ RSpec.describe "tasks/uploads" doexpect { invoke_task }.to change { upload.reload.thumbnail_height }.to(200)end++ it "updates attributes of uploads that are over the size limit" do+ upload.update!(thumbnail_height: 0)+ SiteSetting.max_image_size_kb = 0.001 # 1 byte++ expect { invoke_task }.to change { upload.reload.thumbnail_height }.to(200)+ endendend