Skip to content

Instantly share code, notes, and snippets.

@dariocravero
Created January 26, 2013 17:58
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 dariocravero/6ee6a669d583ff9c0da4 to your computer and use it in GitHub Desktop.
Save dariocravero/6ee6a669d583ff9c0da4 to your computer and use it in GitHub Desktop.
# encoding: utf-8
require 'rubygems'
require 'cloudfiles'
require 'colored'
require 'yaml'
desc "refresh precompiled assets in the rackspace CloudFiles CDN"
task :sync_assets do
root = Padrino.root
config = YAML.load_file(File.join(root, 'config', 'assets.yml'))
cf = CloudFiles::Connection.new(
:username => config[:cdn][:username],
:api_key => config[:cdn][:api_key]
)
config[:assets].each do |asset|
asset_container = cf.container(asset[:container])
dir = File.join(root, asset[:path])
# Get File Lists
cdn_files = asset_container.objects
local_files = asset[:types].map do |type|
Dir[File.join(dir, "*#{type}")]
end.flatten
to_delete = cdn_files.reject { |i| local_files.include?(File.join(dir, i).to_s)}
to_upload = local_files.reject { |i| cdn_files.include?(i[dir.to_s.length+1..-1]) }
# Upload fresh assets
to_upload.each do |f|
begin
unless FileTest.directory?(f) # ignore directories
relative_path = f[dir.to_s.length+1..-1]
type = Rack::Mime.mime_type(File.extname(relative_path))
puts " ↑".green + " Uploading -> #{relative_path} [#{type}]"
file = open(f)
object = asset_container.create_object relative_path, true
object.write file
object.content_type = type
file.close
end
rescue => e
logger.error e.message
logger.error e.backtrace.join("\n")
end
end
# Delete files that no longer exist
to_delete.each do |f|
begin
if asset_container.delete_object(f)
puts " ☓".red + " Deleted -> #{f}"
end
rescue => e
logger.error e.message
logger.error e.backtrace.join("\n")
end
end
puts " Deleted " + to_delete.size.to_s + " file(s)"
puts " Uploaded " + to_upload.size.to_s + " file(s)"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment