mirror of
https://github.com/discourse/discourse.git
synced 2025-03-26 17:25:55 +08:00
FIX: downsize_uploads script to support external storage
Also ensured we update the sha1 property of the upload record to match the actual file.
This commit is contained in:
parent
c3a5a8e095
commit
4fdad12998
@ -9,28 +9,30 @@ puts '', "Downsizing uploads size to no more than #{max_image_pixels} pixels"
|
||||
|
||||
count = 0
|
||||
|
||||
Upload.where("lower(extension) in (?)", ['jpg', 'jpeg', 'gif', 'png']).find_each do |upload|
|
||||
Upload.where("LOWER(extension) IN ('jpg', 'jpeg', 'gif', 'png')").find_each do |upload|
|
||||
count += 1
|
||||
print "\r%8d".freeze % count
|
||||
absolute_path = Discourse.store.path_for(upload)
|
||||
if absolute_path && FileHelper.is_supported_image?(upload.original_filename)
|
||||
file = File.new(absolute_path) rescue nil
|
||||
next unless file
|
||||
|
||||
image_info = FastImage.new(file) rescue nil
|
||||
pixels = image_info.size&.reduce(:*).to_i
|
||||
next unless source = upload.local? ? Discourse.store.path_for(upload) : "https:#{upload.url}"
|
||||
next unless size = (FastImage.size(source) rescue nil)
|
||||
next if size.reduce(:*) < max_image_pixels
|
||||
next unless path = upload.local? ? source : (Discourse.store.download(upload) rescue nil)&.path
|
||||
|
||||
if pixels > max_image_pixels
|
||||
OptimizedImage.downsize(file.path, file.path, "#{max_image_pixels}@", filename: upload.original_filename)
|
||||
OptimizedImage.downsize(path, path, "#{max_image_pixels}@", filename: upload.original_filename)
|
||||
|
||||
upload.filesize = File.size(file)
|
||||
upload.width, upload.height = ImageSizer.resize(*FastImage.new(file).size)
|
||||
upload.save!
|
||||
previous_short_url = upload.short_url
|
||||
|
||||
upload.posts.each do |post|
|
||||
Jobs.enqueue(:process_post, post_id: post.id, bypass_bump: true, cook: true)
|
||||
end
|
||||
end
|
||||
upload.filesize = File.size(path)
|
||||
upload.sha1 = Upload.generate_digest(path)
|
||||
upload.width, upload.height = ImageSizer.resize(*FastImage.size(path))
|
||||
next unless upload.save!
|
||||
|
||||
next unless url = Discourse.store.store_upload(File.new(path), upload)
|
||||
next unless upload.update!(url: url)
|
||||
|
||||
upload.posts.each do |post|
|
||||
post.update!(raw: post.raw.gsub(previous_short_url, upload.short_url))
|
||||
Jobs.enqueue(:process_post, post_id: post.id, bypass_bump: true, cook: true)
|
||||
end
|
||||
end
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user