Class: CloudTempfile::Storage
- Inherits:
-
Object
- Object
- CloudTempfile::Storage
- Defined in:
- lib/cloud_tempfile/storage.rb
Defined Under Namespace
Classes: BucketNotFound
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
Instance Method Summary collapse
-
#connection ⇒ Fog::Storage
This “connection” is the fog connection responsible for persisting the file.
-
#delete_expired_tempfiles ⇒ Object
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.
-
#delete_file(f) ⇒ Object
This action will delete a AWS::File from the specified directory.
-
#directory(options = {}) ⇒ Fog::Storage::Directory
This “directory” action should only be used with the cloud provider and returns a
Fog::Storage::Directory
class. -
#get_remote_files ⇒ Array
Returns a list of remote files for the specified directory.
-
#initialize(cfg) ⇒ Storage
constructor
A new instance of Storage.
-
#local_file(f, body, options = {}) ⇒ Fog::Storage::Local::File
Used with the “Local” provider which will utilize the local file system with Fog.
-
#upload_file(f, body, options = {}) ⇒ Fog::Storage::File
This action will upload a Fog::Storage::File to the specified directory.
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
#config ⇒ Object
Returns the value of attribute config.
10 11 12 |
# File 'lib/cloud_tempfile/storage.rb', line 10 def config @config end |
Instance Method Details
#connection ⇒ Fog::Storage
This “connection” is the fog connection responsible for persisting the file
18 19 20 |
# File 'lib/cloud_tempfile/storage.rb', line 18 def connection @connection ||= ::Fog::Storage.new(self.config.) end |
#delete_expired_tempfiles ⇒ Object
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
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
24 25 26 27 |
# File 'lib/cloud_tempfile/storage.rb', line 24 def directory(={}) prefix = .has_key?(:prefix)? [:prefix] : self.config.prefix @directory ||= connection.directories.get(self.config.fog_directory, :prefix => prefix) end |
#get_remote_files ⇒ Array
Returns a list of remote files for the specified directory
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.
43 44 45 46 47 48 49 |
# File 'lib/cloud_tempfile/storage.rb', line 43 def local_file(f, body, ={}) #return !self.config.public? file = init_fog_file(f, body, ) # If the "file" is empty then return nil return nil if file.nil? || file.blank? || local_root.nil? file = local_root().files.create( file ) end |
#upload_file(f, body, options = {}) ⇒ Fog::Storage::File
This action will upload a Fog::Storage::File to the specified directory
31 32 33 34 35 36 37 38 |
# File 'lib/cloud_tempfile/storage.rb', line 31 def upload_file(f, body, ={}) file = init_fog_file(f, body, ) if self.config.enabled? return directory().files.create( file ) else return local_file(f, body, ) end end |