-
-
Save ngw/49ef28014bb648ffa63e 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
class Project | |
attr_reader :stylesheets_dir, :images_dir | |
def initialize subdomain, config | |
@subdomain = subdomain | |
@config = config | |
end | |
def process | |
sandbox | |
fetch | |
end | |
def sandbox | |
@project_dir = @config[ 'tmpdir' ] + "/#{ @subdomain }" | |
@stylesheets_dir = @project_dir + '/css' | |
@images_dir = @project_dir + '/img' | |
[ @project_dir, @stylesheets_dir, @images_dir ].each do |dir| | |
begin | |
Dir.mkdir( dir ) | |
rescue Errno::EEXIST => e | |
puts 'no buono' | |
end | |
end | |
end | |
def fetch | |
begin | |
item = Happening::S3::Item.new( | |
@config[ 'projects_bucket' ], @subdomain + '/project.zip', | |
:aws_access_key_id => @config[ 's3_access_key' ], | |
:aws_secret_access_key => @config[ 's3_secret_key' ] ) | |
item.get( :on_success => Proc.new { unpack } ) do |response| | |
open( @project_dir + '/project.zip', 'w' ) { |f| f.write( response.response ) } | |
end | |
rescue => e | |
puts e # CAN'T STAY LIKE THIS, NEEDS REAL LOGGING | |
end | |
end | |
def unpack | |
Zip::Archive.open( @project_dir + '/project.zip' ) do |archive| | |
archive.num_files.times do |elem| | |
name = Pathname.new( archive.get_name( elem ) ).basename | |
archive.fopen( name ) do |a| | |
if !( /__MACOSX/.match( name ) ) && !a.directory? | |
case File.mime_type?( name ) | |
when 'text/css' | |
open( @stylesheets_dir + "/#{ name }", 'w' ) do |file| | |
file.write( a.read ) | |
end | |
when 'image/jpeg' | |
open( @images_dir + "/#{ name }", 'w' ) do |file| | |
file.write( a.read ) | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment