FIX: Regularly reset unknown extension of uploads

FIX: Regularly reset unknown extension of uploads

Discourse will try to detect the extension the next time the upload is needed. Maybe there was just a transient error the last time.

diff --git a/app/jobs/scheduled/weekly.rb b/app/jobs/scheduled/weekly.rb
index 52905e2..705cf60 100644
--- a/app/jobs/scheduled/weekly.rb
+++ b/app/jobs/scheduled/weekly.rb
@@ -14,6 +14,7 @@ module Jobs
       MiniScheduler::Stat.purge_old
       Draft.cleanup!
       UserAuthToken.cleanup!
+      Upload.reset_unknown_extensions!
     end
   end
 end
diff --git a/app/models/upload.rb b/app/models/upload.rb
index fcb67ca..b013ece 100644
--- a/app/models/upload.rb
+++ b/app/models/upload.rb
@@ -359,6 +359,10 @@ class Upload < ActiveRecord::Base
     problems
   end
 
+  def self.reset_unknown_extensions!
+    Upload.where(extension: "unknown").update_all(extension: nil)
+  end
+
   private
 
   def short_url_basename
diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb
index 89bbe33..ffb312d 100644
--- a/spec/models/upload_spec.rb
+++ b/spec/models/upload_spec.rb
@@ -288,4 +288,15 @@ describe Upload do
     end
   end
 
+  describe '.reset_unknown_extensions!' do
+    it 'should reset the extension of uploads when it is "unknown"' do
+      upload1 = Fabricate(:upload, extension: "unknown")
+      upload2 = Fabricate(:upload, extension: "png")
+
+      Upload.reset_unknown_extensions!
+
+      expect(upload1.reload.extension).to eq(nil)
+      expect(upload2.reload.extension).to eq("png")
+    end
+  end
 end

GitHub sha: cc7b24b8

1 Like

I wonder if we should “bail out” after X unsuccessful attempts at identifying the extension?

And by bail out, I mean destroy the upload and warn the user something is off?