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

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

Instance Attribute Summary

Attributes inherited from Collection

#connection

Instance Method Summary collapse

Methods inherited from Collection

#clear, #create, #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 Attributes::InstanceMethods

#_dump, #attributes, #identity, #identity=, #merge_attributes, #new_record?, #requires

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
# File 'lib/fog/storage/models/local/files.rb', line 14

def all
  requires :directory
  if directory.collection.get(directory.key)
    data = Dir.entries(connection.path_to(directory.key)).select do |key|
      key[0...1] != '.' && !::File.directory?(connection.path_to(key))
    end.map do |key|
      path = file_path(key)
      {
        :content_length => ::File.size(path),
        :key            => CGI.unescape(key),
        :last_modified  => ::File.mtime(path)
      }
    end
    load(data)
  else
    nil
  end
end

#get(key, &block) ⇒ Object



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

def get(key, &block)
  requires :directory
  path = file_path(CGI.escape(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



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

def head(key)
  requires :directory
  path = file_path(CGI.escape(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



70
71
72
73
# File 'lib/fog/storage/models/local/files.rb', line 70

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