初始化项目
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
<?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 OSS\OssClient;
|
||||
use OSS\Core\OssException;
|
||||
|
||||
/**
|
||||
* 阿里云存储引擎 (OSS)
|
||||
* Class Aliyun
|
||||
* @package app\common\library\storage\engine
|
||||
*/
|
||||
class Aliyun extends Basics
|
||||
{
|
||||
/**
|
||||
* 执行上传
|
||||
* @return bool
|
||||
*/
|
||||
public function upload(): bool
|
||||
{
|
||||
try {
|
||||
$ossClient = new OssClient(
|
||||
$this->config['access_key_id'],
|
||||
$this->config['access_key_secret'],
|
||||
$this->config['domain'],
|
||||
true
|
||||
);
|
||||
$result = $ossClient->uploadFile(
|
||||
$this->config['bucket'],
|
||||
$this->getSaveFileInfo()['file_path'],
|
||||
$this->getRealPath()
|
||||
);
|
||||
} catch (OssException $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param string $filePath
|
||||
* @return bool
|
||||
*/
|
||||
public function delete(string $filePath): bool
|
||||
{
|
||||
try {
|
||||
$ossClient = new OssClient(
|
||||
$this->config['access_key_id'],
|
||||
$this->config['access_key_secret'],
|
||||
$this->config['domain'],
|
||||
true
|
||||
);
|
||||
$ossClient->deleteObject($this->config['bucket'], $filePath);
|
||||
} catch (OssException $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,239 @@
|
||||
<?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 Exception;
|
||||
use think\facade\Request;
|
||||
use think\file\UploadedFile;
|
||||
use cores\traits\ErrorTrait;
|
||||
use app\common\exception\BaseException;
|
||||
|
||||
/**
|
||||
* 存储引擎抽象类
|
||||
* Class Basics
|
||||
* @package app\common\library\storage\drivers
|
||||
*/
|
||||
abstract class Basics
|
||||
{
|
||||
use ErrorTrait;
|
||||
|
||||
// 当前存储引擎
|
||||
protected $storage;
|
||||
|
||||
// 存储配置
|
||||
protected $config;
|
||||
|
||||
// file对象句柄
|
||||
/* @var $file UploadedFile */
|
||||
protected $file;
|
||||
|
||||
// 验证规则
|
||||
protected $validateRuleScene;
|
||||
|
||||
// 磁盘配置
|
||||
protected $disk = 'public';
|
||||
|
||||
// 保存的根文件夹名称
|
||||
protected $rootDirName;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* Server constructor.
|
||||
* @param string $storage 存储方式
|
||||
* @param array|null $config 存储配置
|
||||
*/
|
||||
public function __construct(string $storage, array $config = null)
|
||||
{
|
||||
$this->storage = $storage;
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置上传的文件信息 (外部用户上传)
|
||||
* @param string $name
|
||||
* @return $this
|
||||
* @throws \cores\exception\BaseException
|
||||
*/
|
||||
public function setUploadFile(string $name): Basics
|
||||
{
|
||||
// 接收上传的文件
|
||||
try {
|
||||
$this->file = Request::file($name);
|
||||
} catch (Exception $e) {
|
||||
$this->throwFileError($e);
|
||||
}
|
||||
empty($this->file) && throwError('未找到上传文件的信息');
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件异常处理
|
||||
* @param Exception $e
|
||||
* @throws \cores\exception\BaseException
|
||||
*/
|
||||
private function throwFileError(Exception $e)
|
||||
{
|
||||
$maxSize = ini_get('upload_max_filesize');
|
||||
$myMsg = $e->getCode() === 1 ? "上传的文件超出了服务器最大限制: {$maxSize};可修改php.ini文件中upload_max_filesize项调整" : false;
|
||||
throwError($myMsg ?: $e->getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置上传的文件信息 (系统内部上传)
|
||||
* @param string $filePath 文件路径
|
||||
* @return $this
|
||||
* @throws \cores\exception\BaseException
|
||||
*/
|
||||
public function setUploadFileByReal(string $filePath): Basics
|
||||
{
|
||||
// 接收上传的文件
|
||||
$this->file = new UploadedFile($filePath, basename($filePath));
|
||||
if (empty($this->file)) {
|
||||
throwError('未找到上传文件的信息');
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置上传文件的验证规则
|
||||
* @param string $scene
|
||||
* @return Basics
|
||||
*/
|
||||
public function setValidationScene(string $scene = ''): Basics
|
||||
{
|
||||
$this->validateRuleScene = $scene;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置磁盘配置
|
||||
* @param string $disk
|
||||
* @return $this
|
||||
*/
|
||||
public function setDisk(string $disk): Basics
|
||||
{
|
||||
$this->disk = $disk;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置上传的文件根目录名称
|
||||
* [通常是商城的id, 例如: 10001]
|
||||
* @param string $name
|
||||
* @return $this
|
||||
*/
|
||||
public function setRootDirName(string $name): Basics
|
||||
{
|
||||
$this->rootDirName = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function upload();
|
||||
|
||||
/**
|
||||
* 文件删除
|
||||
* @param string $filePath
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function delete(string $filePath);
|
||||
|
||||
/**
|
||||
* 临时文件的绝对路径
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getRealPath(): string
|
||||
{
|
||||
return $this->file->getRealPath();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回错误信息
|
||||
* @return mixed
|
||||
*/
|
||||
public function getError(): string
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成保存的文件信息
|
||||
* @return array
|
||||
*/
|
||||
public function getSaveFileInfo(): array
|
||||
{
|
||||
// 自动生成的文件名称
|
||||
//$hashName = $this->file->hashName(null);
|
||||
$hashName = $this->hashName();
|
||||
// 存储目录
|
||||
$filePath = $this->getFilePath($hashName);
|
||||
// 文件名称
|
||||
// 去除扩展名的写法 stristr($this->file->getOriginalName(), '.', true)
|
||||
$fileName = $this->file->getOriginalName();
|
||||
// 文件扩展名
|
||||
$fileExt = strtolower($this->file->extension());
|
||||
return [
|
||||
'storage' => $this->storage, // 存储方式
|
||||
'domain' => $this->config['domain'] ?? '', // 存储域名
|
||||
'file_path' => $filePath, // 文件路径
|
||||
'file_name' => $fileName, // 文件名称
|
||||
'file_size' => $this->file->getSize(), // 文件大小(字节)
|
||||
'file_ext' => $fileExt, // 文件扩展名
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动生成文件名
|
||||
* @return string
|
||||
*/
|
||||
private function hashName(): string
|
||||
{
|
||||
return $this->file->hashName(function () {
|
||||
return date('Ymd') . DIRECTORY_SEPARATOR . md5(uniqid((string)mt_rand(), true));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取hashName的路径
|
||||
* @param string $hashName
|
||||
* @return string
|
||||
*/
|
||||
private function getFilePath(string $hashName): string
|
||||
{
|
||||
$filePath = empty($this->rootDirName) ? "{$hashName}" : "{$this->rootDirName}/{$hashName}";
|
||||
return convert_left_slash($filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取hashName的文件名
|
||||
* @param string $filePath
|
||||
* @return string
|
||||
*/
|
||||
protected function getFileHashName(string $filePath): string
|
||||
{
|
||||
return basename($filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取hashName的文件目录
|
||||
* @param string $filePath
|
||||
* @return string
|
||||
*/
|
||||
protected function getFileHashRoute(string $filePath): string
|
||||
{
|
||||
return dirname($filePath);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?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 think\facade\Filesystem;
|
||||
use app\common\library\storage\FileValidate;
|
||||
|
||||
/**
|
||||
* 本地文件驱动
|
||||
* Class Local
|
||||
* @package app\common\library\storage\drivers
|
||||
*/
|
||||
class Local extends Basics
|
||||
{
|
||||
/**
|
||||
* 上传图片文件
|
||||
* @return bool
|
||||
*/
|
||||
public function upload(): bool
|
||||
{
|
||||
// 验证文件类型
|
||||
if (!$this->validate()) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
$filePath = $this->getSaveFileInfo()['file_path'];
|
||||
// 上传到本地服务器
|
||||
$sts = Filesystem::disk($this->disk)->putFileAs(
|
||||
$this->getFileHashRoute($filePath),
|
||||
$this->file,
|
||||
$this->getFileHashName($filePath)
|
||||
);
|
||||
return (bool)$sts;
|
||||
} catch (\Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证上传的文件
|
||||
* @return bool
|
||||
*/
|
||||
private function validate(): bool
|
||||
{
|
||||
$FileValidate = new FileValidate;
|
||||
if (!$FileValidate->check([$this->validateRuleScene => $this->file])) {
|
||||
$this->error = $FileValidate->getError();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param string $filePath
|
||||
* @return bool
|
||||
*/
|
||||
public function delete(string $filePath): bool
|
||||
{
|
||||
// 文件所在目录
|
||||
$realPath = realpath(web_path() . "uploads/{$filePath}");
|
||||
return $realPath === false || unlink($realPath);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?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 Qcloud\Cos\Client;
|
||||
|
||||
/**
|
||||
* 腾讯云存储引擎 (COS)
|
||||
* Class Qcloud
|
||||
* @package app\common\library\storage\engine
|
||||
*/
|
||||
class Qcloud extends Basics
|
||||
{
|
||||
// Qcloud类
|
||||
private $cosClient;
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
* Qcloud constructor.
|
||||
* @param string $storage 存储方式
|
||||
* @param array|null $config 存储配置
|
||||
*/
|
||||
public function __construct(string $storage, array $config = null)
|
||||
{
|
||||
parent::__construct($storage, $config);
|
||||
// 创建Qcloud类
|
||||
$this->createCosClient();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建COS控制类
|
||||
*/
|
||||
private function createCosClient()
|
||||
{
|
||||
$this->cosClient = new Client([
|
||||
'region' => $this->config['region'],
|
||||
'credentials' => [
|
||||
'secretId' => $this->config['secret_id'],
|
||||
'secretKey' => $this->config['secret_key'],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行上传
|
||||
* @return bool
|
||||
*/
|
||||
public function upload(): bool
|
||||
{
|
||||
// 上传文件
|
||||
// putObject(上传接口,最大支持上传5G文件)
|
||||
try {
|
||||
$result = $this->cosClient->putObject([
|
||||
'Bucket' => $this->config['bucket'],
|
||||
'Key' => $this->getSaveFileInfo()['file_path'],
|
||||
'Body' => fopen($this->getRealPath(), 'rb')
|
||||
]);
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param string $filePath
|
||||
* @return bool
|
||||
*/
|
||||
public function delete(string $filePath): bool
|
||||
{
|
||||
try {
|
||||
$result = $this->cosClient->deleteObject(array(
|
||||
'Bucket' => $this->config['bucket'],
|
||||
'Key' => $filePath
|
||||
));
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?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|mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function upload()
|
||||
{
|
||||
// 要上传图片的本地路径
|
||||
$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 方法进行文件的上传
|
||||
list(, $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 */
|
||||
list(, $error) = $bucketMgr->delete($this->config['bucket'], $filePath);
|
||||
if ($error !== null) {
|
||||
$this->error = $error->message();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user