first commit
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service\storage\engine;
|
||||
|
||||
use OSS\OssClient;
|
||||
use OSS\Core\OssException;
|
||||
|
||||
/**
|
||||
* 阿里云存储引擎 (OSS)
|
||||
* Class Qiniu
|
||||
* @package app\common\library\storage\engine
|
||||
*/
|
||||
class Aliyun extends Server
|
||||
{
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
* Aliyun constructor.
|
||||
* @param $config
|
||||
*/
|
||||
public function __construct($config)
|
||||
{
|
||||
parent::__construct();
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行上传
|
||||
* @param $save_dir (保存路径)
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function upload($save_dir)
|
||||
{
|
||||
try {
|
||||
$ossClient = new OssClient(
|
||||
$this->config['access_key'],
|
||||
$this->config['secret_key'],
|
||||
$this->config['domain'],
|
||||
true
|
||||
);
|
||||
$ossClient->uploadFile(
|
||||
$this->config['bucket'],
|
||||
$save_dir . '/' . $this->fileName,
|
||||
$this->getRealPath()
|
||||
);
|
||||
} catch (OssException $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 抓取远程资源
|
||||
* @param $url
|
||||
* @param null $key
|
||||
* @return mixed|void
|
||||
* @author 张无忌(2021/3/2 14:36)
|
||||
*/
|
||||
public function fetch($url, $key = null)
|
||||
{
|
||||
try {
|
||||
$ossClient = new OssClient(
|
||||
$this->config['access_key'],
|
||||
$this->config['secret_key'],
|
||||
$this->config['domain'],
|
||||
true
|
||||
);
|
||||
|
||||
$content = file_get_contents($url);
|
||||
$ossClient->putObject(
|
||||
$this->config['bucket'],
|
||||
$key,
|
||||
$content
|
||||
);
|
||||
} catch (OssException $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param $fileName
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function delete($fileName)
|
||||
{
|
||||
try {
|
||||
$ossClient = new OssClient(
|
||||
$this->config['access_key'],
|
||||
$this->config['secret_key'],
|
||||
$this->config['domain'],
|
||||
true
|
||||
);
|
||||
$ossClient->deleteObject($this->config['bucket'], $fileName);
|
||||
} catch (OssException $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回文件路径
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFileName()
|
||||
{
|
||||
return $this->fileName;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service\storage\engine;
|
||||
|
||||
|
||||
/**
|
||||
* 本地文件驱动
|
||||
* Class Local
|
||||
* @package app\common\library\storage\drivers
|
||||
*/
|
||||
class Local extends Server
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传
|
||||
* @param $save_dir (保存路径)
|
||||
* @return bool
|
||||
*/
|
||||
public function upload($save_dir)
|
||||
{
|
||||
// 验证文件并上传
|
||||
$info = $this->file->move($save_dir, $this->fileName);
|
||||
if (empty($info)) {
|
||||
$this->error = $this->file->getError();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function fetch($url, $key=null) {}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param $fileName
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function delete($fileName)
|
||||
{
|
||||
$check = strpos($fileName, '/');
|
||||
if ($check !== false && $check == 0) {
|
||||
// 文件所在目录
|
||||
$fileName = substr_replace($fileName,"",0,1);
|
||||
}
|
||||
$filePath = public_path() . "{$fileName}";
|
||||
return !file_exists($filePath) ?: unlink($filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回文件路径
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFileName()
|
||||
{
|
||||
return $this->fileName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service\storage\engine;
|
||||
|
||||
use Exception;
|
||||
use Qcloud\Cos\Client;
|
||||
use QCloud\COSSTS\Sts;
|
||||
|
||||
/**
|
||||
* 腾讯云存储引擎 (COS)
|
||||
* Class Qiniu
|
||||
* @package app\common\library\storage\engine
|
||||
*/
|
||||
class Qcloud extends Server
|
||||
{
|
||||
private $config;
|
||||
private $cosClient;
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
* Qcloud constructor.
|
||||
* @param $config
|
||||
*/
|
||||
public function __construct($config)
|
||||
{
|
||||
parent::__construct();
|
||||
$this->config = $config;
|
||||
// 创建COS控制类
|
||||
$this->createCosClient();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建COS控制类
|
||||
*/
|
||||
private function createCosClient()
|
||||
{
|
||||
$this->cosClient = new Client([
|
||||
'region' => $this->config['region'],
|
||||
'credentials' => [
|
||||
'secretId' => $this->config['access_key'],
|
||||
'secretKey' => $this->config['secret_key'],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行上传
|
||||
* @param $save_dir (保存路径)
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function upload($save_dir)
|
||||
{
|
||||
// 上传文件
|
||||
// putObject(上传接口,最大支持上传5G文件)
|
||||
try {
|
||||
$result = $this->cosClient->putObject([
|
||||
'Bucket' => $this->config['bucket'],
|
||||
'Key' => $save_dir . '/' . $this->fileName,
|
||||
'Body' => fopen($this->getRealPath(), 'rb')
|
||||
]);
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* notes: 抓取远程资源(最大支持上传5G文件)
|
||||
* @param $url
|
||||
* @param null $key
|
||||
* @author 张无忌(2021/3/2 14:36)
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function fetch($url, $key=null) {
|
||||
try {
|
||||
$this->cosClient->putObject([
|
||||
'Bucket' => $this->config['bucket'],
|
||||
'Key' => $key,
|
||||
'Body' => fopen($url, 'rb')
|
||||
]);
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param $fileName
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function delete($fileName)
|
||||
{
|
||||
try {
|
||||
$this->cosClient->deleteObject(array(
|
||||
'Bucket' => $this->config['bucket'],
|
||||
'Key' => $fileName
|
||||
));
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回文件路径
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFileName()
|
||||
{
|
||||
return $this->fileName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 获取 STS 临时凭证(用于浏览器直传)
|
||||
* @param string $keyPrefix 资源前缀,如 uploads/video/20260508/
|
||||
* @param int $maxSizeBytes 单文件大小上限(字节)
|
||||
* @param int $durationSeconds 凭证有效期(秒)
|
||||
* @return array {credentials, expiredTime, requestId}
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getStsCredentials(string $keyPrefix, int $maxSizeBytes, int $durationSeconds = 1800): array
|
||||
{
|
||||
$bucket = $this->config['bucket'];
|
||||
// bucket 形如 likeadmin-1300000000,appId 即末段
|
||||
$appId = '';
|
||||
if (strpos($bucket, '-') !== false) {
|
||||
$parts = explode('-', $bucket);
|
||||
$appId = end($parts);
|
||||
}
|
||||
if (!$appId || !ctype_digit($appId)) {
|
||||
throw new Exception('腾讯云 COS bucket 名称不合法,无法解析 appId(期望形如 name-1300000000)');
|
||||
}
|
||||
|
||||
$shortBucket = substr($bucket, 0, strrpos($bucket, '-'));
|
||||
$region = $this->config['region'];
|
||||
$prefix = ltrim($keyPrefix, '/');
|
||||
if ($prefix === '' || substr($prefix, -1) !== '/') {
|
||||
$prefix = $prefix . '/';
|
||||
}
|
||||
|
||||
$duration = max(900, min($durationSeconds, 7200));
|
||||
|
||||
// 自行构造 policy:对象级写动作收紧 + bucket 级 ListMultipartUploads(cos-js-sdk-v5 续传探测必需)
|
||||
$objectArn = sprintf('qcs::cos:%s:uid/%s:%s/%s*', $region, $appId, $bucket, $prefix);
|
||||
$bucketArn = sprintf('qcs::cos:%s:uid/%s:%s/*', $region, $appId, $bucket);
|
||||
|
||||
$policy = [
|
||||
'version' => '2.0',
|
||||
'statement' => [
|
||||
[
|
||||
'effect' => 'allow',
|
||||
'action' => [
|
||||
'name/cos:PostObject',
|
||||
'name/cos:PutObject',
|
||||
'name/cos:InitiateMultipartUpload',
|
||||
'name/cos:UploadPart',
|
||||
'name/cos:CompleteMultipartUpload',
|
||||
'name/cos:AbortMultipartUpload',
|
||||
'name/cos:ListParts',
|
||||
],
|
||||
'resource' => [$objectArn],
|
||||
'condition' => [
|
||||
'numeric_less_than_equal' => [
|
||||
'cos:content-length' => $maxSizeBytes,
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
// ListMultipartUploads 是 bucket 级操作,无法用对象级 ARN 命中
|
||||
'effect' => 'allow',
|
||||
'action' => ['name/cos:ListMultipartUploads'],
|
||||
'resource' => [$bucketArn],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$config = [
|
||||
'url' => 'https://sts.tencentcloudapi.com/',
|
||||
'domain' => 'sts.tencentcloudapi.com',
|
||||
'proxy' => '',
|
||||
'secretId' => $this->config['access_key'],
|
||||
'secretKey' => $this->config['secret_key'],
|
||||
'bucket' => $bucket,
|
||||
'region' => $region,
|
||||
'durationSeconds' => $duration,
|
||||
'policy' => $policy,
|
||||
];
|
||||
|
||||
$sts = new Sts();
|
||||
$result = $sts->getTempKeys($config);
|
||||
if (!is_array($result) || empty($result['credentials'])) {
|
||||
throw new Exception('获取 STS 临时凭证失败');
|
||||
}
|
||||
|
||||
return [
|
||||
'credentials' => [
|
||||
'tmpSecretId' => $result['credentials']['tmpSecretId'] ?? '',
|
||||
'tmpSecretKey' => $result['credentials']['tmpSecretKey'] ?? '',
|
||||
'sessionToken' => $result['credentials']['sessionToken'] ?? '',
|
||||
],
|
||||
'expiredTime' => (int)($result['expiredTime'] ?? (time() + $duration)),
|
||||
'startTime' => (int)($result['startTime'] ?? time()),
|
||||
'bucket' => $bucket,
|
||||
'shortBucket' => $shortBucket,
|
||||
'region' => $region,
|
||||
'appId' => $appId,
|
||||
'host' => sprintf('https://%s.cos.%s.myqcloud.com', $bucket, $region),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes HEAD 校验对象是否真实存在并返回 size / contentType
|
||||
* @param string $key
|
||||
* @return array|false
|
||||
*/
|
||||
public function headObject(string $key)
|
||||
{
|
||||
try {
|
||||
$result = $this->cosClient->headObject([
|
||||
'Bucket' => $this->config['bucket'],
|
||||
'Key' => $key,
|
||||
]);
|
||||
return [
|
||||
'size' => (int)($result['ContentLength'] ?? 0),
|
||||
'content_type' => (string)($result['ContentType'] ?? ''),
|
||||
'etag' => trim((string)($result['ETag'] ?? ''), '"'),
|
||||
];
|
||||
} catch (Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service\storage\engine;
|
||||
|
||||
use Exception;
|
||||
use Qiniu\Auth;
|
||||
use Qiniu\Storage\UploadManager;
|
||||
use Qiniu\Storage\BucketManager;
|
||||
|
||||
/**
|
||||
* 七牛云存储引擎
|
||||
* Class Qiniu
|
||||
* @package app\common\library\storage\engine
|
||||
*/
|
||||
class Qiniu extends Server
|
||||
{
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
* Qiniu constructor.
|
||||
* @param $config
|
||||
*/
|
||||
public function __construct($config)
|
||||
{
|
||||
parent::__construct();
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 执行上传
|
||||
* @param $save_dir
|
||||
* @return bool|mixed
|
||||
* @author 张无忌
|
||||
* @date 2021/7/27 16:02
|
||||
*/
|
||||
public function upload($save_dir)
|
||||
{
|
||||
// 要上传图片的本地路径
|
||||
$realPath = $this->getRealPath();
|
||||
|
||||
// 构建鉴权对象
|
||||
$auth = new Auth($this->config['access_key'], $this->config['secret_key']);
|
||||
|
||||
// 要上传的空间
|
||||
$token = $auth->uploadToken($this->config['bucket']);
|
||||
|
||||
// 初始化 UploadManager 对象并进行文件的上传
|
||||
$uploadMgr = new UploadManager();
|
||||
|
||||
try {
|
||||
// 调用 UploadManager 的 putFile 方法进行文件的上传
|
||||
$key = $save_dir . '/' . $this->fileName;
|
||||
list(, $error) = $uploadMgr->putFile($token, $key, $realPath);
|
||||
|
||||
if ($error !== null) {
|
||||
$this->error = $error->message();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 抓取远程资源
|
||||
* @param $url
|
||||
* @param null $key
|
||||
* @return bool|mixed
|
||||
* @author 张无忌
|
||||
* @date 2021/7/27 16:02
|
||||
*/
|
||||
public function fetch($url, $key=null)
|
||||
{
|
||||
try {
|
||||
if (substr($url, 0, 1) !== '/' || strstr($url, 'http://') || strstr($url, 'https://')) {
|
||||
$auth = new Auth($this->config['access_key'], $this->config['secret_key']);
|
||||
$bucketManager = new BucketManager($auth);
|
||||
list(, $err) = $bucketManager->fetch($url, $this->config['bucket'], $key);
|
||||
} else {
|
||||
$auth = new Auth($this->config['access_key'], $this->config['secret_key']);
|
||||
$token = $auth->uploadToken($this->config['bucket']);
|
||||
$uploadMgr = new UploadManager();
|
||||
list(, $err) = $uploadMgr->putFile($token, $key, $url);
|
||||
}
|
||||
|
||||
if ($err !== null) {
|
||||
$this->error = $err->message();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 删除文件
|
||||
* @param $fileName
|
||||
* @return bool|mixed
|
||||
* @author 张无忌
|
||||
* @date 2021/7/27 16:02
|
||||
*/
|
||||
public function delete($fileName)
|
||||
{
|
||||
// 构建鉴权对象
|
||||
$auth = new Auth($this->config['access_key'], $this->config['secret_key']);
|
||||
// 初始化 UploadManager 对象并进行文件的上传
|
||||
$bucketMgr = new BucketManager($auth);
|
||||
|
||||
try {
|
||||
list($res, $error) = $bucketMgr->delete($this->config['bucket'], $fileName);
|
||||
if ($error !== null) {
|
||||
$this->error = $error->message();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回文件路径
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFileName()
|
||||
{
|
||||
return $this->fileName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service\storage\engine;
|
||||
|
||||
use think\Request;
|
||||
use think\Exception;
|
||||
|
||||
/**
|
||||
* 存储引擎抽象类
|
||||
* Class server
|
||||
* @package app\common\library\storage\drivers
|
||||
*/
|
||||
abstract class Server
|
||||
{
|
||||
protected $file;
|
||||
protected $error;
|
||||
protected $fileName;
|
||||
protected $fileInfo;
|
||||
|
||||
// 是否为内部上传
|
||||
protected $isInternal = false;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* Server constructor.
|
||||
*/
|
||||
protected function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置上传的文件信息
|
||||
* @param string $name
|
||||
* @throws Exception
|
||||
*/
|
||||
public function setUploadFile($name)
|
||||
{
|
||||
// 接收上传的文件
|
||||
$this->file = request()->file($name);
|
||||
if (empty($this->file)) {
|
||||
throw new Exception('未找到上传文件的信息');
|
||||
}
|
||||
|
||||
// 校验上传文件后缀
|
||||
$limit = array_merge(config('project.file_image'), config('project.file_video'), config('project.file_file'));
|
||||
if (!in_array(strtolower($this->file->extension()), $limit)) {
|
||||
throw new Exception('不允许上传' . $this->file->extension() . '后缀文件');
|
||||
}
|
||||
|
||||
// 文件信息
|
||||
$this->fileInfo = [
|
||||
'ext' => $this->file->extension(),
|
||||
'size' => $this->file->getSize(),
|
||||
'mime' => $this->file->getMime(),
|
||||
'name' => $this->file->getOriginalName(),
|
||||
'realPath' => $this->file->getRealPath(),
|
||||
];
|
||||
// 生成保存文件名
|
||||
$this->fileName = $this->buildSaveName();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置上传的文件信息
|
||||
* @param string $filePath
|
||||
*/
|
||||
public function setUploadFileByReal($filePath)
|
||||
{
|
||||
// 设置为系统内部上传
|
||||
$this->isInternal = true;
|
||||
// 文件信息
|
||||
$this->fileInfo = [
|
||||
'name' => basename($filePath),
|
||||
'size' => filesize($filePath),
|
||||
'tmp_name' => $filePath,
|
||||
'error' => 0,
|
||||
];
|
||||
// 生成保存文件名
|
||||
$this->fileName = $this->buildSaveName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 抓取网络资源
|
||||
* @param $url
|
||||
* @param $key
|
||||
* @author 张无忌(2021/3/2 14:15)
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function fetch($url, $key);
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
* @param $save_dir (保存路径)
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function upload($save_dir);
|
||||
|
||||
/**
|
||||
* 文件删除
|
||||
* @param $fileName
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function delete($fileName);
|
||||
|
||||
/**
|
||||
* 返回上传后文件路径
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function getFileName();
|
||||
|
||||
/**
|
||||
* 返回文件信息
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFileInfo()
|
||||
{
|
||||
return $this->fileInfo;
|
||||
}
|
||||
|
||||
protected function getRealPath()
|
||||
{
|
||||
return $this->fileInfo['realPath'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回错误信息
|
||||
* @return mixed
|
||||
*/
|
||||
public function getError()
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成保存文件名
|
||||
*/
|
||||
private function buildSaveName()
|
||||
{
|
||||
// 要上传图片的本地路径
|
||||
$realPath = $this->getRealPath();
|
||||
// 扩展名
|
||||
$ext = pathinfo($this->getFileInfo()['name'], PATHINFO_EXTENSION);
|
||||
// 自动生成文件名
|
||||
return date('YmdHis') . substr(md5($realPath), 0, 5)
|
||||
. str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT) . ".{$ext}";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user