Skip to content

Commit e48857e

Browse files
Alex TharpAlex Tharp
Alex Tharp
authored and
Alex Tharp
committed
Merge pull request #270 from cbdr/fix-and-refactor-update_url-task
Fix and Refactor update_url Task
2 parents 6a5bb12 + 82a442f commit e48857e

File tree

2 files changed

+10
-19
lines changed

2 files changed

+10
-19
lines changed

app/models/media.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module PaperclipExtensions
22
module Attachment
3-
def arbitrary_url_for(pattern, style_name = :default)
3+
def arbitrary_url_for(pattern, style_name = :original)
44
Paperclip::Interpolations.interpolate pattern, self, style_name
55
end
66
end

lib/tasks/cortex.rake

+9-18
Original file line numberDiff line numberDiff line change
@@ -209,35 +209,26 @@ namespace :cortex do
209209
end
210210

211211
namespace :media do
212-
desc 'Manage Cortex media'
213-
task :update_url => :environment do
214-
old_url = ENV['OLD_PATH']
215-
unless old_url
216-
puts 'OLD_PATH must be set'
212+
desc 'Update existing Media assets on S3 with new URL structure. Specify new structure in model, then pass old_path with the previous structure, i.e.: ":class/:attachment/:style-:id.:extension"'
213+
task :update_url, [:old_path] => :environment do |t, args|
214+
s3 = Aws::S3::Client.new(region: ENV['S3_REGION'], access_key_id: ENV['AWS_ACCESS_KEY_ID'], secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'])
215+
unless args[:old_path]
216+
puts '"old_path" argument must be set'
217217
end
218218

219219
Media.find_each do |media|
220220
unless media.attachment_file_name.blank?
221-
object_key = media.attachment.arbitrary_url_for old_url
222-
223-
s3 = Aws::S3::Client.new(region: ENV['S3_REGION'], access_key_id: ENV['AWS_ACCESS_KEY_ID'], secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'])
221+
object_key = media.attachment.arbitrary_url_for args[:old_path]
224222

225223
begin
226-
s3.get_object({ bucket:ENV['S3_BUCKET_NAME'], key: object_key }, target: media.attachment_file_name)
227-
228-
file = File.new media.attachment_file_name
229-
224+
image = s3.get_object({bucket: ENV['S3_BUCKET_NAME'], key: object_key})
230225
puts "Re-saving image attachment #{media.id} - #{media.attachment_file_name}"
231-
image = file
232226
media.attachment = image
233227
media.save
234-
# if there are multiple styles, you want to recreate them:
228+
# if there are multiple styles (thumbnail, etc), recreate them:
235229
media.attachment.reprocess!
236-
237-
file.close
238-
File.delete media.attachment_file_name
239230
rescue => ex
240-
puts "An error of type #{ex.class} occurred, message is #{ex.message}"
231+
puts "An error of type #{ex.class} occurred, message is: '#{ex.message}'"
241232
end
242233
end
243234
end

0 commit comments

Comments
 (0)