2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-09-07 12:02:53 +08:00

FIX: Validate upload is still valid after calling the "before_upload_creation" event (#13091)

Since we use the event to perform additional validations on the file, we should check if it added any errors to the upload before saving it. This change makes the UploadCreator more consistent since we no longer have to rely on exceptions.
This commit is contained in:
Roman Rizzi 2021-06-15 10:10:03 -03:00 committed by GitHub
parent 00255d0bd2
commit fa57316a4e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 3 deletions

View file

@ -153,9 +153,10 @@ class UploadCreator
@upload.assign_attributes(attrs)
end
return @upload unless @upload.save(validate: @opts[:validate])
DiscourseEvent.trigger(:before_upload_creation, @file, is_image, @opts[:for_export])
# Callbacks using this event to validate the upload or the file must add errors to the
# upload errors object.
DiscourseEvent.trigger(:before_upload_creation, @file, is_image, @upload, @opts[:validate])
return @upload unless @upload.errors.empty? && @upload.save(validate: @opts[:validate])
# store the file and update its url
File.open(@file.path) do |f|