Class: CloudTempfile::Storage

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

Defined Under Namespace

Classes: BucketNotFound

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cfg) ⇒ Storage

Returns a new instance of Storage.



12
13
14
# File 'lib/cloud_tempfile/storage.rb', line 12

def initialize(cfg)
  @config = cfg
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



10
11
12
# File 'lib/cloud_tempfile/storage.rb', line 10

def config
  @config
end

Instance Method Details

#connectionFog::Storage

This “connection” is the fog connection responsible for persisting the file

Returns:



18
19
20
# File 'lib/cloud_tempfile/storage.rb', line 18

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

#delete_expired_tempfilesObject

Delete the expired file which are expired if the “config.clean_up” is true and “config.clean_up_older_than” is the amount of seconds it is older than.

Note: This action should be used with the “bundle exec rake cloud_temp_file:clear” rake command (cronjob)



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cloud_tempfile/storage.rb', line 73

def delete_expired_tempfiles
  return if !self.config.clean_up? || !self.config.enabled?
  log "CloudTempfile.delete_expired_tempfiles is running..."
  # Delete expired temp files
  fog_files = (self.config.local?)? local_root.files : get_remote_files
  fog_files.each do |file|
    if file.last_modified <= Time.now.utc.ago(self.config.clean_up_older_than)
      delete_file(file)
    end
  end
  log "CloudTempfile.delete_expired_tempfiles is complete!"
end

#delete_file(f) ⇒ Object

This action will delete a AWS::File from the specified directory

Parameters:

  • (Fog::Storage::File)


53
54
55
56
57
# File 'lib/cloud_tempfile/storage.rb', line 53

def delete_file(f)
  #return false if !f.kind_of?(Fog::Storage::AWS::File) || !storage_provider.eql?(:aws)
  log "Deleting: #{f.key}"
  return f.destroy
end

#directory(options = {}) ⇒ Fog::Storage::Directory

This “directory” action should only be used with the cloud provider and returns a Fog::Storage::Directory class

Returns:

  • (Fog::Storage::Directory)


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

def directory(options={})
  prefix = options.has_key?(:prefix)? options[:prefix] : self.config.prefix
  @directory ||= connection.directories.get(self.config.fog_directory, :prefix => prefix)
end

#get_remote_filesArray

Returns a list of remote files for the specified directory

Returns:

  • (Array)

Raises:



62
63
64
65
66
67
# File 'lib/cloud_tempfile/storage.rb', line 62

def get_remote_files
  raise BucketNotFound.new("#{self.config.fog_provider} Bucket: #{self.config.fog_directory} not found.") unless directory
  files = []
  directory.files.each { |f| files << f if File.extname(f.key).present? }
  return files
end

#local_file(f, body, options = {}) ⇒ Fog::Storage::Local::File

Used with the “Local” provider which will utilize the local file system with Fog. This is handy for development and test environments.

Returns:

  • (Fog::Storage::Local::File)


43
44
45
46
47
48
49
# File 'lib/cloud_tempfile/storage.rb', line 43

def local_file(f, body, options={})
  #return !self.config.public?
  file = init_fog_file(f, body, options)
  # If the "file" is empty then return nil
  return nil if file.nil? || file.blank? || local_root.nil?
  file = local_root(options).files.create( file )
end

#upload_file(f, body, options = {}) ⇒ Fog::Storage::File

This action will upload a Fog::Storage::File to the specified directory

Returns:

  • (Fog::Storage::File)


31
32
33
34
35
36
37
38
# File 'lib/cloud_tempfile/storage.rb', line 31

def upload_file(f, body, options={})
  file = init_fog_file(f, body, options)
  if self.config.enabled?
    return directory(options).files.create( file )
  else
    return local_file(f, body, options)
  end
end