standardstorage 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bb76c120a5013dafd57e21190550e6c15238269695da5fccb006e757b59f45c1
4
+ data.tar.gz: 3949addf2bab883046708aa67f3cb036892d33f7630b935d19c52e0fc1446276
5
+ SHA512:
6
+ metadata.gz: c64611460f888d3b6f63c76a512c089acc4761fd7ce06b5386002782903eaa33661f7dfea722069b3554d5b20a88d1936716a6cea8620818166d8ce7faf15943
7
+ data.tar.gz: f4c00c85b03ceae6ba809dd04c144b45519265cd58f2d8070030a27575686d2560d891c94e8d7fae460135078428e95c98602db9c30c06f9adbed76dc1821b75
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Jon Bracy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,37 @@
1
+ class StandardStorage
2
+
3
+ attr_reader :partition, :partition_depth
4
+
5
+ def initialize(configs={})
6
+ @partition = configs.has_key?(:partition) || configs.has_key?(:partition_depth)
7
+ @partition_depth = if configs[:partition].is_a?(Integer)
8
+ configs[:partition]
9
+ else
10
+ configs[:partition_depth] || 3
11
+ end
12
+ end
13
+
14
+ def copy_to_tempfile(key)
15
+ tmpfile = Tempfile.new([File.basename(key), File.extname(key)], binmode: true)
16
+ cp(key, tmpfile.path)
17
+ if block_given?
18
+ begin
19
+ yield(tmpfile)
20
+ ensure
21
+ tmpfile.close!
22
+ end
23
+ else
24
+ tmpfile
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def partition(value)
31
+ return value unless @partition
32
+
33
+ split = value.scan(/.{1,4}/)
34
+ split.shift(@partition_depth).join("/") + split.join("")
35
+ end
36
+
37
+ end
@@ -0,0 +1,81 @@
1
+ require 'b2'
2
+ require File.expand_path('../../standard_storage', __FILE__)
3
+
4
+ class StandardStorage::B2 < StandardStorage
5
+
6
+ attr_reader :account_id, :bucket, :prefix
7
+
8
+ def initialize(configs={})
9
+ super
10
+ @bucket = configs[:bucket]
11
+ @prefix = configs[:prefix]
12
+ @account_id = configs[:account_id]
13
+
14
+ @client = ::B2.new({
15
+ account_id: @account_id,
16
+ application_key: configs[:application_key]
17
+ }).buckets.find { |b| b.name == @bucket }
18
+ end
19
+
20
+ def local?
21
+ false
22
+ end
23
+
24
+ def url(key, **options)
25
+ @client.get_download_url(destination(key), **options)
26
+ end
27
+
28
+ def destination(key)
29
+ [@prefix, partition(key)].compact.join('/').gsub(/^\//, '')
30
+ end
31
+
32
+ def exists?(key)
33
+ @client.has_key?(destination(key))
34
+ end
35
+
36
+ def write(key, file, meta_info={})
37
+ file = file.tempfile if file.class.name == "ActionDispatch::Http::UploadedFile"
38
+ file = File.open(file) if file.is_a?(String)
39
+
40
+ @client.upload_file(destination(key), file, {
41
+ mime_type: meta_info[:content_type],
42
+ sha1: meta_info[:sha1],
43
+ content_disposition: meta_info[:filename] ? "inline; filename=\"#{meta_info[:filename]}\"" : nil
44
+ })
45
+ end
46
+
47
+ def cp(key, to)
48
+ @client.download(destination(key), to)
49
+ rescue ::B2::NotFound => e
50
+ raise Errno::ENOENT.new(e.message)
51
+ end
52
+
53
+ def read(key, &block)
54
+ @client.download(destination(key), &block)
55
+ rescue ::B2::NotFound => e
56
+ raise Errno::ENOENT.new(e.message)
57
+ end
58
+
59
+ def delete(key)
60
+ @client.delete!(destination(key))
61
+ end
62
+
63
+ def sha1(key)
64
+ @client.file(destination(key)).sha1
65
+ rescue ::B2::NotFound => e
66
+ raise Errno::ENOENT.new(e.message)
67
+ end
68
+
69
+ def last_modified(key)
70
+ @client.file(destination(key)).uploaded_at
71
+ rescue ::B2::NotFound => e
72
+ raise Errno::ENOENT.new(e.message)
73
+ end
74
+
75
+ def mime_type(key)
76
+ @client.file(destination(key)).mime_type
77
+ rescue ::B2::NotFound => e
78
+ raise Errno::ENOENT.new(e.message)
79
+ end
80
+
81
+ end
@@ -0,0 +1,63 @@
1
+ require 'fileutils'
2
+ require File.expand_path('../../standard_storage', __FILE__)
3
+
4
+ class StandardStorage::Filesystem < StandardStorage
5
+
6
+ attr_reader :host, :path, :prefix
7
+
8
+ def initialize(configs={})
9
+ super
10
+ @host = configs[:host]
11
+ @path = configs[:path]
12
+ @prefix = configs[:prefix]
13
+ end
14
+
15
+ def local?
16
+ true
17
+ end
18
+
19
+ def url(key)
20
+ File.join([@host, path(key)].compact)
21
+ end
22
+
23
+ def path(key)
24
+ File.join(['/', @prefix, partition(key)].compact)
25
+ end
26
+
27
+ def destination(key)
28
+ File.join([@path, partition(key)].compact)
29
+ end
30
+
31
+ def exists?(key)
32
+ File.exist?(destination(key))
33
+ end
34
+
35
+ def write(key, file, options={})
36
+ key = destination(key)
37
+ FileUtils.mkdir_p(File.dirname(key))
38
+ FileUtils.cp(file.path, key)
39
+ end
40
+
41
+ def cp(source, destination)
42
+ source = destination(source)
43
+ FileUtils.cp(source, destination)
44
+ end
45
+
46
+ def read(key, &block)
47
+ File.read(destination(key), &block)
48
+ end
49
+
50
+ def delete(key)
51
+ FileUtils.rm(destination(key), force: true)
52
+ end
53
+
54
+ def last_modified(key)
55
+ File.mtime(destination(key))
56
+ end
57
+
58
+ def mime_type(key)
59
+ command = Terrapin::CommandLine.new("file", '-b --mime-type :file')
60
+ command.run({ file: destination(key) }).strip
61
+ end
62
+
63
+ end
@@ -0,0 +1,104 @@
1
+ require 'aws-sdk-s3'
2
+ require File.expand_path('../../standard_storage', __FILE__)
3
+
4
+ class StandardStorage::S3 < StandardStorage
5
+
6
+ attr_reader :region, :bucket, :bucket_host_alias, :prefix
7
+
8
+ # accepts private, public-read, public-read-write,
9
+ # authenticated-read, aws-exec-read, bucket-owner-read,
10
+ # bucket-owner-full-control
11
+ attr_reader :acl
12
+
13
+ # accepts STANDARD, REDUCED_REDUNDANCY, STANDARD_IA, ONEZONE_IA
14
+ attr_reader :storage_class
15
+
16
+ def initialize(configs={})
17
+ super
18
+ @region = configs[:region] || 'us-east-1'
19
+ @bucket = configs[:bucket]
20
+ @virtual_host = configs[:virtual_host] || false
21
+ @prefix = configs[:prefix]
22
+ @storage_class = configs[:storage_class] || 'STANDARD'
23
+ @acl = configs[:acl] || 'private'
24
+
25
+ @client = Aws::S3::Client.new({
26
+ access_key_id: configs[:access_key_id],
27
+ secret_access_key: configs[:secret_access_key],
28
+ region: @region
29
+ })
30
+ end
31
+
32
+ def object_for(key)
33
+ Aws::S3::Object.new(@bucket, key, client: @client)
34
+ end
35
+
36
+ def local?
37
+ false
38
+ end
39
+
40
+ def host
41
+ @bucket_host_alias || "#{@bucket}.s3.amazonaws.com"
42
+ end
43
+
44
+ def url(key, expires_in: nil)
45
+ object_for(destination(key)).presigned_url(:get, {
46
+ virtual_host: @virtual_host,
47
+ expires_in: (expires_in || 900)
48
+ })
49
+ end
50
+
51
+ def destination(key)
52
+ File.join([@prefix, partition(key)].compact).gsub(/^\//, '')
53
+ end
54
+
55
+ def exists?(key)
56
+ object_for(destination(key)).exists?
57
+ end
58
+
59
+ def write(key, file, meta_info={})
60
+ # If we pass a File type object to upload_file we need to make sure
61
+ # it is rewound, otherwise it will upload part of the file then timeout
62
+ # (30+ seconds), then retry. For now we'll just pass the path
63
+ object_for(destination(key)).upload_file(file.respond_to?(:path) ? file.path : file, {
64
+ content_type: meta_info[:content_type],
65
+ content_disposition: meta_info[:filename] ? "inline; filename=\"#{meta_info[:filename]}\"" : nil,
66
+ content_md5: meta_info[:md5],
67
+ storage_class: @storage_class,
68
+ acl: @acl
69
+ })
70
+ end
71
+
72
+ def cp(key, to)
73
+ object_for(destination(key)).download_file(to)
74
+ rescue Aws::S3::Errors::NotFound => e
75
+ raise Errno::ENOENT.new(e.message)
76
+ end
77
+
78
+ def read(key, &block)
79
+ if block
80
+ object_for(destination(key)).get({}, &block)
81
+ else
82
+ object_for(destination(key)).get.body
83
+ end
84
+ rescue Aws::S3::Errors::NotFound => e
85
+ raise Errno::ENOENT.new(e.message)
86
+ end
87
+
88
+ def delete(key)
89
+ object_for(destination(key)).delete
90
+ end
91
+
92
+ def last_modified(key)
93
+ object_for(destination(key)).last_modified
94
+ rescue Aws::S3::Errors::NotFound => e
95
+ raise Errno::ENOENT.new(e.message)
96
+ end
97
+
98
+ def mime_type(key)
99
+ object_for(destination(key)).content_type
100
+ rescue Aws::S3::Errors::NotFound => e
101
+ raise Errno::ENOENT.new(e.message)
102
+ end
103
+
104
+ end
@@ -0,0 +1,3 @@
1
+ class StandardStorage
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,26 @@
1
+ require File.expand_path("../lib/standard_storage/version", __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "standardstorage"
5
+ s.version = StandardStorage::VERSION
6
+ s.authors = ["Jon Bracy"]
7
+ s.email = ["jonbracy@gmail.com"]
8
+ s.homepage = "https://github.com/malomalo/storage"
9
+ s.summary = %q{API for multiple storage backends}
10
+ s.description = %q{A simple API for multiple storage backends like B2, S3, and the FileSystem}
11
+
12
+ s.files = `git ls-files`.split("\n")
13
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
15
+ s.require_paths = ["lib"]
16
+
17
+ # Developoment
18
+ s.add_development_dependency 'rake'
19
+ s.add_development_dependency 'bundler'
20
+ s.add_development_dependency 'minitest'
21
+ s.add_development_dependency 'minitest-reporters'
22
+ s.add_development_dependency 'mocha'
23
+
24
+ # Runtime
25
+ s.add_runtime_dependency 'terrapin'
26
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: standardstorage
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jon Bracy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-01-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-reporters
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mocha
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: terrapin
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: A simple API for multiple storage backends like B2, S3, and the FileSystem
98
+ email:
99
+ - jonbracy@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - LICENSE
105
+ - lib/standard_storage.rb
106
+ - lib/standard_storage/b2.rb
107
+ - lib/standard_storage/filesystem.rb
108
+ - lib/standard_storage/s3.rb
109
+ - lib/standard_storage/version.rb
110
+ - standardstorage.gemspec
111
+ homepage: https://github.com/malomalo/storage
112
+ licenses: []
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.7.4
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: API for multiple storage backends
134
+ test_files: []