c-azure 0.0.3

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: f5f3a029ba511e0ee4984b9d9d24f24249e652ee
4
+ data.tar.gz: ee4471d579ba93781804abd7c49f898332c8e973
5
+ SHA512:
6
+ metadata.gz: 5832739850d5ebc72e220ba5a6d63317cf436dfc44a5d7a72213a161d5995ba4a50ec1a50240e89e9afa57b85242bbc21d0797bdc2665dea19c1d094df20e91a
7
+ data.tar.gz: f2a66ba2f5de4555ac218b54bdc89647b1fb91562874f584c04bed7bee9cb31224ca1e3a58bbaaf19630139617cee4f8d976a02fd18939ac1e571bcdb2d8277b
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.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 heathrow, inc.
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,53 @@
1
+ # Carrierwave::Azure
2
+
3
+ Windows Azure blob storage support for [CarrierWave](https://github.com/carrierwaveuploader/carrierwave)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'carrierwave-azure'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ ## Usage
16
+
17
+ First configure CarrierWave with your Azure storage credentials
18
+
19
+ see [Azure/azure-sdk-for-ruby](https://github.com/Azure/azure-sdk-for-ruby#via-code)
20
+
21
+ ```ruby
22
+ CarrierWave.configure do |config|
23
+ config.azure_storage_account_name = 'YOUR STORAGE ACCOUNT NAME'
24
+ config.azure_storage_access_key = 'YOUR STORAGE ACCESS KEY'
25
+ config.azure_storage_blob_host = 'YOUR STORAGE BLOB HOST' # optional
26
+ config.azure_container = 'YOUR CONTAINER NAME'
27
+ config.asset_host = 'YOUR CDN HOST' # optional
28
+ end
29
+ ```
30
+
31
+ And then in your uploader, set the storage to `:azure`
32
+
33
+ ```ruby
34
+ class ExampleUploader < CarrierWave::Uploader::Base
35
+ storage :azure
36
+ end
37
+ ```
38
+
39
+ ## Contributing
40
+
41
+ In order to run the integration specs you will need to configure some environment variables.
42
+ A sample file is provided as `spec/environment.rb.sample`.
43
+ Copy it over and plug in the appropriate values.
44
+
45
+ ```bash
46
+ cp spec/environment.rb.sample spec/environment.rb
47
+ ```
48
+
49
+ 1. Fork it
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/c-azure.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'c/azure/blob/version'
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = 'c-azure'
7
+ gem.version = C::Azure::Blob::VERSION
8
+ gem.authors = ['Thuong Nguyen']
9
+ gem.email = ['thuongnh.uit@gmail.com']
10
+ gem.summary = %q{Microsoft Azure blob storage support for CarrierWave}
11
+ gem.description = %q{Allows file upload to Azure with the official sdk}
12
+ gem.homepage = 'https://github.com/zelic91/carrierwave-azure'
13
+ gem.license = 'MIT'
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.test_files = gem.files.grep(%r{^rspec})
17
+ gem.require_paths = ['lib']
18
+
19
+ gem.add_dependency 'carrierwave'
20
+ gem.add_dependency 'azure-storage'
21
+
22
+ gem.add_development_dependency 'rake'
23
+ gem.add_development_dependency 'rspec', '~> 3'
24
+ end
Binary file
@@ -0,0 +1,7 @@
1
+ module C
2
+ module Azure
3
+ module Blob
4
+ VERSION = '0.0.3'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,110 @@
1
+ require 'azure/storage'
2
+
3
+ module C
4
+ module Azure
5
+ module Storage
6
+ class Azure < CarrierWave::Storage::Abstract
7
+ def store!(file)
8
+ azure_file = C::Azure::Storage::Azure::File.new(uploader, connection, uploader.store_path)
9
+ azure_file.store!(file)
10
+ azure_file
11
+ end
12
+
13
+ def retrieve!(identifer)
14
+ C::Azure::Storage::Azure::File.new(uploader, connection, uploader.store_path(identifer))
15
+ end
16
+
17
+ def connection
18
+ @connection ||= begin
19
+ client = ::Azure::Storage::Client.create(storage_account_name: uploader.azure_storage_account_name,
20
+ storage_access_key: uploader.azure_storage_access_key)
21
+ client.blob_client
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 store!(file)
35
+ @content = file.read
36
+ @content_type = file.content_type
37
+ @connection.create_block_blob @uploader.azure_container, @path, @content, content_type: @content_type
38
+ true
39
+ end
40
+
41
+ def url(options = {})
42
+ path = ::File.join @uploader.azure_container, @path
43
+ if @uploader.asset_host
44
+ "#{@uploader.asset_host}/#{path}"
45
+ else
46
+ @connection.generate_uri(path).to_s
47
+ end
48
+ end
49
+
50
+ def read
51
+ content
52
+ end
53
+
54
+ def content_type
55
+ @content_type = blob.properties[:content_type] if @content_type.nil? && !blob.nil?
56
+ @content_type
57
+ end
58
+
59
+ def content_type=(new_content_type)
60
+ @content_type = new_content_type
61
+ end
62
+
63
+ def exists?
64
+ blob.nil?
65
+ end
66
+
67
+ def size
68
+ blob.properties[:content_length] unless blob.nil?
69
+ end
70
+
71
+ def filename
72
+ URI.decode(url).gsub(/.*\/(.*?$)/, '\1')
73
+ end
74
+
75
+ def extension
76
+ @path.split('.').last
77
+ end
78
+
79
+ def delete
80
+ begin
81
+ @connection.delete_blob @uploader.azure_container, @path
82
+ true
83
+ rescue ::Azure::Storage::Core::StorageError
84
+ false
85
+ end
86
+ end
87
+
88
+ private
89
+
90
+ def blob
91
+ load_content if @blob.nil?
92
+ @blob
93
+ end
94
+
95
+ def content
96
+ load_content if @content.nil?
97
+ @content
98
+ end
99
+
100
+ def load_content
101
+ @blob, @content = begin
102
+ @connection.get_blob @uploader.azure_container, @path
103
+ rescue ::Azure::Storage::Core::StorageError
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
data/lib/c-azure.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'carrierwave'
2
+ require 'c/azure/blob/version'
3
+ require 'c/azure/storage/azure'
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] = 'C::Azure::Storage::Azure'
13
+ end
14
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe C::Azure::Storage::Azure::File do
4
+ class TestUploader < CarrierWave::Uploader::Base
5
+ storage :azure
6
+ end
7
+
8
+ let(:uploader) { TestUploader.new }
9
+ let(:storage) { C::Azure::Storage::Azure.new uploader }
10
+
11
+ describe '#url' do
12
+ before do
13
+ allow(uploader).to receive(:azure_container).and_return('test')
14
+ allow(uploader).to receive(:azure_account_name).and_return('360uni')
15
+ end
16
+
17
+ subject { C::Azure::Storage::Azure::File.new(uploader, storage.connection, 'dummy.txt').url }
18
+
19
+ context 'with storage_blob_host' do
20
+ before do
21
+ allow(uploader).to receive(:azure_storage_blob_host).and_return("https://#{uploader.azure_account_name}.blob.core.windows.net")
22
+ end
23
+
24
+ it 'should return on asset_host' do
25
+ expect(subject).to eq "#{uploader.azure_storage_blob_host}/test/dummy.txt"
26
+ end
27
+ end
28
+
29
+ context 'with asset_host' do
30
+ before do
31
+ allow(uploader).to receive(:asset_host).and_return("https://#{uploader.azure_account_name}.blob.core.windows.net")
32
+ end
33
+
34
+ it 'should return on asset_host' do
35
+ expect(subject).to eq "#{uploader.asset_host}/test/dummy.txt"
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+ require 'tempfile'
3
+ require 'open-uri'
4
+
5
+ describe C::Azure::Storage::Azure do
6
+ class TestUploader < CarrierWave::Uploader::Base
7
+ storage :azure
8
+ end
9
+
10
+ let(:uploader) { TestUploader.new }
11
+ let(:storage) { C::Azure::Storage::Azure.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]).to eq 'C::Azure::Storage::Azure'
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 'c-azure'
5
+ require 'environment'
6
+
7
+ RSpec.configure do |config|
8
+ config.order = :random
9
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: c-azure
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Thuong Nguyen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-18 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'
34
+ type: :runtime
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: 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 official sdk
70
+ email:
71
+ - thuongnh.uit@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - c-azure.gemspec
83
+ - lib/c-azure.rb
84
+ - lib/c/azure/.version.rb.swp
85
+ - lib/c/azure/blob/version.rb
86
+ - lib/c/azure/storage/azure.rb
87
+ - spec/carrierwave-azure_spec.rb
88
+ - spec/carrierwave/azure/storage/azure_file_spec.rb
89
+ - spec/carrierwave/azure/storage/azure_spec.rb
90
+ - spec/environment.rb.sample
91
+ - spec/spec_helper.rb
92
+ homepage: https://github.com/zelic91/carrierwave-azure
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.5.1
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Microsoft Azure blob storage support for CarrierWave
116
+ test_files: []