Created
December 1, 2011 00:50
-
-
Save msaffitz/1412348 to your computer and use it in GitHub Desktop.
Extract OTZ images from a PG database and create them in the filesystem
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
require 'rubygems' | |
require 'pg' | |
require 'FileUtils' | |
def write_file(data, path, name) | |
return if data.nil? or data.empty? | |
return if File.exists?("#{path}/#{name}") | |
FileUtils.mkdir_p path | |
f = File.new("#{path}/#{name}", 'w') | |
f.write(PGconn.unescape_bytea(data)) | |
f.close | |
end | |
conn = PGconn.open(:dbname => 'otz') | |
puts "Fetching images ... " | |
images = conn.exec('select * from images') | |
puts "Extracting #{images.count} images ... " | |
images.each_with_index do |image, index| | |
puts " (#{index+1}/#{images.count}) .. Extracting #{image['img_file_name']}" | |
path = "extracted_images/#{image['id']}" | |
write_file(image['img_file'], "#{path}/original", image['img_file_name']) | |
write_file(image['img_medium_file'], "#{path}/medium", image['img_file_name']) | |
write_file(image['img_thumb_file'], "#{path}/thumb", image['img_file_name']) | |
end | |
puts "Complete." | |
conn.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment