carrierwave_ucloud 0.1.0 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c09d21bba0998f093f55413283dc9f53539b529a
4
- data.tar.gz: cf996c58986ab64c9bcf0d1ebc2afcf3eaacc939
3
+ metadata.gz: 0371f9bcb1e2f3c5e7685999182e20c83d96106a
4
+ data.tar.gz: c016bf40f6bf941a076a35530c209839941e9714
5
5
  SHA512:
6
- metadata.gz: 99a838f33ac7d29e181a74349aeb2100c729056a2d98a3eee3fd2a6b5d26cbc86ff7111d9b37e157a1ff660cbe12de74208ecedcecf179129c4c2e0e81b89963
7
- data.tar.gz: d159af8c065345d5056114f70b8196d492229d8e4a6df65847a5a96b156810d13836c5a11a25a603fc6c8413d43b6605ad460a86cc95434d2f09cf9dfa72fdb7
6
+ metadata.gz: 75d639800e68ae9fbdda57ecdb924202711794a4a59ca6eb0bf00c2cf91efc70e792042d117dc60b7c9b13ad226a247a0a6676906e7e9246074b2fb5759288c1
7
+ data.tar.gz: 07faae1fa67dc46db26f3037345ed4f50edcf83042eb87672769f02e3ed2c4269f58a920ef06c432dcb445226e9928a09ef0820c3c953f539d94321883914296
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+ Gemfile.lock
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # CarrierwaveUcloud
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/carrierwave_ucloud`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Ucloud Ufile CarrierWave Gem。
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ 参考了aws/aliyun/ucloud多个已有CarrierWave插件的代码,感谢他们的付出。
6
6
 
7
7
  ## Installation
8
8
 
@@ -22,7 +22,36 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- TODO: Write usage instructions here
25
+ 配置文件:config/initializers/carrierwave.rb,内容如下:
26
+
27
+ ```ruby
28
+ # public / private 最少配置一套,ucloud_public_read自己要对应好,暂时没有做任何校验逻辑
29
+ CarrierWave.configure do |config|
30
+ config.storage = :ucloud
31
+ config.ucloud_public_key = "public_key"
32
+ config.ucloud_private_key = "private_key"
33
+ config.ucloud_public_read = true # 默认使用public bucket,可在单个uploader覆写
34
+ # public bucket配置
35
+ config.ucloud_public_bucket = "public_bucket_name"
36
+ config.ucloud_public_bucket_host = "http://public_bucket_name.cn-bj.ufileos.com"
37
+ config.ucloud_public_cdn_host = "http://public_bucket_name.cn-bj.ufileos.com"
38
+ config.ucloud_private_bucket = "private_bucket_name"
39
+ # private bucket配置
40
+ config.ucloud_private_bucket_host = "http://private_bucket_name.cn-bj.ufileos.com"
41
+ config.ucloud_private_cdn_host = "http://private_bucket_name.cn-bj.ufileos.com"
42
+ config.ucloud_private_expire_seconds = 300
43
+ end
44
+ ```
45
+
46
+ 全局配置ucloud_public_read,默认使用对应bucket配置,个别uploader如要用另外一种bucket配置,添加如下方法:
47
+ ```ruby
48
+ class XxxUploader < CarrierWave::Uploader::Base
49
+ def ucloud_public_read
50
+ # true or false,XxxUploader以此配置选择bucket配置及逻辑
51
+ end
52
+ end
53
+ ```
54
+
26
55
 
27
56
  ## Development
28
57
 
@@ -32,6 +32,9 @@ Gem::Specification.new do |spec|
32
32
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
33
  spec.require_paths = ["lib"]
34
34
 
35
+ spec.add_dependency 'carrierwave', '~> 1.2'
36
+ spec.add_dependency 'faraday', '~> 0.11'
37
+
35
38
  spec.add_development_dependency "bundler", "~> 1.16"
36
39
  spec.add_development_dependency "rake", "~> 10.0"
37
40
  spec.add_development_dependency "rspec", "~> 3.0"
@@ -0,0 +1,17 @@
1
+ require 'carrierwave'
2
+
3
+ module CarrierWave
4
+ module Storage
5
+ class Ucloud < Abstract
6
+ def store!(file)
7
+ f = UcloudFile.new(uploader, self, uploader.store_path)
8
+ f.store(file, 'Content-Type' => file.content_type)
9
+ f
10
+ end
11
+
12
+ def retrieve!(identifier)
13
+ UcloudFile.new(uploader, self, uploader.store_path(identifier))
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,44 @@
1
+ module CarrierWave
2
+ module Storage
3
+ class UcloudFile < CarrierWave::SanitizedFile
4
+ attr_reader :path, :bucket
5
+
6
+ def initialize(uploader, base, path)
7
+ @uploader = uploader
8
+ @path = path
9
+ @base = base
10
+ @bucket = ::CarrierWave::Ucloud::Bucket.new(uploader)
11
+ end
12
+
13
+ def read
14
+ response = bucket.get(path)
15
+ @headers = response.headers.deep_transform_keys { |k| k.underscore.to_sym rescue key }
16
+ response.body
17
+ end
18
+
19
+ def delete
20
+ bucket.delete(path)
21
+ end
22
+
23
+ def url(opts = {})
24
+ bucket.url(path, opts)
25
+ end
26
+
27
+ def store(file, headers = {})
28
+ bucket.put(path, file, headers)
29
+ end
30
+
31
+ def content_type
32
+ headers[:content_type]
33
+ end
34
+
35
+ def content_type=(new_content_type)
36
+ headers[:content_type] = new_content_type
37
+ end
38
+
39
+ def headers
40
+ @headers ||= {}
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,132 @@
1
+ require 'base64'
2
+ require 'openssl'
3
+
4
+ module CarrierWave
5
+ module Ucloud
6
+ class Bucket
7
+ PATH_PREFIX = %r{^/}
8
+
9
+ def initialize(uploader)
10
+ @ucloud_public_key = uploader.ucloud_public_key
11
+ @ucloud_private_key = uploader.ucloud_private_key
12
+ @ucloud_public_read = uploader.ucloud_public_read
13
+ @ucloud_bucket = @ucloud_public_read ? uploader.ucloud_public_bucket : uploader.ucloud_private_bucket
14
+ @ucloud_bucket_host = @ucloud_public_read ? uploader.ucloud_public_bucket_host : uploader.ucloud_private_bucket_host
15
+ @ucloud_cdn_host = @ucloud_public_read ? uploader.ucloud_public_cdn_host : uploader.ucloud_private_cdn_host
16
+ @ucloud_private_expire_seconds = uploader.ucloud_private_expire_seconds || 300
17
+
18
+ unless @ucloud_cdn_host.include?('//')
19
+ raise "config.ucloud_cdn_host requirement include // http:// or https://, but you give: #{@ucloud_cdn_host}"
20
+ end
21
+ end
22
+
23
+ # 上传文件
24
+ def put(path, file, headers = {})
25
+ path.sub!(PATH_PREFIX, '')
26
+
27
+ response = conn.put(path, file.read) do |req|
28
+ req.headers = headers
29
+ token = authorization(req.method, headers['Content-Type'], path)
30
+ req.headers['Authorization'] = token
31
+ end
32
+
33
+ if response.success?
34
+ true
35
+ else
36
+ raise 'Ucloud上传失败'
37
+ end
38
+ end
39
+
40
+ # 读取文件
41
+ def get(path)
42
+ path.sub!(PATH_PREFIX, '')
43
+ response = conn.get(url(path))
44
+
45
+ if response.success?
46
+ return response
47
+ else
48
+ raise 'Ucloud Get File Fail'
49
+ end
50
+ end
51
+
52
+ # 删除文件
53
+ def delete(path)
54
+ path.sub!(PATH_PREFIX, '')
55
+ response = conn.delete(url(path)) do |req|
56
+ req.headers['Authorization'] = authorization(req.method, nil, path)
57
+ end
58
+
59
+ if response.success?
60
+ true
61
+ else
62
+ raise 'Ucloud Get File Fail'
63
+ end
64
+ end
65
+
66
+ def url(path, opts = {})
67
+ if @ucloud_public_read
68
+ public_get_url(path, opts)
69
+ else
70
+ private_get_url(path, opts)
71
+ end
72
+ end
73
+
74
+ # 公开的访问地址
75
+ def public_get_url(path, opts = {})
76
+ path.sub!(PATH_PREFIX, '')
77
+ url = ''
78
+ if opts[:thumb]
79
+ # TODO
80
+ else
81
+ url = [@ucloud_cdn_host, path].join('/')
82
+ end
83
+ url
84
+ end
85
+
86
+ # 私有空间访问地址
87
+ def private_get_url(path, opts = {})
88
+ public_get_url(path, opts) + privite_get_url_auth(path)
89
+ end
90
+
91
+ private
92
+
93
+ def conn
94
+ @conn ||= begin
95
+ Faraday.new(url: @ucloud_bucket_host) do |req|
96
+ req.request :url_encoded
97
+ req.adapter Faraday.default_adapter
98
+ end
99
+ end
100
+ end
101
+
102
+ # 私密查看url的认证信息
103
+ def privite_get_url_auth(path)
104
+ expired_ts = private_expire_ts
105
+ signed_str = signature(string_to_sign('GET', nil, path, expired_ts))
106
+ "?UCloudPublicKey=#{@ucloud_public_key}&Expires=#{expired_ts}&Signature=#{signed_str}"
107
+ end
108
+
109
+ def private_expire_ts
110
+ @ucloud_private_expire_seconds + Time.now.to_i
111
+ end
112
+
113
+ def authorization(http_method, content_type, path)
114
+ signed_str = signature(string_to_sign(http_method, content_type, path))
115
+ "UCloud " + @ucloud_public_key + ":" + signed_str
116
+ end
117
+
118
+ def signature(str)
119
+ Base64.strict_encode64(OpenSSL::HMAC.digest('sha1', @ucloud_private_key, str))
120
+ end
121
+
122
+ def string_to_sign(http_method, ori_content_type, path, expired_ts = nil)
123
+ http_verb = "#{http_method.upcase}\n"
124
+ content_md5 = "\n"
125
+ content_type = "#{ori_content_type}\n"
126
+ timestamp = "#{expired_ts}\n"
127
+ full_path = "/#{@ucloud_bucket}/#{path}"
128
+ http_verb + content_md5 + content_type + timestamp + full_path
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,28 @@
1
+ module CarrierWave
2
+ module Ucloud
3
+ module Configuration
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ add_config :ucloud_public_key
8
+ add_config :ucloud_private_key
9
+ # 是否使用公开url读取(其实跟设置的bucket是否私密有关),需要手工指定,可在Uploader覆写
10
+ add_config :ucloud_public_read
11
+ # 公开bucket的配置
12
+ add_config :ucloud_public_bucket
13
+ add_config :ucloud_public_bucket_host
14
+ add_config :ucloud_public_cdn_host
15
+ # 私有bucket的配置
16
+ add_config :ucloud_private_bucket
17
+ add_config :ucloud_private_bucket_host
18
+ add_config :ucloud_private_cdn_host
19
+ # 如果是私密的bucket,生成url的有效时间
20
+ add_config :ucloud_private_expire_seconds
21
+
22
+ configure do |config|
23
+ config.storage_engines[:ucloud] = 'CarrierWave::Storage::Ucloud'
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,4 +1,10 @@
1
- require "carrierwave_ucloud/version"
1
+ require 'carrierwave_ucloud/version'
2
+ require 'carrierwave/storage/ucloud'
3
+ require 'carrierwave/storage/ucloud_file'
4
+ require 'carrierwave/ucloud/bucket'
5
+ require 'carrierwave/ucloud/configuration'
6
+
7
+ CarrierWave::Uploader::Base.send(:include, CarrierWave::Ucloud::Configuration)
2
8
 
3
9
  module CarrierwaveUcloud
4
10
  # Your code goes here...
@@ -1,3 +1,3 @@
1
1
  module CarrierwaveUcloud
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carrierwave_ucloud
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - zhchsf
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-29 00:00:00.000000000 Z
11
+ date: 2018-08-30 00:00:00.000000000 Z
12
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: '1.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.11'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.11'
13
41
  - !ruby/object:Gem::Dependency
14
42
  name: bundler
15
43
  requirement: !ruby/object:Gem::Requirement
@@ -70,6 +98,10 @@ files:
70
98
  - bin/console
71
99
  - bin/setup
72
100
  - carrierwave_ucloud.gemspec
101
+ - lib/carrierwave/storage/ucloud.rb
102
+ - lib/carrierwave/storage/ucloud_file.rb
103
+ - lib/carrierwave/ucloud/bucket.rb
104
+ - lib/carrierwave/ucloud/configuration.rb
73
105
  - lib/carrierwave_ucloud.rb
74
106
  - lib/carrierwave_ucloud/version.rb
75
107
  homepage: https://github.com/zhchsf/carrierwave_ucloud