2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-27 16:23:01 -05:00
|
|
|
require File.expand_path("../../config/environment", __FILE__)
|
|
|
|
|
|
|
|
# no less than 1 megapixel
|
|
|
|
max_image_pixels = [ARGV[0].to_i, 1_000_000].max
|
|
|
|
|
|
|
|
puts '', "Downsizing uploads size to no more than #{max_image_pixels} pixels"
|
|
|
|
|
2018-04-20 16:59:44 -04:00
|
|
|
count = 0
|
|
|
|
|
2019-02-11 00:28:43 -05:00
|
|
|
Upload.where("lower(extension) in (?)", ['jpg', 'jpeg', 'gif', 'png']).find_each do |upload|
|
2018-04-20 16:59:44 -04:00
|
|
|
count += 1
|
|
|
|
print "\r%8d".freeze % count
|
2018-02-27 16:23:01 -05:00
|
|
|
absolute_path = Discourse.store.path_for(upload)
|
2018-09-09 22:22:45 -04:00
|
|
|
if absolute_path && FileHelper.is_supported_image?(upload.original_filename)
|
2018-02-27 16:23:01 -05:00
|
|
|
file = File.new(absolute_path) rescue nil
|
|
|
|
next unless file
|
|
|
|
|
|
|
|
image_info = FastImage.new(file) rescue nil
|
|
|
|
pixels = image_info.size&.reduce(:*).to_i
|
|
|
|
|
|
|
|
if pixels > max_image_pixels
|
|
|
|
OptimizedImage.downsize(file.path, file.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!
|
|
|
|
|
|
|
|
upload.posts.each do |post|
|
|
|
|
Jobs.enqueue(:process_post, post_id: post.id, bypass_bump: true, cook: true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
puts '', 'Done', ''
|