FIX: Allow attr updates of over-size-limit uploads (#18986)

This commit is contained in:
Jarek Radosz 2022-11-11 17:56:11 +01:00 committed by GitHub
parent 99e5fbe303
commit dc8a7e74f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 4 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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