_includes/
_layouts/
_pages/
_posts/
assets/
css/
fonts/
images/
js/
.gitignore
62 B
Gemfile
68 B
Gemfile.lock
1.7 KiB
Makefile
198 B
README
192 B
_config.yml
278 B
avatar.png
44.3 KiB
cloudhead.gpg
892 B
favicon.png
179 B
index.md
79 B
publish
1.6 KiB
robots.txt
33 B
publish
raw
| 1 | #!/usr/bin/env ruby |
| 2 | |
| 3 | require 'aws-sdk-s3' |
| 4 | require 'digest/md5' |
| 5 | require 'rack/mime' |
| 6 | require 'pathname' |
| 7 | |
| 8 | CLIENT = Aws::S3::Client.new(region: ENV['AWS_REGION'] || 'eu-west-1') |
| 9 | BUCKET = File.basename(Dir.pwd) |
| 10 | |
| 11 | USAGE = <<EOF |
| 12 | usage: #{$0} file... |
| 13 | |
| 14 | the following environment vars will be used: |
| 15 | |
| 16 | AWS_REGION |
| 17 | AWS_ACCESS_KEY_ID |
| 18 | AWS_SECRET_ACCESS_KEY |
| 19 | |
| 20 | EOF |
| 21 | |
| 22 | def upload(files=["."]) |
| 23 | base = Pathname::new(files.first) |
| 24 | files = files.map do |f| |
| 25 | File.directory?(f) ? traverse(Dir.new(f)) : f |
| 26 | end.flatten |
| 27 | |
| 28 | files.each do |f| |
| 29 | print "#{f}.. " |
| 30 | |
| 31 | key = Pathname::new(f).relative_path_from(base).to_s |
| 32 | mime = Rack::Mime::MIME_TYPES[File.extname(f)] |
| 33 | cache = if /^text/.match?(mime) then 60 else 3600 end |
| 34 | |
| 35 | obj = CLIENT.get_object({bucket: BUCKET, key: key}) rescue nil |
| 36 | local_md5 = md5sum(f).chomp |
| 37 | remote_md5 = obj.etag.tr('"', '') if obj |
| 38 | |
| 39 | if local_md5.to_s != remote_md5.to_s |
| 40 | CLIENT.put_object( |
| 41 | body: File::new(f), |
| 42 | key: key, |
| 43 | bucket: BUCKET, |
| 44 | content_type: mime, |
| 45 | cache_control: "max-age=#{cache}", |
| 46 | acl: "public-read" |
| 47 | ) |
| 48 | |
| 49 | puts "ok" |
| 50 | else |
| 51 | puts "skipping" |
| 52 | end |
| 53 | end |
| 54 | end |
| 55 | |
| 56 | def md5sum(file_name) |
| 57 | Digest::MD5.hexdigest(File.read(file_name)) |
| 58 | end |
| 59 | |
| 60 | def traverse(dir) |
| 61 | list = [] |
| 62 | dir.each do |entry| |
| 63 | unless [".", "..", ".git"].include?(entry) |
| 64 | path = File.join(dir.path, entry) |
| 65 | if File.directory?(path) |
| 66 | list += traverse(Dir.new(path)) |
| 67 | else |
| 68 | list << path |
| 69 | end |
| 70 | end |
| 71 | end |
| 72 | list |
| 73 | end |
| 74 | |
| 75 | if ARGV[0] == "-h" |
| 76 | puts USAGE |
| 77 | exit 0 |
| 78 | end |
| 79 | |
| 80 | begin |
| 81 | upload(ARGV) |
| 82 | rescue Exception => e |
| 83 | $stderr.puts "error: #{e.message}" |
| 84 | exit 1 |
| 85 | end |