mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 14:03:39 +08:00
Prioritize user avatars when regenerating missing optimized images.
This commit is contained in:
parent
7e80810de1
commit
2448db4716
|
@ -390,44 +390,54 @@ def regenerate_missing_optimized
|
|||
public_directory = "#{Rails.root}/public"
|
||||
missing_uploads = Set.new
|
||||
|
||||
OptimizedImage.includes(:upload)
|
||||
.where("LENGTH(COALESCE(url, '')) > 0")
|
||||
.where("width > 0 AND height > 0")
|
||||
.find_each do |optimized_image|
|
||||
avatar_upload_ids = UserAvatar.all.pluck(:custom_upload_id, :gravatar_upload_id).flatten.compact
|
||||
|
||||
upload = optimized_image.upload
|
||||
default_scope = OptimizedImage.includes(:upload)
|
||||
|
||||
next unless optimized_image.url =~ /^\/[^\/]/
|
||||
next unless upload.url =~ /^\/[^\/]/
|
||||
[
|
||||
default_scope
|
||||
.where("optimized_images.upload_id IN (?)", avatar_upload_ids),
|
||||
|
||||
thumbnail = "#{public_directory}#{optimized_image.url}"
|
||||
original = "#{public_directory}#{upload.url}"
|
||||
default_scope
|
||||
.where("optimized_images.upload_id NOT IN (?)", avatar_upload_ids)
|
||||
.where("LENGTH(COALESCE(url, '')) > 0")
|
||||
.where("width > 0 AND height > 0")
|
||||
].each do |scope|
|
||||
scope.find_each do |optimized_image|
|
||||
upload = optimized_image.upload
|
||||
|
||||
if !File.exists?(thumbnail) || File.size(thumbnail) <= 0
|
||||
# make sure the original image exists locally
|
||||
if (!File.exists?(original) || File.size(original) <= 0) && upload.origin.present?
|
||||
# try to fix it by redownloading it
|
||||
begin
|
||||
downloaded = FileHelper.download(upload.origin, SiteSetting.max_image_size_kb.kilobytes, "discourse-missing", true) rescue nil
|
||||
if downloaded && downloaded.size > 0
|
||||
FileUtils.mkdir_p(File.dirname(original))
|
||||
File.open(original, "wb") { |f| f.write(downloaded.read) }
|
||||
next unless optimized_image.url =~ /^\/[^\/]/
|
||||
next unless upload.url =~ /^\/[^\/]/
|
||||
|
||||
thumbnail = "#{public_directory}#{optimized_image.url}"
|
||||
original = "#{public_directory}#{upload.url}"
|
||||
|
||||
if !File.exists?(thumbnail) || File.size(thumbnail) <= 0
|
||||
# make sure the original image exists locally
|
||||
if (!File.exists?(original) || File.size(original) <= 0) && upload.origin.present?
|
||||
# try to fix it by redownloading it
|
||||
begin
|
||||
downloaded = FileHelper.download(upload.origin, SiteSetting.max_image_size_kb.kilobytes, "discourse-missing", true) rescue nil
|
||||
if downloaded && downloaded.size > 0
|
||||
FileUtils.mkdir_p(File.dirname(original))
|
||||
File.open(original, "wb") { |f| f.write(downloaded.read) }
|
||||
end
|
||||
ensure
|
||||
downloaded.try(:close!) if downloaded.respond_to?(:close!)
|
||||
end
|
||||
ensure
|
||||
downloaded.try(:close!) if downloaded.respond_to?(:close!)
|
||||
end
|
||||
end
|
||||
|
||||
if File.exists?(original) && File.size(original) > 0
|
||||
FileUtils.mkdir_p(File.dirname(thumbnail))
|
||||
OptimizedImage.resize(original, thumbnail, optimized_image.width, optimized_image.height)
|
||||
putc "#"
|
||||
if File.exists?(original) && File.size(original) > 0
|
||||
FileUtils.mkdir_p(File.dirname(thumbnail))
|
||||
OptimizedImage.resize(original, thumbnail, optimized_image.width, optimized_image.height)
|
||||
putc "#"
|
||||
else
|
||||
missing_uploads << original
|
||||
putc "X"
|
||||
end
|
||||
else
|
||||
missing_uploads << original
|
||||
putc "X"
|
||||
putc "."
|
||||
end
|
||||
else
|
||||
putc "."
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user