Files
hdk-core/src/Service/OssService.php
李东云 8616c5f61c perf: 适配已弃用的命名空间
Signed-off-by: 李东云 <dongyu.li@luxcreo.ai>
2023-06-19 18:22:42 +08:00

148 lines
4.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* OssService.php@LuxStudio
*
* @author 李东云<dongyun.li@luxcreo.cn>
* Powered by PhpStorm
* Created on 2022/5/11
*/
namespace Singularity\HDK\Core\Service;
use Hyperf\Codec\Json;
use function Hyperf\Config\config;
class OssService
{
/**
* @param string|null $accessKeyId 不传则取配置文件
* @param string|null $accessKeySecret 不传则取配置文件
* @param string|null $host 不传则取配置文件
* @param int|null $expiration 过期时间默认30s
*/
public function __construct(
public ?string $accessKeyId = null,
public ?string $accessKeySecret = null,
public ?string $host = null,
public ?string $callbackUrl = null,
public ?int $expiration = 30
) {
$this->accessKeyId ??= config('common.third_party.storage.oss.access_key_id');
$this->accessKeySecret ??= config('common.third_party.storage.oss.access_key_secret');
$this->host ??= config('common.third_party.storage.oss.oss_host');
$this->callbackUrl ??= config('common.third_party.storage.oss.oss_callback') ?? null;
}
/**
* 生成签证
*
* @param string|null $dir
* @param bool $isImage
* @param int $maxFileSize 最大文件大小单位b
*
* @return array{
* 'accessid' : string,
* 'host' : string,
* 'policy': string,
* 'signature': string,
* 'expire' : int,
* 'dir' : string,
* 'callback':string
* }
*/
public function generatePolicy(
string $dir = null,
bool $isImage = false,
int $maxFileSize = 1048576000
): array {
$expire_time = time() + $this->expiration;
$expiration = $this->gmtIso8601($expire_time);
//最大文件大小.用户可以自己设置
$condition = [
0 => 'Content-Length-Range',
1 => 0,
2 => $maxFileSize,
];
$conditions[] = $condition;
// 表示用户上传的数据,必须是以$dir开始不然上传会失败这一步不是必须项只是为了安全起见防止用户通过policy上传到别人的目录。
if ($dir) {
$start = [
0 => 'starts-with',
1 => '$key',
2 => $dir,
];
$conditions[] = $start;
}
$policy = Json::encode([
'expiration' => $expiration,
'conditions' => $conditions,
]);
$base64_policy = base64_encode($policy);
$string_to_sign = $base64_policy;
$signature = base64_encode(hash_hmac('sha1', $string_to_sign, $this->accessKeySecret, true));
$callback = isset($this->callbackUrl) ? Json::encode([
'callbackUrl' => $this->callbackUrl,
'callbackBody' => $isImage ? <<<'callbackBody'
{
"bucket": ${bucket},
"object": ${object},
"etag": ${etag},
"size": ${size},
"mimeType": ${mimeType},
"imageInfo": {
"height": ${imageInfo.height},
"width": ${imageInfo.width},
"format": ${imageInfo.format}
},
"uid": ${x:uid},
"name": ${x:name},
"hash": ${x:hash}
}
callbackBody : <<<'callbackBody'
{
"bucket": ${bucket},
"object": ${object},
"etag": ${etag},
"size": ${size},
"mimeType": ${mimeType},
"uid": ${x:uid},
"name": ${x:name},
"hash": ${x:hash}
}
callbackBody,
'callbackBodyType' => 'application/json',
]) : null;
$result = [
'accessid' => $this->accessKeyId,
'host' => $this->host,
'policy' => $base64_policy,
'signature' => $signature,
'expire' => $expire_time,
'dir' => $dir,
];
if (isset($callback)) {
$result += ['callback' => base64_encode($callback),];
}
return $result;
}
/**
* 将时间戳转化为 ISO8601 格式
*
* @param int $time
*
* @return string
*/
protected function gmtIso8601(int $time): string
{
return str_replace('+00:00', '.000Z', gmdate('c', $time));
}
}