Class: Fog::Storage::Local::Files

Inherits:
Collection
  • Object
show all
Defined in:
lib/fog/local/models/storage/files.rb

Instance Attribute Summary

Attributes inherited from Collection

#service

Instance Method Summary collapse

Methods inherited from Collection

#clear, #create, #destroy, #initialize, #inspect, #load, model, #model, #reload, #table, #to_json

Methods included from Attributes::ClassMethods

#_load, #aliases, #attribute, #attributes, #identity, #ignore_attributes, #ignored_attributes

Methods included from Core::DeprecatedConnectionAccessors

#connection, #connection=, #prepare_service_value

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #persisted?, #requires, #requires_one

Constructor Details

This class inherits a constructor from Fog::Collection

Instance Method Details

#allObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/local/models/storage/files.rb', line 14

def all
  requires :directory
  if directory.collection.get(directory.key)
    data = []
    Dir.chdir(service.path_to(directory.key)) {
      data = Dir.glob('**/*').reject do |file|
        ::File.directory?(file)
      end.map do |key|
        path = file_path(key)
        {
          :content_length => ::File.size(path),
          :key            => key,
          :last_modified  => ::File.mtime(path)
        }
      end
    }
    load(data)
  else
    nil
  end
end

#get(key, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fog/local/models/storage/files.rb', line 36

def get(key, &block)
  requires :directory
  path = file_path(key)
  if ::File.exists?(path)
    data = {
      :content_length => ::File.size(path),
      :key            => key,
      :last_modified  => ::File.mtime(path)
    }
    if block_given?
      file = ::File.open(path)
      while (chunk = file.read(Excon::CHUNK_SIZE)) && yield(chunk); end
      file.close
      new(data)
    else
      body = ::File.read(path)
      new(data.merge!(:body => body))
    end
  else
    nil
  end
end

#head(key) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/fog/local/models/storage/files.rb', line 59

def head(key)
  requires :directory
  path = file_path(key)
  if ::File.exists?(path)
    new({
      :content_length => ::File.size(path),
      :key            => key,
      :last_modified  => ::File.mtime(path)
    })
  else
    nil
  end
end

#new(attributes = {}) ⇒ Object



73
74
75
76
# File 'lib/fog/local/models/storage/files.rb', line 73

def new(attributes = {})
  requires :directory
  super({ :directory => directory }.merge!(attributes))
end