diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 2326221..9230d13 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -7,10 +7,12 @@ def get_profile_image(user) private def image_for_handle(user) - if user.twitter_identity.present? - url_for(user.twitter_identity.profile_image) if user.twitter_identity.profile_image.attached? - elsif user.github_identity.present? - url_for(user.github_identity.profile_image) if user.github_identity.profile_image.attached? + if user.twitter_identity.present? && user.twitter_identity.profile_image.attached? + url_for(user.twitter_identity.profile_image) + elsif user.github_identity.present? && user.github_identity.profile_image.attached? + url_for(user.github_identity.profile_image) + else + 'https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png' end end -end \ No newline at end of file +end diff --git a/lib/tasks/images.rake b/lib/tasks/images.rake index 8d0f5ad..3483e82 100644 --- a/lib/tasks/images.rake +++ b/lib/tasks/images.rake @@ -1,17 +1,36 @@ namespace :images do desc 'Load images from Cloudinary and store in Active Storage' task load_images: :environment do - User.where('updated_at < ?', '2023-08-26').each do |user| - cloudinary_url = "https://res.cloudinary.com/whyloveruby/image/twitter_name/#{user.twitter_identity.handle}.jpg" + saved_count = 0 + error_count = 0 + batch_count = 0 + User.where('updated_at < ?', '2023-08-26').find_in_batches(batch_size: 20) do |batch| + batch.each do |user| + cloudinary_url = "https://res.cloudinary.com/whyloveruby/image/twitter_name/#{user.twitter_identity.handle}.jpg" + begin + downloaded_image = URI.parse(cloudinary_url).open + downloaded_image.rewind + + user.twitter_identity.profile_image.attach(io: downloaded_image, filename: "#{user.twitter_identity.handle}.jpg") - downloaded_image = URI.parse(cloudinary_url).open - - downloaded_image.rewind - - user.twitter_identity.profile_image.attach(io: downloaded_image, filename: "#{user.twitter_identity.handle}.jpg") - - puts "Image for #{user.twitter_identity.name} loaded and attached." + if user.twitter_identity.profile_image.attached? + saved_count += 1 + else + error_count += 1 + puts "\nError saving profile image for user #{user.twitter_identity.name}:" + puts user.twitter_identity.errors_full_message.to_sentence + end + rescue StandardError => e + # Handling the exception + error_count += 1 + puts "\nError downloading image for user #{user.id}: #{e.message}" + end + end + batch_count += 1 + puts "\nBatch #{batch_count} completed..." + puts "#{error_count} errors..." + puts "#{saved_count} saved..." end + puts "\n\nProcess Completed" end end - \ No newline at end of file diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 097dafb..5a1d7b6 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -17,7 +17,7 @@ context 'get_profile_image for User' do it 'should render image tag with the twitter handle from active storage' do - user = User.create(updated_at: Date.parse('2022-12-31').end_of_day) + user = User.create(name: "Test") user.external_identities.create(handle: "my_twitter_handle", provider: "twitter", uid: "252152") image_path = Rails.root.join('app', 'assets', 'images', 'rails.png') @@ -28,7 +28,7 @@ expect(generated_url).to match("profile_image.*my_twitter_handle\.png") end it 'should render image tag with the github handle from active storage' do - user = User.create(updated_at: Date.parse('2022-12-31').end_of_day) + user = User.create(name: "Test") user.external_identities.create(handle: "my_github_handle", provider: "github", uid: "252150") image_path = Rails.root.join('app', 'assets', 'images', 'rails.png')