2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-28 05:23:01 +08:00
|
|
|
require File.expand_path("../../config/environment", __FILE__)
|
|
|
|
|
2020-05-26 21:38:23 +08:00
|
|
|
# Supported ENV arguments:
|
|
|
|
#
|
|
|
|
# VERBOSE=1
|
|
|
|
# Shows debug information.
|
|
|
|
#
|
|
|
|
# INTERACTIVE=1
|
|
|
|
# Shows debug information and pauses for input on issues.
|
|
|
|
#
|
|
|
|
# WORKER_ID/WORKER_COUNT
|
|
|
|
# When running the script on a single forum in multiple terminals.
|
|
|
|
# For example, if you want 4 concurrent scripts use WORKER_COUNT=4
|
|
|
|
# and WORKER_ID from 0 to 3
|
|
|
|
|
|
|
|
MIN_IMAGE_PIXELS = 500_000 # 0.5 megapixels
|
|
|
|
DEFAULT_IMAGE_PIXELS = 1_000_000 # 1 megapixel
|
|
|
|
|
|
|
|
MAX_IMAGE_PIXELS = [
|
|
|
|
ARGV[0]&.to_i || DEFAULT_IMAGE_PIXELS,
|
|
|
|
MIN_IMAGE_PIXELS
|
|
|
|
].max
|
|
|
|
|
|
|
|
ENV["VERBOSE"] = "1" if ENV["INTERACTIVE"]
|
|
|
|
|
|
|
|
def log(*args)
|
|
|
|
puts(*args) if ENV["VERBOSE"]
|
|
|
|
end
|
2018-02-28 05:23:01 +08:00
|
|
|
|
2020-05-26 21:38:23 +08:00
|
|
|
def process_uploads
|
|
|
|
puts "", "Downsizing images to no more than #{MAX_IMAGE_PIXELS} pixels"
|
2020-01-27 10:31:11 +08:00
|
|
|
|
2020-05-26 21:38:23 +08:00
|
|
|
dimensions_count = 0
|
|
|
|
downsized_count = 0
|
2020-01-27 10:31:11 +08:00
|
|
|
|
2020-06-11 20:47:59 +08:00
|
|
|
scope = Upload.by_users.where("LOWER(extension) IN ('jpg', 'jpeg', 'gif', 'png')")
|
2020-05-26 21:38:23 +08:00
|
|
|
scope = scope.where(<<-SQL, MAX_IMAGE_PIXELS)
|
|
|
|
COALESCE(width, 0) = 0 OR
|
|
|
|
COALESCE(height, 0) = 0 OR
|
|
|
|
COALESCE(thumbnail_width, 0) = 0 OR
|
|
|
|
COALESCE(thumbnail_height, 0) = 0 OR
|
|
|
|
width * height > ?
|
|
|
|
SQL
|
2020-01-27 10:31:11 +08:00
|
|
|
|
2020-05-26 21:38:23 +08:00
|
|
|
if ENV["WORKER_ID"] && ENV["WORKER_COUNT"]
|
|
|
|
scope = scope.where("id % ? = ?", ENV["WORKER_COUNT"], ENV["WORKER_ID"])
|
2019-10-10 22:37:55 +08:00
|
|
|
end
|
|
|
|
|
2020-05-26 21:38:23 +08:00
|
|
|
skipped = 0
|
|
|
|
total_count = scope.count
|
|
|
|
puts "Uploads to process: #{total_count}"
|
2019-10-08 23:54:39 +08:00
|
|
|
|
2020-05-26 21:38:23 +08:00
|
|
|
scope.find_each.with_index do |upload, index|
|
|
|
|
progress = (index * 100.0 / total_count).round(1)
|
2019-10-08 23:54:39 +08:00
|
|
|
|
2020-05-26 21:38:23 +08:00
|
|
|
log "\n"
|
|
|
|
print "\r#{progress}% Fixed dimensions: #{dimensions_count} Downsized: #{downsized_count} Skipped: #{skipped} (upload id: #{upload.id})"
|
|
|
|
log "\n"
|
2020-01-27 10:31:11 +08:00
|
|
|
|
2020-06-11 20:47:59 +08:00
|
|
|
path = if upload.local?
|
|
|
|
Discourse.store.path_for(upload)
|
|
|
|
else
|
|
|
|
(Discourse.store.download(upload, max_file_size_kb: 100.megabytes) rescue nil)&.path
|
|
|
|
end
|
2020-05-26 21:38:23 +08:00
|
|
|
|
2020-06-11 20:47:59 +08:00
|
|
|
unless path
|
|
|
|
log "No image path"
|
2020-05-26 21:38:23 +08:00
|
|
|
skipped += 1
|
|
|
|
next
|
|
|
|
end
|
2020-01-27 10:31:11 +08:00
|
|
|
|
2020-05-26 21:38:23 +08:00
|
|
|
begin
|
2020-06-11 20:47:59 +08:00
|
|
|
w, h = FastImage.size(path, raise_on_failure: true)
|
2020-05-26 21:38:23 +08:00
|
|
|
rescue FastImage::UnknownImageType
|
|
|
|
log "Unknown image type"
|
|
|
|
skipped += 1
|
|
|
|
next
|
|
|
|
rescue FastImage::SizeNotFound
|
|
|
|
log "Size not found"
|
|
|
|
skipped += 1
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
if !w || !h
|
|
|
|
log "Invalid image dimensions"
|
|
|
|
skipped += 1
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
ww, hh = ImageSizer.resize(w, h)
|
2020-01-27 10:31:11 +08:00
|
|
|
|
2020-05-26 21:38:23 +08:00
|
|
|
if w == 0 || h == 0 || ww == 0 || hh == 0
|
|
|
|
log "Invalid image dimensions"
|
|
|
|
skipped += 1
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
upload.attributes = {
|
2020-01-27 10:31:11 +08:00
|
|
|
width: w,
|
|
|
|
height: h,
|
|
|
|
thumbnail_width: ww,
|
2020-06-11 20:47:59 +08:00
|
|
|
thumbnail_height: hh,
|
|
|
|
filesize: File.size(path)
|
2020-05-26 21:38:23 +08:00
|
|
|
}
|
2020-01-27 10:31:11 +08:00
|
|
|
|
2020-05-26 21:38:23 +08:00
|
|
|
if upload.changed?
|
|
|
|
log "Correcting the upload dimensions"
|
2020-06-11 20:47:59 +08:00
|
|
|
log "Before: #{upload.width_was}x#{upload.height_was} #{upload.thumbnail_width_was}x#{upload.thumbnail_height_was} (#{upload.filesize_was})"
|
|
|
|
log "After: #{w}x#{h} #{ww}x#{hh} (#{upload.filesize})"
|
2020-01-27 10:31:11 +08:00
|
|
|
|
2020-05-26 21:38:23 +08:00
|
|
|
dimensions_count += 1
|
|
|
|
upload.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
if w * h < MAX_IMAGE_PIXELS
|
|
|
|
log "Image size within allowed range"
|
|
|
|
skipped += 1
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2020-06-11 20:47:59 +08:00
|
|
|
result = ShrinkUploadedImage.new(
|
|
|
|
upload: upload,
|
|
|
|
path: path,
|
|
|
|
max_pixels: MAX_IMAGE_PIXELS,
|
|
|
|
verbose: ENV["VERBOSE"],
|
|
|
|
interactive: ENV["INTERACTIVE"]
|
|
|
|
).perform
|
2020-05-26 21:38:23 +08:00
|
|
|
|
2020-06-11 20:47:59 +08:00
|
|
|
if result
|
2020-05-26 21:38:23 +08:00
|
|
|
downsized_count += 1
|
|
|
|
else
|
|
|
|
skipped += 1
|
|
|
|
end
|
2020-01-27 10:31:11 +08:00
|
|
|
end
|
|
|
|
|
2020-05-26 21:38:23 +08:00
|
|
|
STDIN.beep
|
|
|
|
puts "", "Done", Time.zone.now
|
2018-02-28 05:23:01 +08:00
|
|
|
end
|
|
|
|
|
2020-05-26 21:38:23 +08:00
|
|
|
process_uploads
|