From dc8a7e74f4c35db51e86cda8fb416c2f35371178 Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Fri, 11 Nov 2022 17:56:11 +0100 Subject: [PATCH] FIX: Allow attr updates of over-size-limit uploads (#18986) --- app/models/upload.rb | 6 ++++++ lib/tasks/uploads.rake | 4 ++++ lib/validators/upload_validator.rb | 6 ++---- spec/tasks/uploads_spec.rb | 7 +++++++ 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/app/models/upload.rb b/app/models/upload.rb index d11e79d0733..ad004f6e668 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -34,6 +34,7 @@ class Upload < ActiveRecord::Base attr_accessor :for_export attr_accessor :for_site_setting attr_accessor :for_gravatar + attr_accessor :validate_file_size validates_presence_of :filesize validates_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_s self.url end diff --git a/lib/tasks/uploads.rake b/lib/tasks/uploads.rake index cc3dc72c228..9fea1372bbb 100644 --- a/lib/tasks/uploads.rake +++ b/lib/tasks/uploads.rake @@ -1189,6 +1189,10 @@ task "uploads:downsize" => :environment do log "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 = false upload.save! end diff --git a/lib/validators/upload_validator.rb b/lib/validators/upload_validator.rb index 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 PM if (upload.for_private_message && SiteSetting.allow_staff_to_upload_any_file_in_pm) @@ -141,6 +138,8 @@ class UploadValidator < ActiveModel::Validator end def maximum_file_size(upload, type) + return if !upload.validate_file_size + max_size_kb = if upload.for_export SiteSetting.max_export_file_size_kb else @@ -157,5 +156,4 @@ class UploadValidator < ActiveModel::Validator upload.errors.add(:filesize, message) end end - end diff --git a/spec/tasks/uploads_spec.rb b/spec/tasks/uploads_spec.rb index 620f26b9179..600e4800f0f 100644 --- a/spec/tasks/uploads_spec.rb +++ b/spec/tasks/uploads_spec.rb @@ -235,5 +235,12 @@ RSpec.describe "tasks/uploads" do expect { 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) + end end end