Update paths of already created paper clip attachments

I had this inappropriate Paperclip configurator:

class Photo < ActiveRecord::Base

  has_attached_file :image, :storage => :s3,
                    :styles => { :medium => "600x600>", :small => "320x320>", :thumb => "100x100#" },
                    :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
                    :path => "/:style/:filename"
end

This is a mistake because two images cannot have the same size and file name. To fix this, I changed the configuration to:

class Photo < ActiveRecord::Base

  has_attached_file :image, :storage => :s3,
                    :styles => { :medium => "600x600>", :small => "320x320>", :thumb => "100x100#" },
                    :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
                    :path => "/:style/:id_:filename"
end

Unfortunately, this breaks all the URLs into attachments that I have already created. How can I update these file paths or otherwise make the urls work?

+3
source share
2 answers

I ended this manually with aws-s3gem:

Photo.all.map{|p| [p.image.path(:thumb), "/thumb/#{p.id}_#{p.image_file_name}"]}.each do |p|
  if AWS::S3::S3Object.exists? p[0], bucket_name
    AWS::S3::S3Object.rename p[0], p[1], bucket_name
  end
end

(Of course, I had to repeat it for each attachment style)

+3
source

You can run Photo.find_each { |photo| photo.image.reprocess! }from migration or even inside the console.

​​ , , paperclip. rake paperclip:refresh CLASS=Photo. RAILS_ENV, .

rake , , lib/tasks

+2

Source: https://habr.com/ru/post/1750193/


All Articles