23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/middleman-aws-deploy/s3deploy.rb', line 23
def registered(app, opts_data = {}, &block)
@@options = Opts.new(opts_data)
yield @@options if block_given?
@@options.threads ||= 8
app.send :include, Options
app.after_build do
puts "== Uploading to S3"
def storage
@storage ||= Fog::Storage.new({
:provider => 'AWS',
:aws_access_key_id => aws_s3_opts.access_key_id,
:aws_secret_access_key => aws_s3_opts.secret_access_key,
:region => aws_s3_opts.region
})
end
def remote_files
@directory ||= storage.directories.get(aws_s3_opts.bucket)
@remote_files ||= @directory.files
end
def etag(key)
object = remote_files.head(key)
object && object.etag
end
def upload(key)
begin
remote_files.new({
:key => key,
:body => File.open("./build/#{key}"),
:public => true,
:acl => 'public-read'
}).save
rescue
$stderr.puts "Failed to upload #{key}"
end
end
def files
@files ||= Dir["./build/**/.*", "./build/**/*"]
.reject{|f| File.directory?(f) }.map{|f| f.sub(/^(?:\.\/)?build\//, '') }
end
replace = []
progress = ProgressBar.new('Hash check', files.count)
Parallel.each(files,
:in_threads => aws_s3_opts.threads,
:finish => lambda {|i, item| progress.inc }) do |f|
md5 = Digest::MD5.hexdigest(File.read("./build/#{f}"))
replace << f if md5 != etag(f)
end
progress.finish
if replace.empty?
puts "Nothing to upload"
else
progress = ProgressBar.new('Uploading', replace.count)
Parallel.each(replace,
:in_threads => aws_s3_opts.threads,
:finish => lambda {|i, item| progress.inc }) do |f|
upload(f)
end
progress.finish
end
end
end
|