FIX: Regenerate optimized images instead of migrating from old scheme.

FIX: Regenerate optimized images instead of migrating from old scheme.

OptimizedImage.migrate_to_new_scheme was optimizing optimized images which we don’t need to do. Regnerating the optimized image is way easier.

diff --git a/app/jobs/scheduled/migrate_upload_scheme.rb b/app/jobs/scheduled/migrate_upload_scheme.rb
index 21c33a1..d72108a 100644
--- a/app/jobs/scheduled/migrate_upload_scheme.rb
+++ b/app/jobs/scheduled/migrate_upload_scheme.rb
@@ -14,6 +14,7 @@ module Jobs
 
       # migrate uploads to new scheme
       problems = Upload.migrate_to_new_scheme(50)
+
       problems.each do |hash|
         upload_id = hash[:upload].id
         Discourse.handle_job_exception(hash[:ex], error_context(args, "Migrating upload id #{upload_id}", upload_id: upload_id))
@@ -24,12 +25,16 @@ module Jobs
       # Clean up orphan optimized images
       OptimizedImage.where("upload_id NOT IN (SELECT id FROM uploads)").destroy_all
 
-      # migrate optimized_images to new scheme
-      problems = OptimizedImage.migrate_to_new_scheme(50)
+      # Clean up optimized images that needs to be regenerated
+      OptimizedImage.joins(:upload)
+        .where("optimized_images.url NOT LIKE '%/optimized/_X/%'")
+        .where("uploads.url LIKE '%/original/_X/%'")
+        .limit(50)
+        .find_each do |optimized_image|
 
-      problems.each do |hash|
-        image = OptimizedImage.find_by(id:  hash[:optimized_image].id)
-        image.destroy! if image
+        upload = optimized_image.upload
+        optimized_image.destroy!
+        upload.rebake_posts_on_old_scheme
       end
     end
 
diff --git a/app/models/optimized_image.rb b/app/models/optimized_image.rb
index 726f0e3..3860d34 100644
--- a/app/models/optimized_image.rb
+++ b/app/models/optimized_image.rb
@@ -350,72 +350,6 @@ class OptimizedImage < ActiveRecord::Base
       false
     end
   end
-
-  def self.migrate_to_new_scheme(limit = nil)
-    problems = []
-
-    if SiteSetting.migrate_to_new_scheme
-      max_file_size_kb = SiteSetting.max_image_size_kb.kilobytes
-      local_store = FileStore::LocalStore.new
-
-      scope = OptimizedImage.includes(:upload)
-        .where("url NOT LIKE '%/optimized/_X/%'")
-        .order(id: :desc)
-
-      scope.limit(limit) if limit
-
-      scope.each do |optimized_image|
-        begin
-          # keep track of the url
-          previous_url = optimized_image.url.dup
-          # where is the file currently stored?
-          external = previous_url =~ /^\/\//
-          # download if external
-          if external
-            url = SiteSetting.scheme + ":" + previous_url
-            file = FileHelper.download(
-              url,
-              max_file_size: max_file_size_kb,
-              tmp_file_name: "discourse",
-              follow_redirect: true
-            ) rescue nil
-            path = file.path
-          else
-            path = local_store.path_for(optimized_image)
-            file = File.open(path)
-          end
-          # compute SHA if missing
-          if optimized_image.sha1.blank?
-            optimized_image.sha1 = Upload.generate_digest(path)
-          end
-          # optimize if image
-          FileHelper.optimize_image!(path)
-          # store to new location & update the filesize
-          File.open(path) do |f|
-            optimized_image.url = Discourse.store.store_optimized_image(f, optimized_image)
-            optimized_image.save
-          end
-          # remap the URLs
-          DbHelper.remap(UrlHelper.absolute(previous_url), optimized_image.url) unless external
-          DbHelper.remap(previous_url, optimized_image.url)
-          # remove the old file (when local)
-          unless external
-            FileUtils.rm(path, force: true)
-          end
-        rescue => e
-          problems << { optimized_image: optimized_image, ex: e }
-          # just ditch the optimized image if there was any errors
-          optimized_image.destroy
-        ensure
-          file&.close
-          file&.unlink if file&.respond_to?(:unlink)
-        end
-      end
-    end
-
-    problems
-  end
-
 end
 
 # == Schema Information
diff --git a/app/models/upload.rb b/app/models/upload.rb
index 9de4e28..0e5fef5 100644
--- a/app/models/upload.rb
+++ b/app/models/upload.rb
@@ -272,6 +272,9 @@ class Upload < ActiveRecord::Base
           # remap the URLs
           DbHelper.remap(UrlHelper.absolute(previous_url), upload.url) unless external
           DbHelper.remap(previous_url, upload.url)
+
+          upload.optimized_images.find_each(&:destroy!)
+          upload.rebake_posts_on_old_scheme
           # remove the old file (when local)
           unless external
             FileUtils.rm(path, force: true)
@@ -288,6 +291,12 @@ class Upload < ActiveRecord::Base
     problems
   end
 
+  private
+
+  def rebake_posts_on_old_scheme
+    self.posts.where("cooked LIKE '%/_optimized/%'").find_each(&:rebake!)
+  end
+
 end
 
 # == Schema Information

GitHub sha: feb731bf