Class: Pindo::PgyerUploadClient
- Inherits:
-
Object
- Object
- Pindo::PgyerUploadClient
- Defined in:
- lib/pindo/client/pgyeruploadclient.rb
Defined Under Namespace
Classes: PgyerUploadProgressBar
Instance Attribute Summary collapse
-
#attach_url ⇒ Object
Returns the value of attribute attach_url.
-
#bucket_name ⇒ Object
Returns the value of attribute bucket_name.
-
#default_url ⇒ Object
Returns the value of attribute default_url.
-
#file_size ⇒ Object
Returns the value of attribute file_size.
-
#progress_bar ⇒ Object
Returns the value of attribute progress_bar.
-
#region ⇒ Object
Returns the value of attribute region.
-
#token ⇒ Object
Returns the value of attribute token.
-
#upload_binary_file ⇒ Object
Returns the value of attribute upload_binary_file.
Instance Method Summary collapse
- #create_req(upload_url: nil, body_data: nil, read_length: nil) ⇒ Object
-
#initialize ⇒ PgyerUploadClient
constructor
A new instance of PgyerUploadClient.
- #load_token ⇒ Object
- #multi_task_upload_data_req(task_num: 1, retry_count: 3) ⇒ Object
- #post_upload_finish_req(upload_path_key: nil, upload_id: nil, eTags: nil) ⇒ Object
- #post_upload_url_req(upload_path_key: nil, file_ceil_size: nil) ⇒ Object
- #single_task_upload_data_req ⇒ Object
- #single_task_upload_part_data_req(upload_params_item: nil, hydra_handle: nil) ⇒ Object
- #upload_file(binary_file: nil, isAttach: false) ⇒ Object
Constructor Details
#initialize ⇒ PgyerUploadClient
Returns a new instance of PgyerUploadClient.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/pindo/client/pgyeruploadclient.rb', line 28 def initialize() begin @pgyer_token_file = File.join(File::(Pindoconfig.instance.pindo_dir), ".pgyer_token") config_file = File.join(File::(Pindoconfig.instance.pindo_common_configdir), "pgyer_client_config.json") config_json = JSON.parse(File.read(config_file)) @region = config_json["region"] @bucket_name = config_json["bucket_name"] @default_url = config_json["default_url"] @attach_url = config_json["attach_url"] @use_local_wechat_url = config_json["use_local_wechat_url"] @baseurl = config_json["pgyerapps_base_url"] @request_config = config_json["pgyerapps_req_config"] @pgyer_aes_key = config_json["pgyerapps_aes_key"] @token = load_token rescue => error raise Informative, "PgyerUploadClient 初始化失败!" end end |
Instance Attribute Details
#attach_url ⇒ Object
Returns the value of attribute attach_url.
21 22 23 |
# File 'lib/pindo/client/pgyeruploadclient.rb', line 21 def attach_url @attach_url end |
#bucket_name ⇒ Object
Returns the value of attribute bucket_name.
18 19 20 |
# File 'lib/pindo/client/pgyeruploadclient.rb', line 18 def bucket_name @bucket_name end |
#default_url ⇒ Object
Returns the value of attribute default_url.
20 21 22 |
# File 'lib/pindo/client/pgyeruploadclient.rb', line 20 def default_url @default_url end |
#file_size ⇒ Object
Returns the value of attribute file_size.
24 25 26 |
# File 'lib/pindo/client/pgyeruploadclient.rb', line 24 def file_size @file_size end |
#progress_bar ⇒ Object
Returns the value of attribute progress_bar.
25 26 27 |
# File 'lib/pindo/client/pgyeruploadclient.rb', line 25 def @progress_bar end |
#region ⇒ Object
Returns the value of attribute region.
17 18 19 |
# File 'lib/pindo/client/pgyeruploadclient.rb', line 17 def region @region end |
#token ⇒ Object
Returns the value of attribute token.
15 16 17 |
# File 'lib/pindo/client/pgyeruploadclient.rb', line 15 def token @token end |
#upload_binary_file ⇒ Object
Returns the value of attribute upload_binary_file.
23 24 25 |
# File 'lib/pindo/client/pgyeruploadclient.rb', line 23 def upload_binary_file @upload_binary_file end |
Instance Method Details
#create_req(upload_url: nil, body_data: nil, read_length: nil) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/pindo/client/pgyeruploadclient.rb', line 239 def create_req(upload_url:nil, body_data:nil, read_length:nil) request = nil = { proxy: ENV['http_proxy'] || ENV['https_proxy'], proxyuserpwd: "#{ENV['HTTP_PROXY_USER']}:#{ENV['HTTP_PROXY_PASSWORD']}" } if [:proxy] request = Typhoeus::Request.new( upload_url, method: :put, proxy: [:proxy], proxyuserpwd: [:proxyuserpwd], :body => body_data, :headers => { 'Content-Type' => 'application/octet-stream', 'token' => @token['token'], 'Content-Length' => read_length.to_s } ) else request = Typhoeus::Request.new( upload_url, method: :put, :body => body_data, :headers => { 'Content-Type' => 'application/octet-stream', 'token' => @token['token'], 'Content-Length' => read_length.to_s } ) end return request end |
#load_token ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/pindo/client/pgyeruploadclient.rb', line 54 def load_token() token = nil if File.exist?(@pgyer_token_file) begin data = File.read(@pgyer_token_file) data_string = AESHelper::aes_128_ecb_decrypt(@pgyer_aes_key, data) temp_token = data_string temp_token = JSON.parse(data_string) if !temp_token.nil? && !temp_token["token"].nil? && !temp_token["username"].nil? token = temp_token end rescue => error raise Informative, "PgyerUploadClient 加载pgyer token 失败!!!" end end return token end |
#multi_task_upload_data_req(task_num: 1, retry_count: 3) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/pindo/client/pgyeruploadclient.rb', line 149 def multi_task_upload_data_req(task_num:1, retry_count: 3) for i in 0..@upload_params_list.length-1 do @upload_params_list[i]["retryCount"] = retry_count end while @upload_params_list.size > 0 upload_params_list_temp = [] #每次最大5个线程上传 for i in 1..task_num do upload_params_list_temp << @upload_params_list.shift end hydra = Typhoeus::Hydra.new while upload_params_list_temp.size > 0 upload_params_item = upload_params_list_temp.shift unless upload_params_item.nil? single_task_upload_part_data_req(upload_params_item:upload_params_item, hydra_handle:hydra) end end hydra.run end end |
#post_upload_finish_req(upload_path_key: nil, upload_id: nil, eTags: nil) ⇒ Object
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/pindo/client/pgyeruploadclient.rb', line 276 def post_upload_finish_req(upload_path_key:nil, upload_id:nil, eTags:nil) boss_url = @baseurl + @request_config["multi_signed_url_upload"] body_params = { functionName:"finish", fileKey:upload_path_key, uploadId:upload_id, tags:eTags } con = HttpClient.create_instance_with_proxy res = con.post do |req| req.url boss_url req.headers['Content-Type'] = 'application/json' req.headers['token'] = @token["token"] req.body = body_params.to_json end result_data = nil if !res.body.nil? result_data = JSON.parse(res.body) end return result_data end |
#post_upload_url_req(upload_path_key: nil, file_ceil_size: nil) ⇒ Object
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/pindo/client/pgyeruploadclient.rb', line 306 def post_upload_url_req(upload_path_key:nil, file_ceil_size:nil) boss_url = @baseurl + @request_config["multi_signed_url_upload"] body_params = { functionName:"start", fileKey:upload_path_key, fileSize:file_ceil_size } con = HttpClient.create_instance_with_proxy res = con.post do |req| req.url boss_url req.headers['Content-Type'] = 'application/json' req.headers['token'] = @token["token"] req.body = body_params.to_json end result_data = nil if !res.body.nil? result_data = JSON.parse(res.body) end return result_data end |
#single_task_upload_data_req ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/pindo/client/pgyeruploadclient.rb', line 173 def single_task_upload_data_req() for i in 0..@upload_params_list.length-1 do @upload_params_list[i]["retryCount"] = 3 end while @upload_params_list.size > 0 upload_params_item = @upload_params_list.shift single_task_upload_part_data_req(upload_params_item:upload_params_item) end return @eTags end |
#single_task_upload_part_data_req(upload_params_item: nil, hydra_handle: nil) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/pindo/client/pgyeruploadclient.rb', line 188 def single_task_upload_part_data_req(upload_params_item:nil, hydra_handle:nil) upload_url = upload_params_item["signedUrl"] part_no = upload_params_item["partNo"] file_size_ele = 1024 * 1024 * 5 #5M start_position = file_size_ele * (part_no -1) if part_no * file_size_ele > @file_size read_length = @file_size - start_position else read_length = file_size_ele end file = File.open(@upload_binary_file, "rb") file.seek(start_position) put_data = file.read(read_length) request = create_req(upload_url:upload_url, body_data:put_data, read_length:read_length) request.on_progress do |dltotal, dlnow, ultotal, ulnow| if ulnow @progress_bar.update_upload_index(upload_part:part_no, upload_size:ulnow) @progress_bar.update_upload_progress() end end request.on_complete do |response| if response.success? @progress_bar.complete_upload_index(upload_part:part_no, complete_size:read_length) etag = response.headers["ETag"] eTag_item = { partNumber: part_no, tag: etag} @upload_eTags << eTag_item else @progress_bar.delete_upload_index(upload_part:part_no) upload_params_item["retryCount"] = upload_params_item["retryCount"] - 1 if upload_params_item["retryCount"] > 0 # @upload_params_list.push(upload_params_item) single_task_upload_part_data_req(upload_params_item:upload_params_item, hydra_handle:hydra_handle) else Funlog.instance.("文件#{@upload_binary_file} 上传失败! 😭😭😭") raise Informative, "上传文件失败" end end end if hydra_handle.nil? request.run else hydra_handle.queue(request) end end |
#upload_file(binary_file: nil, isAttach: false) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/pindo/client/pgyeruploadclient.rb', line 73 def upload_file(binary_file:nil, isAttach:false) @upload_binary_file = binary_file @file_size = File.size(@upload_binary_file) @progress_bar = PgyerUploadProgressBar.new(upload_total_size:@file_size) extension = File.extname(@upload_binary_file) filename = File.basename(@upload_binary_file) size_level = 1024 * 1024 file_bytes = File.binread(@upload_binary_file) checksum = Digest::MD5.hexdigest(file_bytes) file_uuid = SecureRandom.uuid total_m = sprintf("%.2f", 1.00 * @file_size / 1024 /1024 ) upload_path = @default_url content_disposition = nil if isAttach upload_path = @default_url + @attach_url content_disposition = "attachment; filename=#{filename}" end upload_path_key = upload_path + file_uuid + extension task_num = 10 puts "文件路径: #{@upload_binary_file}" puts "文件大小: #{total_m}M" puts "上传路径: #{upload_path_key}" puts puts "切片大小: 5MB" upload_result = nil file_size_param = 1.00 * @file_size / 1024 /1024 result_data = post_upload_url_req(upload_path_key:upload_path_key, file_ceil_size:file_size_param.ceil) upload_id= result_data["data"]["uploadId"] @upload_params_list = result_data["data"]["uploadParamsList"] task_num = @upload_params_list.length retry_count = 5 if task_num < 2 task_num = 2 end if task_num > 100 task_num = 100 end puts "线程个数: #{task_num}" puts "重试次数: #{retry_count}" puts Funlog.instance.("开始上传...") @upload_eTags = [] # single_task_upload_data_req( ) multi_task_upload_data_req(task_num:task_num, retry_count:retry_count) result_data = post_upload_finish_req(upload_path_key:upload_path_key, upload_id:upload_id, eTags:@upload_eTags) if result_data["code"] == 200 upload_result = upload_path_key Funlog.instance.("文件#{@upload_binary_file} 上传成功! 😎😎😎") else upload_result = nil Funlog.instance.("文件#{@upload_binary_file} 上传失败! 😭😭😭") end return upload_result end |