Skip to content

Instantly share code, notes, and snippets.

@nhocki
Created August 3, 2011 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhocki/1122011 to your computer and use it in GitHub Desktop.
Save nhocki/1122011 to your computer and use it in GitHub Desktop.
Transfer PaperClip attachment files from S3 to the filesystem
@base = File.join(Rails.root, "public/system/attachments/#{Rails.env}/")
# model: Class
# method: :symbol
# eg transfer(Image, :file)
def transfer(model, method)
dummy = model.new.send(method)
styles = [nil] + dummy.styles.keys
model.all.each do |obj|
styles.each do |style|
cur = obj.send(method)
FileUtils.mkdir_p(File.join(@base, cur.path(style)).gsub(/\/[^\/]*?$/, ''))
f = File.open(File.join(@base, cur.path(style)), 'w')
begin
f.write(cur.to_file(style).read)
f.close
rescue; 0; end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment