getUpyunFileSize()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 3 Features 0
Metric Value
cc 4
eloc 13
c 5
b 3
f 0
nc 6
nop 1
dl 0
loc 22
rs 9.8333
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' => 120,
23
        'connect_timeout' => 120,
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