-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[local] rough first cut of local storage
- Loading branch information
Showing
11 changed files
with
344 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
module Fog | ||
module Local | ||
|
||
def self.new(options={}) | ||
|
||
unless @required | ||
require 'fog/local/models/directories' | ||
require 'fog/local/models/directory' | ||
require 'fog/local/models/file' | ||
require 'fog/local/models/files' | ||
@required = true | ||
end | ||
|
||
unless options[:local_root] | ||
raise ArgumentError.new('local_root is required to access local') | ||
end | ||
if Fog.mocking? | ||
Fog::Local::Mock.new(options) | ||
else | ||
Fog::Local::Real.new(options) | ||
end | ||
end | ||
|
||
def self.reset_data(keys=Mock.data.keys) | ||
Mock.reset_data(keys) | ||
end | ||
|
||
class Mock | ||
|
||
def self.data | ||
@data ||= Hash.new do |hash, key| | ||
hash[key] = {} | ||
end | ||
end | ||
|
||
def self.reset_data(keys=data.keys) | ||
for key in [*keys] | ||
data.delete(key) | ||
end | ||
end | ||
|
||
def initialize(options={}) | ||
@local_root = ::File.expand_path(options[:local_root]) | ||
@data = self.class.data[@local_root] | ||
end | ||
|
||
def local_root | ||
@local_root | ||
end | ||
|
||
def path(partial) | ||
partial | ||
end | ||
end | ||
|
||
class Real | ||
|
||
def initialize(options={}) | ||
@local_root = ::File.expand_path(options[:local_root]) | ||
end | ||
|
||
def local_root | ||
@local_root | ||
end | ||
|
||
def path_to(partial) | ||
::File.join(@local_root, partial) | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module Local | ||
class << self | ||
if Fog.credentials[:local_root] | ||
|
||
def initialized? | ||
true | ||
end | ||
|
||
def [](service) | ||
@@connections ||= Hash.new do |hash, key| | ||
credentials = Fog.credentials.reject do |k,v| | ||
![:local_root].include?(k) | ||
end | ||
hash[key] = case key | ||
when :files | ||
Fog::Local.new(credentials) | ||
end | ||
end | ||
@@connections[service] | ||
end | ||
|
||
def directories | ||
self[:files].directories | ||
end | ||
|
||
else | ||
|
||
def initialized? | ||
false | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'fog/collection' | ||
require 'fog/local/models/directory' | ||
|
||
module Fog | ||
module Local | ||
|
||
class Real | ||
def directories | ||
Fog::Local::Directories.new(:connection => self) | ||
end | ||
end | ||
|
||
class Mock | ||
def directories | ||
Fog::Local::Directories.new(:connection => self) | ||
end | ||
end | ||
|
||
class Directories < Fog::Collection | ||
|
||
model Fog::Local::Directory | ||
|
||
def all | ||
data = Dir.entries(connection.local_root).select do |entry| | ||
entry[0...1] != '.' && ::File.directory?(connection.path_to(entry)) | ||
end.map do |entry| | ||
{:name => entry} | ||
end | ||
load(data) | ||
end | ||
|
||
def get(name) | ||
if ::File.directory?(connection.path_to(name)) | ||
new(:name => name) | ||
else | ||
nil | ||
end | ||
end | ||
|
||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'fog/model' | ||
# require 'fog/local/models/files' | ||
|
||
module Fog | ||
module Local | ||
|
||
class Directory < Fog::Model | ||
|
||
identity :name | ||
|
||
def destroy | ||
requires :name | ||
|
||
if ::File.directory?(path) | ||
Dir.rmdir(path) | ||
true | ||
else | ||
false | ||
end | ||
end | ||
|
||
def files | ||
@files ||= begin | ||
Fog::Local::Files.new( | ||
:directory => self, | ||
:connection => connection | ||
) | ||
end | ||
end | ||
|
||
def save | ||
requires :name | ||
|
||
Dir.mkdir(path) | ||
true | ||
end | ||
|
||
private | ||
|
||
def path | ||
connection.path_to(name) | ||
end | ||
|
||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
require 'fog/model' | ||
|
||
module Fog | ||
module Local | ||
|
||
class File < Fog::Model | ||
|
||
identity :key, 'Key' | ||
|
||
attr_accessor :body | ||
attribute :content_length, 'Content-Length' | ||
# attribute :content_type, 'Content-Type' | ||
attribute :last_modified, 'Last-Modified' | ||
|
||
def body | ||
@body ||= if last_modified | ||
collection.get(identity).body | ||
else | ||
'' | ||
end | ||
end | ||
|
||
def directory | ||
@directory | ||
end | ||
|
||
def destroy | ||
requires :directory, :key | ||
::File.delete(path) | ||
true | ||
end | ||
|
||
def save(options = {}) | ||
requires :body, :directory, :key | ||
file = ::File.new(path, 'w') | ||
file.write(body) | ||
file.close | ||
merge_attributes( | ||
:content_length => ::File.size(path), | ||
:last_modified => ::File.mtime(path) | ||
) | ||
true | ||
end | ||
|
||
private | ||
|
||
def directory=(new_directory) | ||
@directory = new_directory | ||
end | ||
|
||
def path | ||
connection.path_to(::File.join(directory.name, key)) | ||
end | ||
|
||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
require 'fog/collection' | ||
require 'fog/local/models/file' | ||
|
||
module Fog | ||
module Local | ||
|
||
class Files < Fog::Collection | ||
|
||
model Fog::Local::File | ||
|
||
def all | ||
if directory.collection.get(directory.name) | ||
data = Dir.entries(connection.path_to(directory.name)).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 => key, | ||
:last_modified => ::File.mtime(path) | ||
} | ||
end | ||
load(data) | ||
else | ||
nil | ||
end | ||
end | ||
|
||
def directory | ||
@directory | ||
end | ||
|
||
def get(key, &block) | ||
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 = nil | ||
::File.open(path) do |file| | ||
body = file.read | ||
end | ||
new(data.merge!(:body => body)) | ||
end | ||
else | ||
nil | ||
end | ||
end | ||
|
||
def new(attributes = {}) | ||
super({ :directory => directory }.merge!(attributes)) | ||
end | ||
|
||
private | ||
|
||
def directory=(new_directory) | ||
@directory = new_directory | ||
end | ||
|
||
def file_path(key) | ||
connection.path_to(::File.join(directory.name, key)) | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
require 'fog/model' | ||
require 'fog/rackspace/models/files/files' | ||
|
||
module Fog | ||
module Rackspace | ||
|