FIX: empty the missing list on each post loop

This commit is contained in:
Vinoth Kannan 2019-04-10 11:20:26 +05:30
parent df85d4593e
commit bfdd0fe64c
1 changed files with 10 additions and 4 deletions

View File

@ -392,10 +392,12 @@ desc 'Finds missing post upload records from cooked HTML content'
task 'posts:missing_uploads' => :environment do task 'posts:missing_uploads' => :environment do
name = "missing_uploads" name = "missing_uploads"
PostCustomField.where(name: name).destroy_all PostCustomField.where(name: name).destroy_all
posts = Post.where("posts.cooked LIKE '%<a %' OR posts.cooked LIKE '%<img %'").select(:id, :cooked) posts = Post.where("(posts.cooked LIKE '%<a %' OR posts.cooked LIKE '%<img %') AND posts.cooked LIKE '%/uploads/%'").select(:id, :cooked)
missing = [] count = 0
posts.find_each do |post| posts.find_each do |post|
missing = []
Nokogiri::HTML::fragment(post.cooked).css("a/@href", "img/@src").each do |media| Nokogiri::HTML::fragment(post.cooked).css("a/@href", "img/@src").each do |media|
src = media.value src = media.value
next if src.blank? || (src =~ /\/uploads\//).blank? next if src.blank? || (src =~ /\/uploads\//).blank?
@ -406,9 +408,13 @@ task 'posts:missing_uploads' => :environment do
missing << src unless Upload.get_from_url(src) || OptimizedImage.get_from_url(src) missing << src unless Upload.get_from_url(src) || OptimizedImage.get_from_url(src)
end end
missing.each { |src| PostCustomField.create!(post_id: post.id, name: name, value: src) } if missing.present?
missing.each { |src| PostCustomField.create!(post_id: post.id, name: name, value: src) }
count += missing.count
end
putc "." putc "."
end end
puts "", "#{missing.count} post uploads are missing.", "" puts "", "#{count} post uploads are missing.", ""
end end