Merge pull request #4420 from tgxworld/prioritize_user_avataras

Prioritize user avatars when regenerating missing optimized images.
This commit is contained in:
Guo Xiang Tan 2016-08-26 07:58:58 +08:00 committed by GitHub
commit b1c8ede617

View File

@ -390,44 +390,54 @@ def regenerate_missing_optimized
public_directory = "#{Rails.root}/public" public_directory = "#{Rails.root}/public"
missing_uploads = Set.new missing_uploads = Set.new
OptimizedImage.includes(:upload) avatar_upload_ids = UserAvatar.all.pluck(:custom_upload_id, :gravatar_upload_id).flatten.compact
.where("LENGTH(COALESCE(url, '')) > 0")
.where("width > 0 AND height > 0")
.find_each do |optimized_image|
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}" default_scope
original = "#{public_directory}#{upload.url}" .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 next unless optimized_image.url =~ /^\/[^\/]/
# make sure the original image exists locally next unless upload.url =~ /^\/[^\/]/
if (!File.exists?(original) || File.size(original) <= 0) && upload.origin.present?
# try to fix it by redownloading it thumbnail = "#{public_directory}#{optimized_image.url}"
begin original = "#{public_directory}#{upload.url}"
downloaded = FileHelper.download(upload.origin, SiteSetting.max_image_size_kb.kilobytes, "discourse-missing", true) rescue nil
if downloaded && downloaded.size > 0 if !File.exists?(thumbnail) || File.size(thumbnail) <= 0
FileUtils.mkdir_p(File.dirname(original)) # make sure the original image exists locally
File.open(original, "wb") { |f| f.write(downloaded.read) } 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 end
ensure
downloaded.try(:close!) if downloaded.respond_to?(:close!)
end end
end
if File.exists?(original) && File.size(original) > 0 if File.exists?(original) && File.size(original) > 0
FileUtils.mkdir_p(File.dirname(thumbnail)) FileUtils.mkdir_p(File.dirname(thumbnail))
OptimizedImage.resize(original, thumbnail, optimized_image.width, optimized_image.height) OptimizedImage.resize(original, thumbnail, optimized_image.width, optimized_image.height)
putc "#" putc "#"
else
missing_uploads << original
putc "X"
end
else else
missing_uploads << original putc "."
putc "X"
end end
else
putc "."
end end
end end