fog-local 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7aa87f144c0161b011bf8b98de92dc71384bd116
4
+ data.tar.gz: 2c12c547029f47e8f05b4e142c436da8baa557f9
5
+ SHA512:
6
+ metadata.gz: 5191d18d28bfaff0193029a1d098f1481369e0fedd2a8e103770d5c341070383887d4858a8c36b7218bd8f060f372dbf680745d96ca157522a049c28dc4591af
7
+ data.tar.gz: 4396615bc48dac3976ffbcd26a85588868e7cb0d982befcbcc602f7707cd85e11e476faa772c45b440e31d07407f88e79a1fb91d917faff256364540cc098283
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ tests/.fog
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ sudo: false
3
+ script: bundle exec rake test
4
+ rvm:
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - 2.1.5
8
+ - 2.2.0
9
+ - jruby-19mode
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,18 @@
1
+ ## Getting Involved
2
+
3
+ New contributors are always welcome, when it doubt please ask questions. We strive to be an open and welcoming community. Please be nice to one another.
4
+
5
+ ### Coding
6
+
7
+ * Pick a task:
8
+ * Offer feedback on open [pull requests](https://github.com/fog/fog/pulls).
9
+ * Review open [issues](https://github.com/fog/fog/issues) for things to help on.
10
+ * [Create an issue](https://github.com/fog/fog/issues/new) to start a discussion on additions or features.
11
+ * Fork the project, add your changes and tests to cover them in a topic branch.
12
+ * Commit your changes and rebase against `fog/fog` to ensure everything is up to date.
13
+ * [Submit a pull request](https://github.com/fog/fog/compare/).
14
+
15
+ ### Non-Coding
16
+
17
+ * Offer feedback on open [issues](https://github.com/fog/fog/issues).
18
+ * Organize or volunteer at events.
data/CONTRIBUTORS.md ADDED
@@ -0,0 +1,18 @@
1
+ * geemus <geemus@gmail.com>
2
+ * Lance Ivy <lance@cainlevy.net>
3
+ * Paul Thornthwaite <tokengeek@gmail.com>
4
+ * Benjamin Manns <benmanns@gmail.com>
5
+ * Sjoerd Andringa <sjoerd.andringa@me.com>
6
+ * Juris Galang <jurisgalang@gmail.com>
7
+ * Paul Thornthwaite <paul@brightbox.co.uk>
8
+ * Jamie Paton <jamiep@activestate.com>
9
+ * Thomas Wright <tfwright@gmail.com>
10
+ * Wesley Beary <me@geemus.com>
11
+ * Karl Freeman <karlfreeman@gmail.com>
12
+ * Andy Lindeman <andy@andylindeman.com>
13
+ * Athir Nuaimi <anuaimi@devfoundry.com>
14
+ * Brian D. Burns <iosctr@gmail.com>
15
+ * Jade Tucker <jadeatucker@gmail.com>
16
+ * James Herdman <james.herdman@me.com>
17
+ * Adam Tanner <adam@adamtanner.org>
18
+ * Mark Yen <marky@activestate.com>
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fog-local.gemspec
4
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 [CONTRIBUTORS.md](https://github.com/fog/fog/blob/master/CONTRIBUTORS.md)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # Fog::Local
2
+
3
+ ![Gem Version](https://badge.fury.io/rb/fog-local.svg)
4
+ [![Build Status](https://travis-ci.org/fog/fog-local.svg?branch=master)](https://travis-ci.org/fog/fog-local)
5
+ [![Dependency Status](https://gemnasium.com/fog/fog-local.svg)](https://gemnasium.com/fog/fog-local)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'fog-local'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install fog-local
22
+
23
+ ## Usage
24
+
25
+ Initialise a `Fog::Storage` object using local provider
26
+
27
+ ```ruby
28
+
29
+ storage = Fog::Storage.new({
30
+ :local_root => '~/fog',
31
+ :provider => 'Local'
32
+ })
33
+
34
+ ```
35
+
36
+ This can then be used like any other [Fog storage](http://fog.io/storage/).
37
+
38
+ ```ruby
39
+ directory = storage.directories.create(:key => 'data')
40
+ directory.files.create(:body => 'Hello World!', :key => 'hello_world.txt')
41
+ ```
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it ( https://github.com/fog/fog-local/fork )
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default => :test
4
+
5
+ mock = ENV['FOG_MOCK'] || 'true'
6
+ task :test do
7
+ sh("export FOG_MOCK=#{mock} && bundle exec shindont")
8
+ end
data/fog-local.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fog/local/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fog-local"
8
+ spec.version = Fog::Local::VERSION
9
+ spec.authors = ["Wesley Beary", "Ville Lautanala"]
10
+ spec.email = ["geemus@gmail.com", "lautis@gmail.com"]
11
+
12
+ spec.summary = %q{Module for the 'fog' gem to support local filesystem storage.}
13
+ spec.description = %q{This library can be used as a module for `fog` or as standalone provider
14
+ to use local filesystem storage.}
15
+ spec.homepage = "https://github.com/fog/fog-local"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.7'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'shindo', '~> 0.3'
25
+
26
+ spec.add_dependency 'fog-core', '~> 1.27'
27
+ end
data/lib/fog-local.rb ADDED
@@ -0,0 +1 @@
1
+ require 'fog/local'
@@ -0,0 +1,29 @@
1
+ class Local < Fog::Bin
2
+ class << self
3
+ def class_for(key)
4
+ case key
5
+ when :storage
6
+ Fog::Storage::Local
7
+ else
8
+ raise ArgumentError, "Unsupported #{self} service: #{key}"
9
+ end
10
+ end
11
+
12
+ def [](service)
13
+ @@connections ||= Hash.new do |hash, key|
14
+ hash[key] = case key
15
+ when :storage
16
+ Fog::Logger.warning("Local[:storage] is not recommended, use Storage[:local] for portability")
17
+ Fog::Storage.new(:provider => 'Local')
18
+ else
19
+ raise ArgumentError, "Unrecognized service: #{key.inspect}"
20
+ end
21
+ end
22
+ @@connections[service]
23
+ end
24
+
25
+ def services
26
+ Fog::Local.services
27
+ end
28
+ end
29
+ end
data/lib/fog/local.rb ADDED
@@ -0,0 +1 @@
1
+ require 'fog/local/storage'
@@ -0,0 +1,9 @@
1
+ require 'fog/core'
2
+
3
+ module Fog
4
+ module Local
5
+ extend Fog::Provider
6
+
7
+ service(:storage, 'Storage')
8
+ end
9
+ end
@@ -0,0 +1,33 @@
1
+ require 'fog/core/collection'
2
+ require 'fog/local/models/storage/directory'
3
+
4
+ module Fog
5
+ module Storage
6
+ class Local
7
+ class Directories < Fog::Collection
8
+ model Fog::Storage::Local::Directory
9
+
10
+ def all
11
+ data = if ::File.directory?(service.local_root)
12
+ Dir.entries(service.local_root).select do |entry|
13
+ entry[0...1] != '.' && ::File.directory?(service.path_to(entry))
14
+ end.map do |entry|
15
+ {:key => entry}
16
+ end
17
+ else
18
+ []
19
+ end
20
+ load(data)
21
+ end
22
+
23
+ def get(key, options = {})
24
+ if ::File.directory?(service.path_to(key))
25
+ new(:key => key)
26
+ else
27
+ nil
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,53 @@
1
+ require 'fog/core/model'
2
+ require 'fog/local/models/storage/files'
3
+
4
+ module Fog
5
+ module Storage
6
+ class Local
7
+ class Directory < Fog::Model
8
+ identity :key
9
+
10
+ def destroy
11
+ requires :key
12
+
13
+ if ::File.directory?(path)
14
+ Dir.rmdir(path)
15
+ true
16
+ else
17
+ false
18
+ end
19
+ end
20
+
21
+ def files
22
+ @files ||= begin
23
+ Fog::Storage::Local::Files.new(
24
+ :directory => self,
25
+ :service => service
26
+ )
27
+ end
28
+ end
29
+
30
+ def public=(new_public)
31
+ new_public
32
+ end
33
+
34
+ def public_url
35
+ nil
36
+ end
37
+
38
+ def save
39
+ requires :key
40
+
41
+ FileUtils.mkpath(path)
42
+ true
43
+ end
44
+
45
+ private
46
+
47
+ def path
48
+ service.path_to(key)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,132 @@
1
+ require 'fileutils'
2
+ require 'fog/core/model'
3
+
4
+ module Fog
5
+ module Storage
6
+ class Local
7
+ class File < Fog::Model
8
+ identity :key, :aliases => 'Key'
9
+
10
+ attribute :content_length, :aliases => 'Content-Length', :type => :integer
11
+ # attribute :content_type, :aliases => 'Content-Type'
12
+ attribute :last_modified, :aliases => 'Last-Modified'
13
+
14
+ require 'uri'
15
+
16
+ def body
17
+ attributes[:body] ||= if last_modified
18
+ collection.get(identity).body
19
+ else
20
+ ''
21
+ end
22
+ end
23
+
24
+ def body=(new_body)
25
+ attributes[:body] = new_body
26
+ end
27
+
28
+ def content_type
29
+ @content_type ||= begin
30
+ unless (mime_types = ::MIME::Types.of(key)).empty?
31
+ mime_types.first.content_type
32
+ end
33
+ end
34
+ end
35
+
36
+ def directory
37
+ @directory
38
+ end
39
+
40
+ def copy(target_directory_key, target_file_key, options={})
41
+ requires :directory, :key
42
+ service.copy_object(directory.key, key, target_directory_key, target_file_key)
43
+ target_directory = service.directories.new(:key => target_directory_key)
44
+ target_directory.files.get(target_file_key)
45
+ end
46
+
47
+ def destroy
48
+ requires :directory, :key
49
+ ::File.delete(path) if ::File.exist?(path)
50
+ dirs = path.split(::File::SEPARATOR)[0...-1]
51
+ dirs.length.times do |index|
52
+ dir_path = dirs[0..-index].join(::File::SEPARATOR)
53
+ if dir_path.empty? # path starts with ::File::SEPARATOR
54
+ next
55
+ end
56
+ # don't delete the containing directory or higher
57
+ if dir_path == service.path_to(directory.key)
58
+ break
59
+ end
60
+ pwd = Dir.pwd
61
+ if ::File.exist?(dir_path) && ::File.directory?(dir_path)
62
+ Dir.chdir(dir_path)
63
+ if Dir.glob('*').empty?
64
+ Dir.rmdir(dir_path)
65
+ end
66
+ Dir.chdir(pwd)
67
+ end
68
+ end
69
+ true
70
+ end
71
+
72
+ def public=(new_public)
73
+ new_public
74
+ end
75
+
76
+ def public_url
77
+ requires :directory, :key
78
+
79
+ if service.endpoint
80
+ escaped_directory = URI.escape(directory.key)
81
+ escaped_key = URI.escape(key)
82
+
83
+ ::File.join(service.endpoint, escaped_directory, escaped_key)
84
+ else
85
+ nil
86
+ end
87
+ end
88
+
89
+ def save(options = {})
90
+ requires :body, :directory, :key
91
+ dirs = path.split(::File::SEPARATOR)[0...-1]
92
+ dirs.length.times do |index|
93
+ dir_path = dirs[0..index].join(::File::SEPARATOR)
94
+ if dir_path.empty? # path starts with ::File::SEPARATOR
95
+ next
96
+ end
97
+ # create directory if it doesn't already exist
98
+ begin
99
+ Dir.mkdir(dir_path)
100
+ rescue Errno::EEXIST
101
+ raise unless ::File.directory?(dir_path)
102
+ end
103
+ end
104
+ file = ::File.new(path, 'wb')
105
+ if body.is_a?(String)
106
+ file.write(body)
107
+ elsif body.kind_of? ::File and ::File.exist?(body.path)
108
+ FileUtils.cp(body.path, path)
109
+ else
110
+ file.write(body.read)
111
+ end
112
+ file.close
113
+ merge_attributes(
114
+ :content_length => Fog::Storage.get_body_size(body),
115
+ :last_modified => ::File.mtime(path)
116
+ )
117
+ true
118
+ end
119
+
120
+ private
121
+
122
+ def directory=(new_directory)
123
+ @directory = new_directory
124
+ end
125
+
126
+ def path
127
+ service.path_to(::File.join(directory.key, key))
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end