Skip to content

Commit 1a8a2e6

Browse files
committed
Merge branch 'develop'
2 parents ffb0b81 + e48857e commit 1a8a2e6

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

app/models/media.rb

Lines changed: 1 addition & 1 deletion
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

Lines changed: 13 additions & 22 deletions
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-
image = Tempfile.new media.attachment_file_name
229-
begin
230-
puts "Re-saving image attachment #{media.id} - #{media.attachment_file_name}"
231-
media.attachment = image
232-
media.save
233-
# if there are multiple styles, you want to recreate them:
234-
media.attachment.reprocess!
235-
ensure
236-
image.close
237-
image.unlink
238-
end
224+
image = s3.get_object({bucket: ENV['S3_BUCKET_NAME'], key: object_key})
225+
puts "Re-saving image attachment #{media.id} - #{media.attachment_file_name}"
226+
media.attachment = image
227+
media.save
228+
# if there are multiple styles (thumbnail, etc), recreate them:
229+
media.attachment.reprocess!
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)