carrierwave-qcloud 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 61f57f3a44b467c0b0d7c33535bc2529c415d999
4
+ data.tar.gz: d443608f6d032f619c8d5c8e667bbcf705402f87
5
+ SHA512:
6
+ metadata.gz: cba36fd5c311319a3abef639664c39444ee4e061b03163e2aaf1412b2c472f3cff5e50d8f4b090b26dafb04f96e7059309a875b2a48fff109356856253659cf1
7
+ data.tar.gz: 930895f85340d8ba3620936658b0cba252f819a506b305fdfa6c6f3b3f845719eb695a0c4043f97df36b01dee4d469b0f04443e8c644620310cb174b871ef3a3
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.4
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in carrierwave-qcloud.gemspec
4
+ gemspec
@@ -0,0 +1,62 @@
1
+ # Carrierwave::Qcloud
2
+
3
+ This gem adds storage support for [Qcloud COS](https://www.qcloud.com/doc/product/227/%E4%BA%A7%E5%93%81%E4%BB%8B%E7%BB%8D) to [CarrierWave](https://github.com/carrierwaveuploader/carrierwave)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'carrierwave-qcloud'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install carrierwave-qcloud
20
+
21
+ ## Usage
22
+
23
+ You'll need to configure the to use this in config/initializes/carrierwave.rb
24
+
25
+ ```ruby
26
+ CarrierWave.configure do |config|
27
+ #config.storage = :qcloud # set default storage
28
+ config.qcloud_app_id = 'xxxxxx'
29
+ config.qcloud_secret_id = 'xxxxxx'
30
+ config.qcloud_secret_key = 'xxxxxx'
31
+ config.qcloud_bucket = "bucketname"
32
+ end
33
+ ```
34
+
35
+ If dont want to use `qcloud` as default storage, just set the storage to `:qcloud` in the specified uploader:
36
+
37
+ ```ruby
38
+ class AvatarUploader < CarrierWave::Uploader::Base
39
+ storage :qcloud
40
+ end
41
+ ```
42
+
43
+ You can override configuration item in individual uploader like this:
44
+
45
+ ```ruby
46
+ class AvatarUploader < CarrierWave::Uploader::Base
47
+ storage :qcloud
48
+
49
+ self.qcloud_bucket = "avatars"
50
+ end
51
+ ```
52
+
53
+ ## Development
54
+
55
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
56
+
57
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rainchen/carrierwave-qcloud.
62
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "carrierwave/qcloud"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -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 'carrierwave/qcloud/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "carrierwave-qcloud"
8
+ spec.version = Carrierwave::Qcloud::VERSION
9
+ spec.authors = ["RainChen"]
10
+ spec.email = ["hirainchen@gmail.com"]
11
+
12
+ spec.summary = %q{Qcloud COS Storage support for CarrierWave}
13
+ spec.description = %q{Qcloud COS Storage support for CarrierWave}
14
+ spec.homepage = "https://github.com/rainchen/carrierwave-qcloud"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+
25
+ spec.add_dependency "carrierwave", ">= 0.10.0"
26
+ spec.add_dependency "qcloud_cos", ">= 0.4.2"
27
+ end
@@ -0,0 +1,17 @@
1
+ require "carrierwave/qcloud/version"
2
+ require "carrierwave/qcloud/configuration"
3
+ require 'carrierwave/storage/qcloud'
4
+
5
+ module Carrierwave
6
+ module Qcloud
7
+ def self.register_carrierwave_storage_engine
8
+ CarrierWave.configure do |config|
9
+ config.storage_engines.merge!({ qcloud: 'CarrierWave::Storage::Qcloud' })
10
+ end
11
+
12
+ CarrierWave::Uploader::Base.send(:include, CarrierWave::Qcloud::Configuration)
13
+ end
14
+ end
15
+ end
16
+
17
+ Carrierwave::Qcloud.register_carrierwave_storage_engine
@@ -0,0 +1,37 @@
1
+ module CarrierWave
2
+ module Qcloud
3
+ module Configuration
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ add_config :qcloud_app_id
8
+ add_config :qcloud_secret_id
9
+ add_config :qcloud_secret_key
10
+ add_config :qcloud_bucket
11
+ end
12
+ end
13
+
14
+ module ClassMethods
15
+ def add_config(name)
16
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
17
+ def self.#{name}(value=nil)
18
+ @#{name} = value if value
19
+ return @#{name} if self.object_id == #{self.object_id} || defined?(@#{name})
20
+ name = superclass.#{name}
21
+ return nil if name.nil? && !instance_variable_defined?("@#{name}")
22
+ @#{name} = name && !name.is_a?(Module) && !name.is_a?(Symbol) && !name.is_a?(Numeric) && !name.is_a?(TrueClass) && !name.is_a?(FalseClass) ? name.dup : name
23
+ end
24
+
25
+ def self.#{name}=(value)
26
+ @#{name} = value
27
+ end
28
+
29
+ def #{name}
30
+ value = self.class.#{name}
31
+ value.instance_of?(Proc) ? value.call : value
32
+ end
33
+ RUBY
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ module Carrierwave
2
+ module Qcloud
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,101 @@
1
+ require 'carrierwave'
2
+ require 'qcloud_cos' # use qcloud cos SDK
3
+
4
+ module CarrierWave
5
+ module Storage
6
+ ##
7
+ # qcloud storage engine
8
+ #
9
+ # CarrierWave.configure do |config|
10
+ # config.storage = :qcloud
11
+ # config.qcloud_app_id = 'xxxxxx'
12
+ # config.qcloud_secret_id = 'xxxxxx'
13
+ # config.qcloud_secret_key = 'xxxxxx'
14
+ # config.qcloud_bucket = "bucketname"
15
+ # end
16
+ #
17
+ # wiki: https://github.com/richardkmichael/carrierwave-activerecord/wiki/Howto:-Adding-a-new-storage-engine
18
+ # rdoc: http://www.rubydoc.info/gems/carrierwave/CarrierWave/Storage/Abstract
19
+ class Qcloud < Abstract
20
+ # config qcloud sdk by getting configuration from uplander
21
+ def self.configure_qcloud_sdk(uploader)
22
+ QcloudCos.configure do |config|
23
+ config.app_id = uploader.qcloud_app_id
24
+ config.secret_id = uploader.qcloud_secret_id
25
+ config.secret_key = uploader.qcloud_secret_key
26
+ config.bucket = uploader.qcloud_bucket
27
+ config.endpoint = "http://web.file.myqcloud.com/files/v1/"
28
+ end
29
+ end
30
+
31
+ # hook: store the file on qcloud
32
+ def store!(file)
33
+ self.class.configure_qcloud_sdk(uploader)
34
+
35
+ qcloud_file = File.new(file)
36
+ qcloud_file.path = uploader.store_path(identifier)
37
+ qcloud_file.store
38
+ qcloud_file
39
+ end
40
+
41
+ # hook: retrieve the file on qcloud
42
+ def retrieve!(identifier)
43
+ self.class.configure_qcloud_sdk(uploader)
44
+
45
+ if uploader.file # file is present after store!
46
+ uploader.file
47
+ else
48
+ file_path = uploader.store_path(identifier)
49
+ File.new(nil).tap do |file|
50
+ file.path = file_path
51
+ file.stat
52
+ end
53
+ end
54
+ end
55
+
56
+ # store and retrieve file using qcloud-cos-sdk
57
+ # sdk ref: https://github.com/zlx/qcloud-cos-sdk/blob/master/wiki/get_started.md#api%E8%AF%A6%E7%BB%86%E8%AF%B4%E6%98%8E
58
+ class File < CarrierWave::SanitizedFile
59
+ attr_accessor :qcloud_info
60
+ attr_accessor :path
61
+
62
+ # store/upload file to qcloud
63
+ def store
64
+ result = QcloudCos.upload(path, file.to_file)
65
+ # e.g.: {"code"=>0, "message"=>"SUCCESS", "data"=>{"access_url"=>"http://#{bucket}-#{app_id}.file.myqcloud.com/uploads/user/avatar/5/81b97fdf4f5b4353916c0f40878258bc.jpg", "resource_path"=>"/uploads/user/avatar/5/81b97fdf4f5b4353916c0f40878258bc.jpg", "source_url"=>"http://#{bucket}-#{app_id}.cos.myqcloud.com/uploads/user/avatar/5/81b97fdf4f5b4353916c0f40878258bc.jpg", "url"=>"http://web.file.myqcloud.com/files/v1/uploads/user/avatar/5/81b97fdf4f5b4353916c0f40878258bc.jpg"}}
66
+
67
+ if result['message'] == 'SUCCESS'
68
+ self.qcloud_info = result['data']
69
+ end
70
+ end
71
+
72
+ # file access url on qcloud
73
+ def url
74
+ qcloud_info['access_url']
75
+ end
76
+
77
+ # get file stat on qcloud
78
+ def stat
79
+ result = QcloudCos.stat(path)
80
+ if result['message'] == 'SUCCESS'
81
+ self.qcloud_info = result['data']
82
+ end
83
+ end
84
+
85
+ # delete file on qcloud
86
+ def delete
87
+ result = QcloudCos.delete(path)
88
+ result['message'] == 'SUCCESS'
89
+ # TODO: delete parent dir if it's empty
90
+ # ref: https://github.com/carrierwaveuploader/carrierwave/wiki/How-to%3A-Make-a-fast-lookup-able-storage-directory-structure
91
+ end
92
+
93
+ # TODO: retrieve/download file from qcloud
94
+ def retrieve
95
+ # TODO
96
+ end
97
+ end
98
+
99
+ end
100
+ end
101
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-qcloud
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - RainChen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: carrierwave
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 0.10.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: qcloud_cos
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 0.4.2
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 0.4.2
83
+ description: Qcloud COS Storage support for CarrierWave
84
+ email:
85
+ - hirainchen@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - README.md
95
+ - Rakefile
96
+ - bin/console
97
+ - bin/setup
98
+ - carrierwave-qcloud.gemspec
99
+ - lib/carrierwave/qcloud.rb
100
+ - lib/carrierwave/qcloud/configuration.rb
101
+ - lib/carrierwave/qcloud/version.rb
102
+ - lib/carrierwave/storage/qcloud.rb
103
+ homepage: https://github.com/rainchen/carrierwave-qcloud
104
+ licenses: []
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.4.8
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Qcloud COS Storage support for CarrierWave
126
+ test_files: []