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
|
|
|
|
|
2019-10-10 10:37:55 -04:00
|
|
|
puts "", "Fixing all images dimensions in the database", ""
|
2018-02-27 16:23:01 -05:00
|
|
|
|
2018-04-20 16:59:44 -04:00
|
|
|
count = 0
|
|
|
|
|
2019-10-10 10:37:55 -04:00
|
|
|
Upload
|
|
|
|
.where("LOWER(extension) IN ('jpg', 'jpeg', 'gif', 'png')")
|
|
|
|
.where("COALESCE(width, 0) = 0 OR COALESCE(height, 0) = 0 OR COALESCE(thumbnail_width, 0) = 0 OR COALESCE(thumbnail_height, 0) = 0")
|
|
|
|
.find_each do |upload|
|
|
|
|
|
|
|
|
count += 1
|
|
|
|
print "\r%8d".freeze % count
|
|
|
|
|
|
|
|
next unless source = upload.local? ? Discourse.store.path_for(upload) : "https:#{upload.url}"
|
|
|
|
|
|
|
|
w, h = FastImage.size(source)
|
|
|
|
ww, hh = ImageSizer.resize(w, h)
|
|
|
|
|
|
|
|
next if w == 0 || h == 0 || ww == 0 || hh == 0
|
|
|
|
|
|
|
|
upload.update!(
|
|
|
|
width: w,
|
|
|
|
height: h,
|
|
|
|
thumbnail_width: ww,
|
|
|
|
thumbnail_height: hh,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "", "Downsizing images to no more than #{max_image_pixels} pixels"
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
Upload
|
|
|
|
.where("LOWER(extension) IN ('jpg', 'jpeg', 'gif', 'png')")
|
|
|
|
.where("width * height > ?", max_image_pixels)
|
|
|
|
.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
|
|
|
|
2019-10-08 11:54:39 -04:00
|
|
|
next unless source = upload.local? ? Discourse.store.path_for(upload) : "https:#{upload.url}"
|
|
|
|
next unless size = (FastImage.size(source) rescue nil)
|
2019-10-10 10:37:55 -04:00
|
|
|
|
|
|
|
if size.reduce(:*) < max_image_pixels
|
|
|
|
ww, hh = ImageSizer.resize(*size)
|
|
|
|
|
|
|
|
upload.update!(
|
|
|
|
width: size[0],
|
|
|
|
height: size[1],
|
|
|
|
thumbnail_width: ww,
|
|
|
|
thumbnail_height: hh,
|
|
|
|
)
|
|
|
|
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2019-10-08 11:54:39 -04:00
|
|
|
next unless path = upload.local? ? source : (Discourse.store.download(upload) rescue nil)&.path
|
2018-02-27 16:23:01 -05:00
|
|
|
|
2019-10-08 11:54:39 -04:00
|
|
|
OptimizedImage.downsize(path, path, "#{max_image_pixels}@", filename: upload.original_filename)
|
2018-02-27 16:23:01 -05:00
|
|
|
|
2019-10-08 11:54:39 -04:00
|
|
|
previous_short_url = upload.short_url
|
2018-02-27 16:23:01 -05:00
|
|
|
|
2019-10-10 10:37:55 -04:00
|
|
|
sha1 = Upload.generate_digest(path)
|
|
|
|
w, h = FastImage.size(path)
|
|
|
|
ww, hh = ImageSizer.resize(w, h)
|
|
|
|
|
|
|
|
new_file = true
|
|
|
|
|
|
|
|
if existing_upload = Upload.find_by(sha1: sha1)
|
|
|
|
upload = existing_upload
|
|
|
|
new_file = false
|
|
|
|
end
|
|
|
|
|
2019-10-08 11:54:39 -04:00
|
|
|
upload.filesize = File.size(path)
|
2019-10-10 10:37:55 -04:00
|
|
|
upload.sha1 = sha1
|
|
|
|
upload.width = w
|
|
|
|
upload.height = h
|
|
|
|
upload.thumbnail_width = ww
|
|
|
|
upload.thumbnail_height = hh
|
2019-10-08 11:54:39 -04:00
|
|
|
next unless upload.save!
|
|
|
|
|
2019-10-10 10:37:55 -04:00
|
|
|
if new_file
|
|
|
|
next unless url = Discourse.store.store_upload(File.new(path), upload)
|
|
|
|
next unless upload.update!(url: url)
|
|
|
|
end
|
2019-10-08 11:54:39 -04:00
|
|
|
|
|
|
|
upload.posts.each do |post|
|
2019-10-10 10:37:55 -04:00
|
|
|
post.update!(raw: post.raw.gsub(previous_short_url, upload.short_url)) if new_file
|
2019-10-08 11:54:39 -04:00
|
|
|
Jobs.enqueue(:process_post, post_id: post.id, bypass_bump: true, cook: true)
|
2018-02-27 16:23:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-10 10:37:55 -04:00
|
|
|
puts "", "Done"
|