You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.4 KiB
72 lines
2.4 KiB
11 months ago
|
<?php
|
||
|
// +----------------------------------------------------------------------
|
||
|
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
|
||
|
// +----------------------------------------------------------------------
|
||
|
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
|
||
|
// +----------------------------------------------------------------------
|
||
|
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
|
||
|
// +----------------------------------------------------------------------
|
||
|
// | Author: 萤火科技 <admin@yiovo.com>
|
||
|
// +----------------------------------------------------------------------
|
||
|
declare (strict_types=1);
|
||
|
|
||
|
namespace app\common\library\storage\engine;
|
||
|
|
||
|
use Qiniu\Auth;
|
||
|
use Qiniu\Http\Error;
|
||
|
use Qiniu\Storage\UploadManager;
|
||
|
use Qiniu\Storage\BucketManager;
|
||
|
|
||
|
/**
|
||
|
* 七牛云存储引擎
|
||
|
* Class Qiniu
|
||
|
* @package app\common\library\storage\engine
|
||
|
*/
|
||
|
class Qiniu extends Basics
|
||
|
{
|
||
|
/**
|
||
|
* 执行上传
|
||
|
* @return bool
|
||
|
* @throws \Exception
|
||
|
*/
|
||
|
public function upload(): bool
|
||
|
{
|
||
|
// 要上传图片的本地路径
|
||
|
$realPath = $this->getRealPath();
|
||
|
// 构建鉴权对象
|
||
|
$auth = new Auth($this->config['access_key'], $this->config['secret_key']);
|
||
|
// 要上传的空间
|
||
|
$token = $auth->uploadToken($this->config['bucket']);
|
||
|
// 初始化 UploadManager 对象并进行文件的上传
|
||
|
$uploadMgr = new UploadManager();
|
||
|
// 调用 UploadManager 的 putFile 方法进行文件的上传
|
||
|
[, $error] = $uploadMgr->putFile($token, $this->getSaveFileInfo()['file_path'], $realPath);
|
||
|
/* @var $error Error */
|
||
|
if ($error !== null) {
|
||
|
$this->error = $error->message();
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 删除文件
|
||
|
* @param string $filePath
|
||
|
* @return bool
|
||
|
*/
|
||
|
public function delete(string $filePath): bool
|
||
|
{
|
||
|
// 构建鉴权对象
|
||
|
$auth = new Auth($this->config['access_key'], $this->config['secret_key']);
|
||
|
// 初始化 UploadManager 对象并进行文件的上传
|
||
|
$bucketMgr = new BucketManager($auth);
|
||
|
/* @var $error Error */
|
||
|
[, $error] = $bucketMgr->delete($this->config['bucket'], $filePath);
|
||
|
if ($error !== null) {
|
||
|
$this->error = $error->message();
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
}
|