Class: Jammit::S3Uploader

Inherits:
Object
  • Object
show all
Defined in:
lib/jammit/s3_uploader.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ S3Uploader

Returns a new instance of S3Uploader.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/jammit/s3_uploader.rb', line 5

def initialize(options = {})
  @bucket = options[:bucket]
  unless @bucket
    @bucket_name = options[:bucket_name] || Jammit.configuration[:s3_bucket]
    @access_key_id = options[:access_key_id] || Jammit.configuration[:s3_access_key_id]
    @secret_access_key = options[:secret_access_key] || Jammit.configuration[:s3_secret_access_key]
    @bucket_location = options[:bucket_location] || Jammit.configuration[:s3_bucket_location]
    @cache_control = options[:cache_control] || Jammit.configuration[:s3_cache_control]
    @acl = options[:acl] || Jammit.configuration[:s3_permission]

    @bucket = find_or_create_bucket
  end
end

Instance Method Details

#find_or_create_bucketObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/jammit/s3_uploader.rb', line 73

def find_or_create_bucket
  s3_service = S3::Service.new(:access_key_id => @access_key_id, :secret_access_key => @secret_access_key)

  # find or create the bucket
  begin
    s3_service.buckets.find(@bucket_name)
  rescue S3::Error::NoSuchBucket
    log "Bucket not found. Creating '#{@bucket_name}'..."
    bucket = s3_service.buckets.build(@bucket_name)

    location = (@bucket_location.to_s.strip.downcase == "eu") ? :eu : :us
    bucket.save(location)
    bucket
  end
end

#log(msg) ⇒ Object



89
90
91
# File 'lib/jammit/s3_uploader.rb', line 89

def log(msg)
  puts msg
end

#uploadObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jammit/s3_uploader.rb', line 19

def upload
  log "Pushing assets to S3 bucket: #{@bucket.name}"
  globs = []

  # add default package path
  if Jammit.gzip_assets
    globs << "public/#{Jammit.package_path}/**/*.gz"
  else
    globs << "public/#{Jammit.package_path}/**/*.css"
    globs << "public/#{Jammit.package_path}/**/*.js"
  end

  # add images
  globs << "public/images/**/*"

  # add custom configuration if defined
  s3_upload_files = Jammit.configuration[:s3_upload_files]
  globs << s3_upload_files if s3_upload_files.is_a?(String)
  globs += s3_upload_files if s3_upload_files.is_a?(Array)

  # upload all the globs
  globs.each do |glob|
    upload_from_glob(glob)
  end
end

#upload_from_glob(glob) ⇒ Object



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
# File 'lib/jammit/s3_uploader.rb', line 45

def upload_from_glob(glob)
  log "Pushing files from #{glob}"
  log "#{ASSET_ROOT}/#{glob}"
  Dir["#{ASSET_ROOT}/#{glob}"].each do |local_path|
    next if File.directory?(local_path)
    remote_path = local_path.gsub(/^#{ASSET_ROOT}\/public\//, "")

    use_gzip = false

    # handle gzipped files
    if File.extname(remote_path) == ".gz"
      use_gzip = true
      remote_path = remote_path.gsub(/\.gz$/, "")
    end

    log "pushing file to s3: #{remote_path}"

    # save to s3
    new_object = @bucket.objects.build(remote_path)
    new_object.cache_control = @cache_control if @cache_control
    new_object.content_type = MimeMagic.by_path(remote_path)
    new_object.content = open(local_path)
    new_object.content_encoding = "gzip" if use_gzip
    new_object.acl = @acl if @acl
    new_object.save
  end
end