Last active
December 17, 2015 05:39
-
-
Save HungYuHei/5559349 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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