UploadHelper.php
5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace App\Helpers;
use AlibabaCloud\SDK\Mts\V20140618\Mts;
use AlibabaCloud\SDK\Sts\V20150401\Models\AssumeRoleRequest;
use AlibabaCloud\SDK\Sts\V20150401\Models\AssumeRoleResponseBody\credentials;
use AlibabaCloud\SDK\Sts\V20150401\Sts;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\Config;
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
use OSS\OssClient;
class UploadHelper
{
/**
* @return bool
*/
public static function isInternalServer(): bool
{
return config('services.oss.internal_server', false);
}
/**
* @param array<string,string> $options
* @return \Darabonba\OpenApi\Models\Config
*/
protected static function getConfig(array $options = []): Config
{
return new Config(['accessKeyId' => config('services.oss.sts.key'), 'accessKeySecret' => config('services.oss.sts.secret'), ...$options]);
}
/**
* @return \AlibabaCloud\SDK\Sts\V20150401\Sts
*/
public static function getStsClient(): Sts
{
return new Sts(self::getConfig(['regionId' => config('services.oss.sts.endpoint')]));
}
/**
* @param int $second
* @return \AlibabaCloud\SDK\Sts\V20150401\Models\AssumeRoleResponseBody\credentials
*/
public static function getStsToken(int $second = 3000): credentials
{
$assumeRoleRequest = new AssumeRoleRequest([
"roleArn" => config('services.oss.sts.role'),
"roleSessionName" => Str::uuid()->toString(),
"durationSeconds" => $second,
]);
return self::getStsClient()->assumeRoleWithOptions($assumeRoleRequest, new RuntimeOptions([]))->body->credentials;
}
/**
* @return \AlibabaCloud\SDK\Mts\V20140618\Mts
*/
public static function getMtsClient(): Mts
{
return new Mts(self::getConfig(['regionId' => config('services.oss.mts.endpoint'), 'protocol' => 'HTTPS']));
}
/**
* @return \OSS\OssClient
*/
public static function getOssClient(bool $internal = false): OssClient
{
$token = self::getStsToken();
return new OssClient($token->accessKeyId, $token->accessKeySecret, $internal ? self::getOssInternal() : self::getOssEndpoint(), false, $token->securityToken);
}
/**
* @param string $object
* @param array<string,mixed> $options
* @param int $second
* @return string
* @throws \OSS\Core\OssException
*/
public static function getOssSignUrl(string $object, array $options = [], int $second = 3000): string
{
return self::getOssClient()->signUrl(self::getOssBucket(), $object, $second, "PUT", [
OssClient::OSS_OBJECT_ACL => OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE, ...$options
]);
}
/**
* @return string
*/
public static function getOssBucket(): string
{
return config('services.oss.bucket');
}
public static function getOssSSL(): string
{
return config('services.oss.ssl') ? 'https://' : 'http://';
}
/**
* @return string
*/
public static function getOssDomain(bool $host = true): string
{
return Str::of(config('services.oss.domain'))->when($host, fn(Stringable $str) => $str->prepend(self::getOssSSL()))->toString();
}
/**
* @param bool $useSuper
* @param bool $host
* @return string
*/
public static function getOssEndpoint(bool $useSuper = false, bool $host = false): string
{
return Str::of($useSuper ? config('services.oss.super_endpoint') : config('services.oss.endpoint'))
->when($host, fn(Stringable $str) => $str->prepend(self::getOssSSL() . self::getOssBucket() . '.'))->toString();
}
public static function getOssInternal(bool $host = false): string
{
return Str::of(config('services.oss.internal'))
->when($host, fn(Stringable $str) => $str->prepend(self::getOssSSL() . self::getOssBucket() . '.'))->toString();
}
/**
* @param string $prefix
* @return string
*/
public static function getFileName(string $prefix = ''): string
{
return implode('/', array_filter([$prefix, now()->format('Ymd'), Str::ulid()->toBase32()]));
}
/**
* @param string $url
* @return string
*/
public static function urlToPath(string $url): string
{
return ltrim(parse_url($url, PHP_URL_PATH), '/');
}
/**
* @param string $object
* @param string $file
* @param array<string,mixed> $options
* @return string
* @throws \OSS\Core\OssException
*/
public static function put(string $object, string $file, array $options = []): string
{
self::getOssClient(self::isInternalServer())->uploadFile(self::getOssBucket(), $object, $file, [
OssClient::OSS_OBJECT_ACL => OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE, ...$options
]);
return self::getOssDomain() . '/' . $object;
}
/**
* CDN域名转 OSS域名
* @param string $url
* @param bool $internal
* @return string
*/
public static function toEndpointUrl(string $url, bool $internal = false): string
{
return Str::replace(self::getOssDomain(), $internal ? self::getOssInternal(true) : self::getOssEndpoint(false, true), $url);
}
}