FIX: Trigger before upload event after saving and before uploading it, so we are sure that the upload is valid. (#10269)

FIX: Trigger before upload event after saving and before uploading it, so we are sure that the upload is valid. (#10269)

diff --git a/lib/upload_creator.rb b/lib/upload_creator.rb
index 7716af2..ea05115 100644
--- a/lib/upload_creator.rb
+++ b/lib/upload_creator.rb
@@ -39,8 +39,6 @@ class UploadCreator
     is_image ||= @image_info && FileHelper.is_supported_image?("test.#{@image_info.type}")
     is_image = false if @opts[:for_theme]
 
-    DiscourseEvent.trigger(:before_upload_creation, @file, is_image)
-
     DistributedMutex.synchronize("upload_#{user_id}_#{@filename}") do
       if is_image
         extract_image_info!
@@ -124,6 +122,8 @@ class UploadCreator
       add_metadata!
       return @upload unless @upload.save
 
+      DiscourseEvent.trigger(:before_upload_creation, @file, is_image)
+
       # store the file and update its url
       File.open(@file.path) do |f|
         url = Discourse.store.store_upload(f, @upload)

GitHub sha: 2abfd30d

This commit appears in #10269 which was approved by eviltrout. It was merged by romanrizzi.