mirror of
http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore.git
synced 2026-01-15 07:15:06 +08:00
148 lines
4.1 KiB
PHP
148 lines
4.1 KiB
PHP
<?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));
|
||
}
|
||
}
|