Skip to content

Instantly share code, notes, and snippets.

@butcher
Created December 20, 2010 14:20
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 butcher/748417 to your computer and use it in GitHub Desktop.
Save butcher/748417 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'cloudfiles'
require 'paperclip'
# turn off paperclip logging because they use ActiveRecord
Paperclip.options[:log] = false
container = 'assets-test'
credentials = {:username => 'username', :api_key => '1234567890123456789'}
file_list = Dir["#{ARGV[0].sub(/\/+\Z/, '')}/**/*"]
ATTACHEMENTS = {
"product_images" => {
:smallest => {:geometry => "27x26#"},
:extra_small => {:geometry => "46x46#"},
:small => {:geometry => "85x85"},
:extra_medium => {:geometry => "224x150#"},
:medium => {:geometry => "320x240#"},
:big => {:geometry => "575x387>"}
},
"customize/appearances" => {
:small => {:geometry => "27x26", :format => :png}
},
"generic_images" => {
:medium => {:geometry => "300x300>"},
:thumb => {:geometry => "100x100>"},
:big => {:geometry => "954x308#"},
:small => {:geometry => "85x85#"}
},
"customers/avatars" => {
:small => {:geometry => "34x34#"},
:thumb => {:geometry => "58x58#"},
:normal => {:geometry => "116x116#"},
:medium => {:geometry => "116x116#"}
},
"image_versions" => {
:small => {:geometry => "27x26", :format => :png},
:medium => {:geometry => "320x240>", :format => :png},
:big => {:geometry => "575x387>", :format => :png}
},
"media" => {
:small_thumb => {:geometry => "50x50", :format => :jpg},
:medium_thumb => {:geometry => "100x100", :format => :jpg},
:large_thumb => {:geometry => "370x370", :format => :jpg},
:detail_preview => {:geometry => "450x338", :format => :jpg}
}
}
$stderr.print("connecting to cloudfiles... ")
connection = CloudFiles::Connection.new(credentials)
$stderr.puts("OK")
def connection.upload_files(container, file_list)
$stderr.print("opening container '#{container}'... ")
container = self.container(container)
$stderr.puts("OK")
file_list.each do |file_name|
next unless File.file?(file_name)
# check to image attachment
if (file_name =~ /system\/images\/printer/) and !(file_name =~ /\/(media_thumbs)|(logos)|(label_images)\//)
# skip if from attchment and not original
next unless file_name =~ /\/original\//
# get styles by name
attachment_type = file_name.match(/printer\/([a-z_\/]*)\/\d*/)[1] rescue $stderr.puts("Can`t parse filename to find attachment type.")
unless ATTACHEMENTS[attachment_type]
$stderr.puts("Don`t know attachment for '#{attachment_type}'")
next
end
# crop, resize and upload
original_file = File.open(file_name, "r")
$stderr.puts("Found paperclip original file. #{file_name}")
ATTACHEMENTS[attachment_type].each do |name, options|
cropped_file = Paperclip::Thumbnail.new(original_file, options).make
cropped_name = file_name.gsub(/\/original\//, "/#{name}/")
# change extension in image name if option[:format] exists
cropped_name.sub!(/[a-zA-Z]*$/, options[:format].to_s) if options[:format]
$stderr.print("\t#{name.to_s.upcase}:\tcreate object #{cropped_name}\t")
object = container.create_object(cropped_name)
$stderr.print("OK\tupload... ")
object.write(cropped_file)
$stderr.puts("OK")
end
# upload original
$stderr.print("\tORIG:\tcreate object #{file_name}\t")
object = container.create_object(file_name)
$stderr.print("OK\tupload... ")
object.write(original_file)
$stderr.print("OK\n\n")
original_file.close
else
$stderr.print("create object #{file_name}... ")
object = container.create_object(file_name)
$stderr.print("OK\tupload... ")
File.open(file_name, 'r') do |file|
object.write(file)
end
$stderr.puts("OK")
end
end
end
connection.upload_files(container, file_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment