-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e316506
commit d454693
Showing
3 changed files
with
38 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters