mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 03:16:41 +08:00
Add a rake task to fix uploads with wrong extension.
This commit is contained in:
parent
6c41b54b2e
commit
17047806b9
|
@ -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
|
||||
|
|
81
lib/upload_fixer.rb
Normal file
81
lib/upload_fixer.rb
Normal file
|
@ -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
|
Loading…
Reference in New Issue
Block a user