Passed
Push — master ( 93ae1f...c8123d )
by TQ
04:05
created

getFileUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 2
b 0
f 0
1
<?php
2
require dirname(__DIR__) . '/vendor/autoload.php';
3
4
use GuzzleHttp\Client;
5
use GuzzleHttp\Exception\RequestException;
6
7
define('USER_NAME', 'tester');
8
define('PWD', 'grjxv2mxELR3');
9
define('BUCKET', 'sdkimg');
10
define('PIC_PATH', dirname(__FILE__) . '/assets/sample.jpeg');
11
define('PIC_SIZE', filesize(PIC_PATH));
12
13
function getFileUrl($path)
14
{
15
    return "http://" . BUCKET . ".b0.upaiyun.com/" . ltrim($path, '/');
16
}
17
18
function getUpyunFileSize($path)
19
{
20
    $url = getFileUrl($path);
21
    $client = new Client([
22
        'timeout' => 60,
23
        'connect_timeout' => 60,
24
        'http_errors' => false,
25
    ]);
26
27
    for ($i = 0; $i < 3; $i++) {
28
        try {
29
            $response = $client->head($url);
30
            if ($response->getStatusCode() === 200) {
31
                return (int) $response->getHeaderLine('Content-Length');
32
            }
33
        } catch (RequestException $e) {
34
            // 忽略网络异常,继续重试
35
        }
36
        usleep(500000); // 等待 500ms
37
    }
38
39
    return false;
40
}
41
42