carrierwave-upyun 1.0.1 → 1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f791f761160e72a58c354c42e721eafa4a7f832eb5ecc5b7bc79a8a870c852b9
4
- data.tar.gz: 9af67d6bdf3b6b82cc8e237b4e867a08fdc27e595c90e20b5a8630b3defffdc0
3
+ metadata.gz: 46d635232316c1ca6dfbdf4314dd97f9aa41a3b8595a68d7bad51311e80a092d
4
+ data.tar.gz: f59490f9edb05becd23b4ce4686ea45d5feedb6130e821fb1a4c9c0012bff44e
5
5
  SHA512:
6
- metadata.gz: 9fd03f1cbf3f23f5df6cb1015508d7e29406f2eb55eccf7170417be372220d0eedc59323b9c853473013a094fb55923065572d64e843afc195166d5e478413fb
7
- data.tar.gz: a2090f8fd7154ddefbc43914d11929a7f1871e38eea1b0a7bbbd81557053eb88273164120f2b8c785e0f58271e1f8eb58f78b0c8f397d0fb7f596cb42ea9928f
6
+ metadata.gz: 8a82ebefdccdf6547a213be676dc7394ba73fab6b4b438b53508d8e47c5ba569a03f284b257c25f67f12ff753e84bfb09799aa2659221daadbb6ad2b9bf01bcf
7
+ data.tar.gz: 691d27d37b8f10f0ea2209a306fd9d22ece505fd16e7a4a9b7c418c731439de24eeb0d50bc126d8b55443c69df70aca3ea9bbcd3701b32442782c0202fc783ee
@@ -1,8 +1,12 @@
1
+ ## 1.0.2
2
+
3
+ - 修正 1.0.1 版本上传文件存储不正确的问题。
4
+
1
5
  ## 1.0.1
2
6
 
3
7
  - 对于 CarrierWave 的 cache 机制正确支持;
4
8
  - 对 UpYun 偶发的 `concurrent put or delete` 错误容错;
5
- - 清理不必要的 dependency.
9
+ - 清理不必要的 dependency
6
10
 
7
11
  ## 1.0.0
8
12
 
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "carrierwave/storage/upyun"
4
+ require "carrierwave/storage/upyun/file"
4
5
  require "carrierwave/upyun/configuration"
5
6
 
6
7
  CarrierWave.configure do |config|
@@ -21,125 +21,6 @@ module CarrierWave
21
21
  class UploadError < RuntimeError; end
22
22
  class ConcurrentUploadError < RuntimeError; end
23
23
 
24
- class File < CarrierWave::SanitizedFile
25
- def initialize(uploader, base, path)
26
- @uploader = uploader
27
- @path = path
28
- @base = base
29
- end
30
-
31
- ##
32
- # Returns the current path/filename of the file on Cloud Files.
33
- #
34
- # === Returns
35
- #
36
- # [String] A path
37
- #
38
- attr_reader :path
39
-
40
- def escaped_path
41
- @escaped_path ||= CGI.escape(@path)
42
- end
43
-
44
- def content_type
45
- @content_type || ""
46
- end
47
-
48
- attr_writer :content_type
49
-
50
- ##
51
- # Reads the contents of the file from Cloud Files
52
- #
53
- # === Returns
54
- #
55
- # [String] contents of the file
56
- #
57
- def read
58
- res = conn.get(escaped_path)
59
- @headers = res.headers
60
- res.body
61
- end
62
-
63
- ##
64
- # Remove the file from Cloud Files
65
- #
66
- def delete
67
- conn.delete(escaped_path)
68
- true
69
- rescue StandardError => e
70
- puts "carrierwave-upyun delete failed: #{e.inspect}"
71
- nil
72
- end
73
-
74
- ##
75
- # Returns the url on the Cloud Files CDN. Note that the parent container must be marked as
76
- # public for this to work.
77
- #
78
- # === Returns
79
- #
80
- # [String] file's url
81
- #
82
- def url
83
- return nil unless @uploader.upyun_bucket_host
84
-
85
- [@uploader.upyun_bucket_host, @path].join("/")
86
- end
87
-
88
- def content_type
89
- headers[:content_type]
90
- end
91
-
92
- def content_type=(new_content_type)
93
- headers[:content_type] = new_content_type
94
- end
95
-
96
- ##
97
- # Writes the supplied data into the object on Cloud Files.
98
- #
99
- # === Returns
100
- #
101
- # boolean
102
- #
103
- def store(new_file, headers = {})
104
- res = conn.put(escaped_path, new_file.read) do |req|
105
- req.headers = { "Expect" => "", "Mkdir" => "true" }.merge(headers)
106
- end
107
-
108
- if res.status != 200
109
- # code: 42900007 -> concurrent put or delete
110
- json = JSON.parse(res.body)
111
- # retry upload
112
- raise ConcurrentUploadError, res.body if json["code"] == 42_900_007
113
-
114
- raise UploadError, res.body
115
- end
116
-
117
- true
118
- rescue ConcurrentUploadError => e
119
- puts "Warning: UpYun error #{e.message}, retry again."
120
- retry
121
- end
122
-
123
- def headers
124
- @headers ||= begin
125
- conn.get(@path).headers
126
- rescue Faraday::ClientError
127
- {}
128
- end
129
- end
130
-
131
- def conn
132
- @conn ||= begin
133
- api_host = @uploader.upyun_api_host || DEFAULT_API_URL
134
- Faraday.new(url: "#{api_host}/#{@uploader.upyun_bucket}") do |req|
135
- req.request :basic_auth, @uploader.upyun_username, @uploader.upyun_password
136
- req.request :url_encoded
137
- req.adapter Faraday.default_adapter
138
- end
139
- end
140
- end
141
- end # File
142
-
143
24
  ##
144
25
  # Store the file on UpYun
145
26
  #
@@ -0,0 +1,186 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+
5
+ module CarrierWave::Storage
6
+ class UpYun
7
+ class File
8
+ def initialize(uploader, base, path)
9
+ @uploader = uploader
10
+ @path = path
11
+ @base = base
12
+ end
13
+
14
+ ##
15
+ # Returns the current path/filename of the file on Cloud Files.
16
+ #
17
+ # === Returns
18
+ #
19
+ # [String] A path
20
+ #
21
+ attr_reader :path
22
+
23
+ def escaped_path
24
+ @escaped_path ||= CGI.escape(@path)
25
+ end
26
+
27
+ def content_type
28
+ @content_type || ""
29
+ end
30
+
31
+ attr_writer :content_type
32
+
33
+ ##
34
+ # Reads the contents of the file from Cloud Files
35
+ #
36
+ # === Returns
37
+ #
38
+ # [String] contents of the file
39
+ #
40
+ def read
41
+ res = conn.get(escaped_path)
42
+ @headers = res.headers
43
+ res.body
44
+ end
45
+
46
+ ##
47
+ # Remove the file from Cloud Files
48
+ #
49
+ def delete
50
+ conn.delete(escaped_path)
51
+ true
52
+ rescue StandardError => e
53
+ puts "carrierwave-upyun delete failed: #{e.inspect}"
54
+ nil
55
+ end
56
+
57
+ ##
58
+ # Returns the url on the Cloud Files CDN. Note that the parent container must be marked as
59
+ # public for this to work.
60
+ #
61
+ # === Returns
62
+ #
63
+ # [String] file's url
64
+ #
65
+ def url
66
+ return nil unless @uploader.upyun_bucket_host
67
+
68
+ [@uploader.upyun_bucket_host, @path].join("/")
69
+ end
70
+
71
+ ##
72
+ # Return file name, if available
73
+ #
74
+ # === Returns
75
+ #
76
+ # [String] file name
77
+ # or
78
+ # [NilClass] no file name available
79
+ #
80
+ def filename
81
+ return unless file_url = url
82
+ ::File.basename(file_url.split('?').first)
83
+ end
84
+
85
+ def extension
86
+ path_elements = path.split('.')
87
+ path_elements.last if path_elements.size > 1
88
+ end
89
+
90
+ def content_type
91
+ headers[:content_type]
92
+ end
93
+
94
+ def content_type=(new_content_type)
95
+ headers[:content_type] = new_content_type
96
+ end
97
+
98
+ def headers
99
+ @headers ||= begin
100
+ conn.get(@path).headers
101
+ rescue Faraday::ClientError
102
+ {}
103
+ end
104
+ end
105
+
106
+ ##
107
+ # Writes the supplied data into the object on Cloud Files.
108
+ #
109
+ # === Returns
110
+ #
111
+ # boolean
112
+ #
113
+ def store(new_file, headers = {})
114
+ # Copy from cache_path
115
+ if new_file.is_a?(self.class)
116
+ new_file.copy_to(self.escaped_path)
117
+ return true
118
+ end
119
+
120
+ res = conn.put(self.escaped_path, new_file.read) do |req|
121
+ req.headers = { "Expect" => "", "Mkdir" => "true" }.merge(headers)
122
+ end
123
+
124
+ check_put_response!(res)
125
+
126
+ true
127
+ rescue ConcurrentUploadError => e
128
+ retry
129
+ end
130
+
131
+ ##
132
+ # Creates a copy of this file and returns it.
133
+ #
134
+ # === Parameters
135
+ #
136
+ # [new_path (String)] The path where the file should be copied to.
137
+ #
138
+ # === Returns
139
+ #
140
+ # @return [CarrierWave::Storage::UpYun::File] the location where the file will be stored.
141
+ #
142
+ def copy_to(new_path)
143
+ escaped_new_path = CGI.escape(new_path)
144
+ res = conn.put(new_path) do |req|
145
+ req.headers = {
146
+ "X-Upyun-Copy-Source" => "/#{@uploader.upyun_bucket}/#{self.path}",
147
+ "Content-Length" => 0,
148
+ "Mkdir" => "true",
149
+ }
150
+ end
151
+
152
+ check_put_response!(res)
153
+
154
+ File.new(@uploader, @base, new_path)
155
+ rescue ConcurrentUploadError => e
156
+ retry
157
+ end
158
+
159
+ private
160
+
161
+ def check_put_response!(res)
162
+ if res.status != 200
163
+ # code: 42900007 -> concurrent put or delete
164
+ json = JSON.parse(res.body)
165
+ # retry upload
166
+ raise ConcurrentUploadError, res.body if json["code"] == 42_900_007
167
+
168
+ raise UploadError, res.body
169
+ end
170
+ end
171
+
172
+ def conn
173
+ @conn ||= begin
174
+ api_host = @uploader.upyun_api_host || DEFAULT_API_URL
175
+ Faraday.new(url: "#{api_host}/#{@uploader.upyun_bucket}") do |req|
176
+ req.request :basic_auth, @uploader.upyun_username, @uploader.upyun_password
177
+ req.request :url_encoded
178
+ req.adapter Faraday.default_adapter
179
+ end
180
+ end
181
+ end
182
+
183
+
184
+ end
185
+ end
186
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carrierwave-upyun
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nowa Zhu
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-11-12 00:00:00.000000000 Z
12
+ date: 2019-12-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: carrierwave
@@ -57,6 +57,7 @@ files:
57
57
  - README.md
58
58
  - lib/carrierwave-upyun.rb
59
59
  - lib/carrierwave/storage/upyun.rb
60
+ - lib/carrierwave/storage/upyun/file.rb
60
61
  - lib/carrierwave/upyun/configuration.rb
61
62
  homepage: https://github.com/nowa/carrierwave-upyun
62
63
  licenses: []