carrierwave-ucloud 0.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1fb9b45c04ba63614bb31f6c518f6571a1326c2b
4
+ data.tar.gz: f87a8ff0b7b62a6c0e2ff386364521b983c1a8b1
5
+ SHA512:
6
+ metadata.gz: 7618d9886a1ff9516b4eeb98d07a9c6ae9f44b992f0e7941ec98cd0dc34b03f86d5eb334f70cc7a71a27aba575adb42e937e83ed67ad7f36d600e034139e4baa
7
+ data.tar.gz: 31dad4eb67c635dca40e4159df0c782fbe04089f21ec4fabfd044303157851af0ca540edfaa891b6d1589672f637535671e566e4e3ca95084995eb060bdc58ab
@@ -0,0 +1,8 @@
1
+ require 'carrierwave/storage/ucloud'
2
+ require 'carrierwave/ucloud/configuration'
3
+ require 'carrierwave/ucloud/digest'
4
+ CarrierWave.configure do |config|
5
+ config.storage_engines.merge!({ucloud: 'CarrierWave::Storage::UCloud'})
6
+ end
7
+
8
+ CarrierWave::Uploader::Base.send(:include, CarrierWave::UCloud::Configuration)
@@ -0,0 +1,155 @@
1
+ # coding: utf-8
2
+ require 'faraday'
3
+
4
+ module CarrierWave
5
+ module Storage
6
+ ##
7
+ #
8
+ # CarrierWave.configure do |config|
9
+ # config.public_key = "xxxxxx"
10
+ # config.private_key = "xxxxxx"
11
+ # config.ucloud_bucket = "my_bucket"
12
+ # config.ucloud_bucket_host = "https://my_bucket.files.example.com"
13
+ # config.ucloud_cdn_host = "https://my_bucket.files.example.com"
14
+ # end
15
+ #
16
+ #
17
+ class UCloud < Abstract
18
+ class File < CarrierWave::SanitizedFile
19
+ def initialize(uploader, base, path)
20
+ @uploader = uploader
21
+ @path = path
22
+ @base = base
23
+ end
24
+
25
+ ##
26
+ # Returns the current path/filename of the file on Cloud Files.
27
+ #
28
+ # === Returns
29
+ #
30
+ # [String] A path
31
+ #
32
+ def path
33
+ @path
34
+ end
35
+
36
+ def escaped_path
37
+ @escaped_path ||= CGI.escape(@path)
38
+ end
39
+
40
+ ##
41
+ # Reads the contents of the file from Cloud Files
42
+ #
43
+ # === Returns
44
+ #
45
+ # [String] contents of the file
46
+ #
47
+ def read
48
+ res = conn.get(escaped_path)
49
+ @headers = res.headers
50
+ res.body
51
+ end
52
+
53
+ ##
54
+ # Remove the file from Cloud Files
55
+ #
56
+ def delete
57
+ begin
58
+ conn.delete(escaped_path)
59
+ true
60
+ rescue => e
61
+ puts "carrierwave-ucloud delete failed: #{e.inspect}"
62
+ nil
63
+ end
64
+ end
65
+
66
+ ##
67
+ # Returns the url on the Cloud Files CDN. Note that the parent container must be marked as
68
+ # public for this to work.
69
+ #
70
+ # === Returns
71
+ #
72
+ # [String] file's url
73
+ #
74
+ def url
75
+ host = @uploader.ucloud_cdn_host || @uploader.ucloud_bucket_host
76
+ return nil unless host
77
+ [host, @path].join("/")
78
+ end
79
+
80
+ def content_type
81
+ headers[:content_type]
82
+ end
83
+
84
+ def content_type=(new_content_type)
85
+ headers[:content_type] = new_content_type
86
+ end
87
+
88
+ ##
89
+ # Writes the supplied data into the object on Cloud Files.
90
+ #
91
+ # === Returns
92
+ #
93
+ # boolean
94
+ #
95
+ def store(data, headers = {})
96
+ r = conn.put(escaped_path, data) do |req|
97
+ req.headers = headers
98
+ token = CarrierWave::UCloud::Digest.authorization(@uploader, req)
99
+ req.headers['Authorization'] = token
100
+ end
101
+ r
102
+ end
103
+
104
+ def headers
105
+ @headers ||= begin
106
+ conn.get(@path).headers
107
+ rescue Faraday::ClientError
108
+ {}
109
+ end
110
+ end
111
+
112
+ def conn
113
+ return @conn if defined?(@conn)
114
+
115
+ @conn = Faraday.new(url: @uploader.ucloud_bucket_host) do |req|
116
+ req.request :url_encoded
117
+ req.adapter Faraday.default_adapter
118
+ end
119
+ end
120
+ end # File
121
+
122
+ ##
123
+ # Store the file on UCloud
124
+ #
125
+ # === Parameters
126
+ #
127
+ # [file (CarrierWave::SanitizedFile)] the file to store
128
+ #
129
+ # === Returns
130
+ #
131
+ # [CarrierWave::Storage::UCloud::File] the stored file
132
+ #
133
+ def store!(file)
134
+ f = File.new(uploader, self, uploader.store_path)
135
+ f.store(file.read, 'Content-Type' => file.content_type)
136
+ f
137
+ end
138
+
139
+ # Do something to retrieve the file
140
+ #
141
+ # @param [String] identifier uniquely identifies the file
142
+ #
143
+ # [identifier (String)] uniquely identifies the file
144
+ #
145
+ # === Returns
146
+ #
147
+ # [CarrierWave::Storage::UCloud::File] the stored file
148
+ #
149
+ def retrieve!(identifier)
150
+ File.new(uploader, self, uploader.store_path(identifier))
151
+ end
152
+
153
+ end # CloudFiles
154
+ end # Storage
155
+ end # CarrierWave
@@ -0,0 +1,37 @@
1
+ module CarrierWave
2
+ module UCloud
3
+ module Configuration
4
+ extend ActiveSupport::Concern
5
+ included do
6
+ add_config :public_key
7
+ add_config :private_key
8
+ add_config :ucloud_bucket
9
+ add_config :ucloud_bucket_host
10
+ add_config :ucloud_cdn_host
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,32 @@
1
+ module CarrierWave
2
+ module UCloud
3
+ class Digest
4
+ class << self
5
+ def authorization(uploader, req)
6
+ sign = sign(uploader, req)
7
+ "UCloud #{uploader.public_key}:#{sign}"
8
+ end
9
+
10
+ def sign(uploader, req)
11
+ string = string_to_sign(uploader, req)
12
+ digest = OpenSSL::Digest.new('sha1')
13
+ Base64.encode64(OpenSSL::HMAC.digest(digest, uploader.private_key, string))
14
+ end
15
+
16
+ def string_to_sign(uploader, req)
17
+ headers = req.headers
18
+ http_verb = "#{req.method.upcase}\n"
19
+ content_md5 = "\n"
20
+ content_type = "#{headers['Content-Type']}\n"
21
+ date = "\n"
22
+ canonicalized_ucloud_headers = ''
23
+ http_verb + content_md5 + content_type + date + canonicalized_ucloud_headers + canonicalized_resource(uploader)
24
+ end
25
+
26
+ def canonicalized_resource(uploader)
27
+ "/#{uploader.ucloud_bucket}/#{uploader.store_path}"
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-ucloud
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Breeze Yang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-05 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: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
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.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.11.0
41
+ description: UCloud Storage support for CarrierWave
42
+ email:
43
+ - breezeWuna@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/carrierwave-ucloud.rb
49
+ - lib/carrierwave/storage/ucloud.rb
50
+ - lib/carrierwave/ucloud/configuration.rb
51
+ - lib/carrierwave/ucloud/digest.rb
52
+ homepage: https://github.com/breeze-yang/carrierwave-ucloud
53
+ licenses: []
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 2.6.8
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: UCloud Storage support for CarrierWave
75
+ test_files: []