Class: CloudCrooner::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud_crooner/storage.rb

Instance Method Summary collapse

Constructor Details

#initializeStorage

Returns a new instance of Storage.



7
8
9
10
11
12
# File 'lib/cloud_crooner/storage.rb', line 7

def initialize
  @bucket_name = CloudCrooner.bucket_name 
  @prefix =      CloudCrooner.prefix 
  @fog_options = CloudCrooner.fog_options 
  @manifest =    CloudCrooner.manifest 
end

Instance Method Details

#bucketObject

Returns the remote assets



24
25
26
# File 'lib/cloud_crooner/storage.rb', line 24

def bucket
  @bucket ||= connection.directories.get(@bucket_name, :prefix => @prefix)
end

#clean_remoteObject

Analogue to CloudCrooner::cleansprockets_assets - deletes old backups of assets



114
115
116
117
118
119
# File 'lib/cloud_crooner/storage.rb', line 114

def clean_remote
  to_delete = remote_assets - local_compiled_assets
  to_delete.each do |f|
    delete_remote_asset(bucket.files.get(f))
  end
end

#connectionObject

Creates a new Fog connection



17
18
19
# File 'lib/cloud_crooner/storage.rb', line 17

def connection
  @connection ||= Fog::Storage.new(@fog_options)
end

#delete_remote_asset(f) ⇒ Object



105
106
107
108
# File 'lib/cloud_crooner/storage.rb', line 105

def delete_remote_asset(f)
  log "Deleting #{f.key} from remote"
  f.destroy
end

#exists_on_remote?(file) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/cloud_crooner/storage.rb', line 35

def exists_on_remote?(file)
  bucket.files.head(file)
end

#local_compiled_assetsObject

Compiled assets prepended with prefix for comparison against remote



31
32
33
# File 'lib/cloud_crooner/storage.rb', line 31

def local_compiled_assets 
  @manifest.files.keys.map {|f| File.join(@prefix, f)} 
end

#log(msg) ⇒ Object



49
50
51
# File 'lib/cloud_crooner/storage.rb', line 49

def log(msg)
  CloudCrooner.log(msg)
end

#remote_assetsObject



99
100
101
102
103
# File 'lib/cloud_crooner/storage.rb', line 99

def remote_assets
  files = []
  bucket.files.each { |f| files << f.key }
  return files
end

#upload_file(f) ⇒ Object

Uploads a file to the bucket. Sets expires header to one year. If a gzipped version of the file exists and is a smaller file size



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
94
95
96
97
# File 'lib/cloud_crooner/storage.rb', line 57

def upload_file(f)
  # grabs the compiled asset from public_path
  full_file_path = File.join(File.dirname(@manifest.dir), f)
  one_year = 31557600
  mime = Rack::Mime.mime_type(File.extname(f)) 
  file = {
    :key => f,
    :public => true,
    :content_type => mime,
    :cache_control => "public, max-age=#{one_year}",
    :expires => CGI.rfc1123_date(Time.now + one_year) 
  }

  gzipped = "#{full_file_path}.gz" 

  if File.exists?(gzipped)
    original_size = File.size(full_file_path)
    gzipped_size = File.size(gzipped)

    if gzipped_size < original_size
      file.merge!({
        :body => File.open(gzipped),
        :content_encoding => 'gzip'
      })
      log "Uploading #{gzipped} in place of #{f}"
    else
      file.merge!({
        :body => File.open(full_file_path)
      })
      log "Gzip exists but has larger file size, uploading #{f}"
    end
  else
    file.merge!({
      :body => File.open(full_file_path)
    })
    log "Uploading #{f}"
  end
  # put in reduced redundancy option here later if desired

  file =  bucket.files.create( file )
end

#upload_filesObject

Upload all new files to the bucket



42
43
44
45
46
47
# File 'lib/cloud_crooner/storage.rb', line 42

def upload_files
  files_to_upload = local_compiled_assets.reject { |f| exists_on_remote?(f) }
  files_to_upload.each do |asset|
    upload_file(asset)
  end
end