Skip to content

Instantly share code, notes, and snippets.

@HungYuHei
Last active December 17, 2015 05:39
Show Gist options
  • Save HungYuHei/5559349 to your computer and use it in GitHub Desktop.
Save HungYuHei/5559349 to your computer and use it in GitHub Desktop.
# coding: utf-8
def convert_path(path)
path.gsub('/uploads/', '')
end
directory_path = "#{Rails.root}/tmp/grid"
FileUtils.mkdir_p directory_path + "/uploads/images"
grid = Mongo::GridFileSystem.new(Mongoid.database)
counter = 0
Image.all.each do |item|
begin
grid.open(convert_path(item.url), 'r') do |f|
new_file = File.new(directory_path + item.url, "w+")
new_file.puts(f.read.force_encoding('utf-8'))
new_file.close
counter = counter + 1
end
rescue Exception => e
puts '-------------------------------------------'
puts e.message
puts "id: #{item.id}"
puts '-------------------------------------------'
end
end
puts '########################### Done #################################'
puts " retrieved #{counter} images, total Image instances: #{Image.count}"
puts '##################################################################'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment