carrierwave-azure_rm 0.0.1

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: 0b52094ba16c32b092b3042ff9d3ca19ab29b91c
4
+ data.tar.gz: e310237d68824ff9f85fdc71bda05bec443c9aec
5
+ SHA512:
6
+ metadata.gz: b5e029cd3e36be31d74b648ecc579962e76683233e9f61a4732a0dc0b62317cf0efaa806be1e06ec7e86c9c9a003dd12afa3c8933319cdd374e071346c08abb9
7
+ data.tar.gz: 70dbff94503155508552f5517d9bc9382a54e3f3d48c1c91f4941b6bff739c49366c9ec9790760247f149cf7e647a912532d58a85e706c87bf8eed72c2d50bac
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ spec/environment.rb
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 nooulaif
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.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # Carrierwave::AzureRM
2
+
3
+ Microsoft Azure Storage blob support for [CarrierWave](https://github.com/carrierwaveuploader/carrierwave).
4
+
5
+ Heavily inspired by @unosk [carrierwave-azure](https://github.com/unosk/carrierwave-azure) gem, which seems to be no longer maintained.
6
+
7
+ Uses new [azure-storage](https://github.com/Azure/azure-storage-ruby) sdk to support [ARM](https://azure.microsoft.com/en-us/documentation/articles/resource-group-overview/).
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'carrierwave-azure_rm'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ ## Usage
20
+
21
+ First configure CarrierWave with your Azure storage credentials
22
+
23
+ see [Azure/azure-storage-ruby](https://github.com/Azure/azure-storage-ruby#via-code)
24
+
25
+ ```ruby
26
+ CarrierWave.configure do |config|
27
+ config.azure_storage_account_name = 'YOUR STORAGE ACCOUNT NAME'
28
+ config.azure_storage_access_key = 'YOUR STORAGE ACCESS KEY'
29
+ config.azure_storage_blob_host = 'YOUR STORAGE BLOB HOST' # optional
30
+ config.azure_container = 'YOUR CONTAINER NAME'
31
+ config.asset_host = 'YOUR CDN HOST' # optional
32
+ end
33
+ ```
34
+
35
+ And then in your uploader, set the storage to `:azure_rm`
36
+
37
+ ```ruby
38
+ class ExampleUploader < CarrierWave::Uploader::Base
39
+ storage :azure_rm
40
+ end
41
+ ```
42
+
43
+ ## Issues
44
+ If you have any problems with or questions about this image, please contact me through a [GitHub issue](https://github.com/nooulaif/carrierwave-azure_rm/issues).
45
+
46
+ ## Contributing
47
+ You are invited to contribute new features, fixes, or updates, large or small.
48
+
49
+ I'm always thrilled to receive pull requests, and do my best to process them as fast as I can.
50
+
51
+ In order to run the integration specs you will need to configure some environment variables.
52
+ A sample file is provided as `spec/environment.rb.sample`.
53
+ Copy it over and plug in the appropriate values.
54
+
55
+ ```bash
56
+ cp spec/environment.rb.sample spec/environment.rb
57
+ ```
58
+
59
+ 1. Fork it
60
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
61
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
62
+ 4. Push to the branch (`git push origin my-new-feature`)
63
+ 5. Create new Pull Request
64
+
65
+ ## License
66
+ Released under the MIT License. See [LICENSE](https://github.com/nooulaif/carrierwave-azure_rm/blob/master/LICENSE) file for details.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,24 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'carrierwave/azure_rm/version'
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = 'carrierwave-azure_rm'
7
+ gem.version = Carrierwave::AzureRM::VERSION
8
+ gem.authors = ['nooulaif']
9
+ gem.email = ['contact@nooulaif.com']
10
+ gem.summary = %q{Microsoft Azure Storage blob support for CarrierWave}
11
+ gem.description = %q{Allows file upload to Azure with the new official sdk}
12
+ gem.homepage = 'https://github.com/nooulaif/carrierwave-azure_rm'
13
+ gem.license = 'MIT'
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.test_files = gem.files.grep(%r{^spec})
17
+ gem.require_paths = ['lib']
18
+
19
+ gem.add_dependency 'carrierwave'
20
+ gem.add_dependency 'azure-storage', '~> 0.10.2.preview'
21
+
22
+ gem.add_development_dependency 'rake'
23
+ gem.add_development_dependency 'rspec', '~> 3'
24
+ end
@@ -0,0 +1,5 @@
1
+ module Carrierwave
2
+ module AzureRM
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,128 @@
1
+ require 'azure/storage'
2
+
3
+ module CarrierWave
4
+ module Storage
5
+ class AzureRM < Abstract
6
+ def store!(file)
7
+ azure_file = CarrierWave::Storage::AzureRM::File.new(uploader, connection, uploader.store_path)
8
+ azure_file.store!(file)
9
+ azure_file
10
+ end
11
+
12
+ def retrieve!(identifer)
13
+ CarrierWave::Storage::AzureRM::File.new(uploader, connection, uploader.store_path(identifer))
14
+ end
15
+
16
+ def connection
17
+ @connection ||= begin
18
+ %i(storage_account_name storage_access_key storage_blob_host).each do |key|
19
+ ::Azure::Storage.send("#{key}=", uploader.send("azure_#{key}"))
20
+ end
21
+ Azure::Storage::Blob::BlobService.new
22
+ end
23
+ end
24
+
25
+ class File
26
+ attr_reader :path
27
+
28
+ def initialize(uploader, connection, path)
29
+ @uploader = uploader
30
+ @connection = connection
31
+ @path = path
32
+ end
33
+
34
+ def ensure_container_exists(name)
35
+ unless @connection.list_containers.any? { |c| c.name == name }
36
+ @connection.create_container(name, public_access_level: 'blob')
37
+ end
38
+ end
39
+
40
+ def store!(file)
41
+ ensure_container_exists(@uploader.send("azure_container"))
42
+ @content_type = file.content_type
43
+ file_to_send = ::File.open(file.file, 'rb')
44
+ blocks = []
45
+
46
+ until file_to_send.eof?
47
+ block_id = Base64.urlsafe_encode64(SecureRandom.uuid)
48
+
49
+ @content = file_to_send.read 4194304 # Send 4MB chunk
50
+ @connection.put_blob_block @uploader.azure_container, @path, block_id, @content
51
+ blocks << [block_id]
52
+ end
53
+
54
+ # Commit block blobs
55
+ @connection.commit_blob_blocks @uploader.azure_container, @path, blocks, content_type: @content_type
56
+
57
+ true
58
+ end
59
+
60
+ def url(options = {})
61
+ path = ::File.join @uploader.azure_container, @path
62
+ if @uploader.asset_host
63
+ "#{@uploader.asset_host}/#{path}"
64
+ else
65
+ @connection.generate_uri(path).to_s
66
+ end
67
+ end
68
+
69
+ def read
70
+ content
71
+ end
72
+
73
+ def content_type
74
+ @content_type = blob.properties[:content_type] if @content_type.nil? && !blob.nil?
75
+ @content_type
76
+ end
77
+
78
+ def content_type=(new_content_type)
79
+ @content_type = new_content_type
80
+ end
81
+
82
+ def exists?
83
+ blob.nil?
84
+ end
85
+
86
+ def size
87
+ blob.properties[:content_length] unless blob.nil?
88
+ end
89
+
90
+ def filename
91
+ URI.decode(url).gsub(/.*\/(.*?$)/, '\1')
92
+ end
93
+
94
+ def extension
95
+ @path.split('.').last
96
+ end
97
+
98
+ def delete
99
+ begin
100
+ @connection.delete_blob @uploader.azure_container, @path
101
+ true
102
+ rescue ::Azure::Core::Http::HTTPError
103
+ false
104
+ end
105
+ end
106
+
107
+ private
108
+
109
+ def blob
110
+ load_content if @blob.nil?
111
+ @blob
112
+ end
113
+
114
+ def content
115
+ load_content if @content.nil?
116
+ @content
117
+ end
118
+
119
+ def load_content
120
+ @blob, @content = begin
121
+ @connection.get_blob @uploader.azure_container, @path
122
+ rescue ::Azure::Core::Http::HTTPError
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,14 @@
1
+ require 'carrierwave'
2
+ require 'carrierwave/azure_rm/version'
3
+ require 'carrierwave/storage/azure_rm'
4
+
5
+ class CarrierWave::Uploader::Base
6
+ add_config :azure_storage_account_name
7
+ add_config :azure_storage_access_key
8
+ add_config :azure_storage_blob_host
9
+ add_config :azure_container
10
+
11
+ configure do |config|
12
+ config.storage_engines[:azure_rm] = 'CarrierWave::Storage::AzureRM'
13
+ end
14
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe CarrierWave::Storage::AzureRM::File do
4
+ class TestUploader < CarrierWave::Uploader::Base
5
+ storage :azure_rm
6
+ end
7
+
8
+ let(:uploader) { TestUploader.new }
9
+ let(:storage) { CarrierWave::Storage::AzureRM.new uploader }
10
+
11
+ describe '#url' do
12
+ before do
13
+ allow(uploader).to receive(:azure_container).and_return('test')
14
+ end
15
+
16
+ subject { CarrierWave::Storage::AzureRM::File.new(uploader, storage.connection, 'dummy.txt').url }
17
+
18
+ context 'with storage_blob_host' do
19
+ before do
20
+ allow(uploader).to receive(:azure_storage_blob_host).and_return('http://example.com')
21
+ end
22
+
23
+ it 'should return on asset_host' do
24
+ expect(subject).to eq 'http://example.com/test/dummy.txt'
25
+ end
26
+ end
27
+
28
+ context 'with asset_host' do
29
+ before do
30
+ allow(uploader).to receive(:asset_host).and_return('http://example.com')
31
+ end
32
+
33
+ it 'should return on asset_host' do
34
+ expect(subject).to eq 'http://example.com/test/dummy.txt'
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+ require 'tempfile'
3
+ require 'open-uri'
4
+
5
+ describe CarrierWave::Storage::AzureRM do
6
+ class TestUploader < CarrierWave::Uploader::Base
7
+ storage :azure_rm
8
+ end
9
+
10
+ let(:uploader) { TestUploader.new }
11
+ let(:storage) { CarrierWave::Storage::AzureRM.new uploader }
12
+
13
+ shared_examples_for 'an expected return value' do
14
+ let(:stored_file) do
15
+ allow(uploader).to receive(:store_path).and_return('test/dummy.png')
16
+ tempfile = Tempfile.new 'test.jpg'
17
+ open(tempfile.path, 'w') do |f|
18
+ f.print '1234567890'
19
+ end
20
+ storage.store! CarrierWave::SanitizedFile.new(
21
+ tempfile: tempfile,
22
+ filename: 'test.jpg',
23
+ content_type: 'image/png'
24
+ )
25
+ end
26
+
27
+ let(:retrieved_file) do
28
+ storage.retrieve! stored_file.path
29
+ end
30
+
31
+ it 'should have a path' do
32
+ expect(subject.path).to eq 'test/dummy.png'
33
+ end
34
+
35
+ it 'should have a url' do
36
+ url = subject.url
37
+ expect(url).to match /^https?:\/\//
38
+ expect(open(url).read).to eq '1234567890'
39
+ end
40
+
41
+ it 'should have a content' do
42
+ expect(subject.read).to eq '1234567890'
43
+ end
44
+
45
+ it 'should have a content_type' do
46
+ expect(subject.content_type).to eq 'image/png'
47
+ end
48
+
49
+ it 'should have a size' do
50
+ expect(subject.size).to eq 10
51
+ end
52
+
53
+ it 'should have a filename' do
54
+ expect(subject.filename).to eq 'dummy.png'
55
+ end
56
+
57
+ it 'should have an extension' do
58
+ expect(subject.extension).to eq 'png'
59
+ end
60
+
61
+ it 'should be deletable' do
62
+ subject.delete
63
+ expect{open subject.url}.to raise_error OpenURI::HTTPError
64
+ end
65
+ end
66
+
67
+ describe '#store!' do
68
+ it_should_behave_like 'an expected return value' do
69
+ subject { stored_file }
70
+ end
71
+ end
72
+
73
+ describe '#retrieve' do
74
+ it_should_behave_like 'an expected return value' do
75
+ subject { retrieved_file }
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe CarrierWave::Uploader::Base do
4
+ it 'should define azure as a storage engine' do
5
+ expect(described_class.storage_engines[:azure_rm]).to eq 'CarrierWave::Storage::AzureRM'
6
+ end
7
+
8
+ it 'should define azure options' do
9
+ is_expected.to respond_to(:azure_storage_account_name)
10
+ is_expected.to respond_to(:azure_storage_access_key)
11
+ is_expected.to respond_to(:azure_storage_blob_host)
12
+ is_expected.to respond_to(:azure_container)
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ CarrierWave.configure do |config|
2
+ config.azure_storage_account_name = 'YOUR_STORAGE_ACCOUNT_NAME'
3
+ config.azure_storage_access_key = 'YOUR_STORAGE_ACCESS_KEY'
4
+ config.azure_container = 'YOUR_CONTAINER_NAME'
5
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+ require 'carrierwave'
4
+ require 'carrierwave-azure_rm'
5
+ require 'environment'
6
+
7
+ RSpec.configure do |config|
8
+ config.order = :random
9
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-azure_rm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - nooulaif
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: carrierwave
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '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'
27
+ - !ruby/object:Gem::Dependency
28
+ name: azure-storage
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.10.2.preview
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.10.2.preview
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
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: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3'
69
+ description: Allows file upload to Azure with the new official sdk
70
+ email:
71
+ - contact@nooulaif.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - carrierwave-azure_rm.gemspec
83
+ - lib/carrierwave-azure_rm.rb
84
+ - lib/carrierwave/azure_rm/version.rb
85
+ - lib/carrierwave/storage/azure_rm.rb
86
+ - spec/carrierwave-azure_spec.rb
87
+ - spec/carrierwave/storage/azure_file_spec.rb
88
+ - spec/carrierwave/storage/azure_spec.rb
89
+ - spec/environment.rb.sample
90
+ - spec/spec_helper.rb
91
+ homepage: https://github.com/nooulaif/carrierwave-azure_rm
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.6.6
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Microsoft Azure Storage blob support for CarrierWave
115
+ test_files:
116
+ - spec/carrierwave-azure_spec.rb
117
+ - spec/carrierwave/storage/azure_file_spec.rb
118
+ - spec/carrierwave/storage/azure_spec.rb
119
+ - spec/environment.rb.sample
120
+ - spec/spec_helper.rb