mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 08:49:06 +08:00
FEATURE: add support for "skip_validations" option in UploadCreator (#13094)
FIX: do not validate uploads when running `uploads:fix_missing_s3` task
This commit is contained in:
parent
075cd07a07
commit
130160537c
|
@ -1024,7 +1024,7 @@ def fix_missing_s3
|
|||
Upload.transaction do
|
||||
begin
|
||||
upload.update_column(:sha1, SecureRandom.hex)
|
||||
fixed_upload = UploadCreator.new(tempfile, "temp.#{upload.extension}").create_for(Discourse.system_user.id)
|
||||
fixed_upload = UploadCreator.new(tempfile, "temp.#{upload.extension}", skip_validations: true).create_for(Discourse.system_user.id)
|
||||
rescue => fix_error
|
||||
# invalid extension is the most common issue
|
||||
end
|
||||
|
|
|
@ -24,11 +24,13 @@ class UploadCreator
|
|||
# - pasted (boolean)
|
||||
# - for_export (boolean)
|
||||
# - for_gravatar (boolean)
|
||||
# - skip_validations (boolean)
|
||||
def initialize(file, filename, opts = {})
|
||||
@file = file
|
||||
@filename = (filename || "").gsub(/[^[:print:]]/, "")
|
||||
@upload = Upload.new(original_filename: @filename, filesize: 0)
|
||||
@opts = opts
|
||||
@opts[:validate] = opts[:skip_validations].present? ? !ActiveRecord::Type::Boolean.new.cast(opts[:skip_validations]) : true
|
||||
end
|
||||
|
||||
def create_for(user_id)
|
||||
|
@ -151,7 +153,7 @@ class UploadCreator
|
|||
@upload.assign_attributes(attrs)
|
||||
end
|
||||
|
||||
return @upload unless @upload.save
|
||||
return @upload unless @upload.save(validate: @opts[:validate])
|
||||
|
||||
DiscourseEvent.trigger(:before_upload_creation, @file, is_image, @opts[:for_export])
|
||||
|
||||
|
@ -161,7 +163,7 @@ class UploadCreator
|
|||
|
||||
if url.present?
|
||||
@upload.url = url
|
||||
@upload.save!
|
||||
@upload.save!(validate: @opts[:validate])
|
||||
else
|
||||
@upload.errors.add(:url, I18n.t("upload.store_failure", upload_id: @upload.id, user_id: user_id))
|
||||
end
|
||||
|
|
|
@ -504,6 +504,32 @@ RSpec.describe UploadCreator do
|
|||
expect(FastImage.size(Discourse.store.path_for(upload))).to eq([320, 320])
|
||||
end
|
||||
end
|
||||
|
||||
describe 'skip validations' do
|
||||
let(:filename) { "small.pdf" }
|
||||
let(:file) { file_from_fixtures(filename, "pdf") }
|
||||
|
||||
before do
|
||||
SiteSetting.authorized_extensions = 'png|jpg'
|
||||
end
|
||||
|
||||
it 'creates upload when skip_validations is true' do
|
||||
upload = UploadCreator.new(file, filename,
|
||||
skip_validations: true
|
||||
).create_for(user.id)
|
||||
|
||||
expect(upload.persisted?).to eq(true)
|
||||
expect(upload.original_filename).to eq(filename)
|
||||
end
|
||||
|
||||
it 'does not create upload when skip_validations is false' do
|
||||
upload = UploadCreator.new(file, filename,
|
||||
skip_validations: false
|
||||
).create_for(user.id)
|
||||
|
||||
expect(upload.persisted?).to eq(false)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#clean_svg!' do
|
||||
|
|
Loading…
Reference in New Issue
Block a user