From 17047806b92260f571d0f76d62f84d2017bf3e4f Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 8 Aug 2018 13:14:52 +0800 Subject: [PATCH] Add a rake task to fix uploads with wrong extension. --- lib/tasks/uploads.rake | 4 +++ lib/upload_fixer.rb | 81 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 lib/upload_fixer.rb diff --git a/lib/tasks/uploads.rake b/lib/tasks/uploads.rake index 40839a4821c..484be1e947e 100644 --- a/lib/tasks/uploads.rake +++ b/lib/tasks/uploads.rake @@ -710,3 +710,7 @@ task "uploads:analyze", [:cache_path, :limit] => :environment do |_, args| puts "List of file paths @ #{path}" puts "Duration: #{Time.zone.now - now} seconds" end + +task "uploads:fix_incorrect_extensions" => :environment do + UploadFixer.fix_extensions +end diff --git a/lib/upload_fixer.rb b/lib/upload_fixer.rb new file mode 100644 index 00000000000..79764bfb525 --- /dev/null +++ b/lib/upload_fixer.rb @@ -0,0 +1,81 @@ +class UploadFixer + def self.fix_extensions + Upload.where("uploads.extension IS NOT NULL").find_each do |upload| + is_external = Discourse.store.external? + previous_url = upload.url.dup + + source = + if is_external + "https:#{previous_url}" + else + Discourse.store.path_for(upload) + end + + correct_extension = FastImage.type(source).to_s.downcase + current_extension = upload.extension.to_s.downcase + + if correct_extension.present? + correct_extension = 'jpg' if correct_extension == 'jpeg' + current_extension = 'jpg' if current_extension == 'jpeg' + + if correct_extension != current_extension + new_filename = change_extension( + upload.original_filename, + correct_extension + ) + + new_url = change_extension(previous_url, correct_extension) + + if is_external + new_url = "/#{new_url}" + source = Discourse.store.get_path_for_upload(upload) + destination = change_extension(source, correct_extension) + + Discourse.store.copy_file( + previous_url, + source, + destination + ) + + upload.update!( + original_filename: new_filename, + url: new_url, + extension: correct_extension + ) + + DbHelper.remap(previous_url, upload.url) + Discourse.store.remove_file(previous_url, source) + else + destination = change_extension(source, correct_extension) + FileUtils.copy(source, destination) + + upload.update!( + original_filename: new_filename, + url: new_url, + extension: correct_extension + ) + + DbHelper.remap(previous_url, upload.url) + + tombstone_path = source.sub("/uploads/", "/uploads/tombstone/") + FileUtils.mkdir_p(File.dirname(tombstone_path)) + + FileUtils.move( + source, + tombstone_path + ) + end + end + end + end + end + + private + + def self.change_extension(path, extension) + pathname = Pathname.new(path) + dirname = pathname.dirname.to_s != "." ? "#{pathname.dirname}/" : "" + basename = File.basename(path, File.extname(path)) + "#{dirname}#{basename}.#{extension}" + end +end