refile-azure 0.1.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
+ SHA1:
3
+ metadata.gz: 39fdbac6236eeba929fa6ea84e01633012a277f8
4
+ data.tar.gz: 6708a71cc817de06cc480d7ab3e805fa30f09900
5
+ SHA512:
6
+ metadata.gz: a9cd033800ef0a6e05aba7bdfb5d78c1a112488a6f13e8cf838c8b777a53a048f6207d267489bcc23bebe8dae449f35bb9dcf9059b05b5d1593bc4a57fc1133f
7
+ data.tar.gz: 827534b076ddcdc0abd4db4627fad6ff8234ef88c4576eb80fc9c0a3a7d09373b98f8eec331b1b13ee0855b44bc613cf71061e5be7b78b6f4a7b44fda4a22422
data/.gitignore ADDED
@@ -0,0 +1,16 @@
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
+ .byebug_history
15
+ mkmf.log
16
+ azure.yml
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in refile-s3.gemspec
4
+ gemspec
5
+
6
+ gem "refile", github: "refile/refile"
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Jonas Nicklas
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,10 @@
1
+ # Refile::Azure
2
+
3
+ Microsoft Azure backend for the [Refile][gh] gem. See the Refile documentation for
4
+ more details.
5
+
6
+ ## License
7
+
8
+ [MIT](LICENSE.txt)
9
+
10
+ [gh]: http://github.com/refile/refile
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: "spec"
7
+
@@ -0,0 +1,165 @@
1
+ require "azure"
2
+ require "open-uri"
3
+ require "refile"
4
+ require "refile/backend_macros"
5
+ require "refile/azure/version"
6
+
7
+ module Refile
8
+
9
+ # @api private
10
+ class AzureBackendError < StandardError; end
11
+
12
+ # @api private
13
+ class AzureCredentialsError < AzureBackendError
14
+ def message
15
+ "Credentials not found"
16
+ end
17
+ end
18
+
19
+ # A refile backend which stores files in Microsoft Azure
20
+ #
21
+ # @example
22
+ # backend = Refile::Backend::Azure.new(
23
+ # storage_account_name: 'mystorage',
24
+ # storage_access_key: 'secret_key',
25
+ # container: "my-container",
26
+ # )
27
+ # file = backend.upload(StringIO.new("hello"))
28
+ # backend.read(file.id) # => "hello"
29
+ class Azure
30
+ extend Refile::BackendMacros
31
+
32
+ attr_reader :storage_account_name, :container, :max_size
33
+
34
+ # Sets up an Azure backend
35
+ #
36
+ # @param [String] container The name of the container where files will be stored
37
+ # @param [String] prefix A prefix to add to all files.
38
+ # @param [Integer, nil] max_size The maximum size of an uploaded file
39
+ # @param [#hash] hasher A hasher which is used to generate ids from files
40
+ # @param [Hash] azure_options Additional options to initialize Azure Client with
41
+ # @see https://github.com/Azure/azure-sdk-for-ruby
42
+ def initialize(storage_account_name:, storage_access_key:, container:, max_size: nil, hasher: Refile::RandomHasher.new, **azure_options)
43
+ @azure_options = azure_options
44
+ @azure = ::Azure.client(
45
+ azure_options.merge(
46
+ storage_account_name: storage_account_name,
47
+ storage_access_key: storage_access_key,
48
+ storage_blob_host: "https://#{storage_account_name}.blob.core.windows.net"
49
+ )
50
+ )
51
+ @storage_account_name = storage_account_name
52
+ @container = container
53
+ @blobs = @azure.blobs
54
+ @hasher = hasher
55
+ @max_size = max_size
56
+ end
57
+
58
+ # Upload a file into this backend
59
+ #
60
+ # @param [IO] uploadable An uploadable IO-like object.
61
+ # @return [Refile::File] The uploaded file
62
+ verify_uploadable def upload(uploadable)
63
+ id = @hasher.hash(uploadable)
64
+
65
+ if uploadable.is_a?(Refile::File) and uploadable.backend.is_a?(Azure) and uploadable.backend.storage_account_name == storage_account_name
66
+ @blobs.copy_blob(@container, id, uploadable.backend.container, uploadable.id)
67
+ else
68
+ body = if IO === uploadable
69
+ uploadable
70
+ elsif uploadable.respond_to?(:read)
71
+ uploadable.read
72
+ else
73
+ uploadable
74
+ end
75
+ @blobs.create_block_blob(@container, id, body)
76
+ end
77
+
78
+ Refile::File.new(self, id)
79
+ end
80
+
81
+ # Get a file from this backend.
82
+ #
83
+ # Note that this method will always return a {Refile::File} object, even
84
+ # if a file with the given id does not exist in this backend. Use
85
+ # {FileSystem#exists?} to check if the file actually exists.
86
+ #
87
+ # @param [String] id The id of the file
88
+ # @return [Refile::File] The retrieved file
89
+ verify_id def get(id)
90
+ Refile::File.new(self, id)
91
+ end
92
+
93
+ # Delete a file from this backend
94
+ #
95
+ # @param [String] id The id of the file
96
+ # @return [void]
97
+ verify_id def delete(id)
98
+ @blobs.delete_blob(@container, id)
99
+ rescue ::Azure::Core::Http::HTTPError => exc
100
+ raise exc unless exc.status_code == 404
101
+ nil
102
+ end
103
+
104
+ # Return an IO object for the uploaded file which can be used to read its
105
+ # content.
106
+ #
107
+ # @param [String] id The id of the file
108
+ # @return [IO] An IO object containing the file contents
109
+ verify_id def open(id)
110
+ StringIO.new(read(id))
111
+ end
112
+
113
+ # Return the entire contents of the uploaded file as a String.
114
+ #
115
+ # @param [String] id The id of the file
116
+ # @return [String] The file's contents
117
+ verify_id def read(id)
118
+ blob, body = @blobs.get_blob(@container, id)
119
+ body
120
+ rescue ::Azure::Core::Http::HTTPError => exc
121
+ raise exc unless exc.status_code == 404
122
+ nil
123
+ end
124
+
125
+ # Return the size in bytes of the uploaded file.
126
+ #
127
+ # @param [String] id The id of the file
128
+ # @return [Integer] The file's size
129
+ verify_id def size(id)
130
+ @blobs.get_blob_properties(@container, id).properties[:content_length]
131
+ rescue ::Azure::Core::Http::HTTPError => exc
132
+ raise exc unless exc.status_code == 404
133
+ nil
134
+ end
135
+
136
+ # Return whether the file with the given id exists in this backend.
137
+ #
138
+ # @param [String] id The id of the file
139
+ # @return [Boolean]
140
+ verify_id def exists?(id)
141
+ @blobs.get_blob_properties(@container, id)
142
+ true
143
+ rescue ::Azure::Core::Http::HTTPError => exc
144
+ raise exc unless exc.status_code == 404
145
+ false
146
+ end
147
+
148
+ # Remove all files in this backend. You must confirm the deletion by
149
+ # passing the symbol `:confirm` as an argument to this method.
150
+ #
151
+ # @example
152
+ # backend.clear!(:confirm)
153
+ # @raise [Refile::Confirm] Unless the `:confirm` symbol has been passed.
154
+ # @param [:confirm] confirm Pass the symbol `:confirm` to confirm deletion.
155
+ # @return [void]
156
+ def clear!(confirm = nil)
157
+ raise Refile::Confirm unless confirm == :confirm
158
+ @blobs.list_blobs(@container).each do |blob|
159
+ @blobs.delete_blob(@container, blob.name)
160
+ end
161
+
162
+ end
163
+
164
+ end
165
+ end
@@ -0,0 +1,5 @@
1
+ module Refile
2
+ class Azure
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'refile/azure/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "refile-azure"
8
+ spec.version = Refile::Azure::VERSION
9
+ spec.authors = ["Ivan Pirlik"]
10
+ spec.email = ["abmajor7@gmail.com"]
11
+ spec.summary = "Microsoft Azure backend for the Refile gem"
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.required_ruby_version = ">= 2.1.0"
21
+
22
+ spec.add_dependency "refile", "~> 0.6.0"
23
+ spec.add_dependency "azure", "~> 0.7.5"
24
+ spec.add_development_dependency "bundler", "~> 1.7"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "webmock"
28
+ end
@@ -0,0 +1,12 @@
1
+ require "refile/spec_helper"
2
+ require "refile/azure"
3
+
4
+ WebMock.allow_net_connect!
5
+
6
+ config = YAML.load_file("azure.yml").map { |k, v| [k.to_sym, v] }.to_h
7
+
8
+ RSpec.describe Refile::Azure do
9
+ let(:backend) { Refile::Azure.new(max_size: 100, **config) }
10
+
11
+ it_behaves_like :backend
12
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: refile-azure
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Pirlik
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: refile
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.6.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.6.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: azure
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.7.5
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.7.5
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - abmajor7@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - lib/refile/azure.rb
110
+ - lib/refile/azure/version.rb
111
+ - refile-azure.gemspec
112
+ - spec/refile/azure_spec.rb
113
+ homepage: ''
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: 2.1.0
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.5.1
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: Microsoft Azure backend for the Refile gem
137
+ test_files:
138
+ - spec/refile/azure_spec.rb