初始化仓库

This commit is contained in:
wangxiaowei
2025-04-22 14:09:52 +08:00
commit 8b100110bb
5155 changed files with 664201 additions and 0 deletions

View File

@ -0,0 +1,128 @@
<?php
namespace app\common\service\storage;
use think\Exception;
/**
* 存储模块驱动
* Class driver
* @package app\common\library\storage
*/
class Driver
{
private $config; // upload 配置
private $engine; // 当前存储引擎类
/**
* 构造方法
* Driver constructor.
* @param $config
* @param null|string $storage 指定存储方式,如不指定则为系统默认
* @throws Exception
*/
public function __construct($config, $storage = null)
{
$this->config = $config;
$this->engine = $this->getEngineClass($storage);
}
/**
* 设置上传的文件信息
* @param string $name
* @return mixed
*/
public function setUploadFile($name = 'iFile')
{
return $this->engine->setUploadFile($name);
}
/**
* 设置上传的文件信息
* @param string $filePath
* @return mixed
*/
public function setUploadFileByReal($filePath)
{
return $this->engine->setUploadFileByReal($filePath);
}
/**
* 执行文件上传
* @param $save_dir (保存路径)
* @return mixed
*/
public function upload($save_dir)
{
return $this->engine->upload($save_dir);
}
/**
* Notes: 抓取网络资源
* @param $url
* @param $key
* @author 张无忌(2021/3/2 14:16)
* @return mixed
*/
public function fetch($url, $key) {
return $this->engine->fetch($url, $key);
}
/**
* 执行文件删除
* @param $fileName
* @return mixed
*/
public function delete($fileName)
{
return $this->engine->delete($fileName);
}
/**
* 获取错误信息
* @return mixed
*/
public function getError()
{
return $this->engine->getError();
}
/**
* 获取文件路径
* @return mixed
*/
public function getFileName()
{
return $this->engine->getFileName();
}
/**
* 返回文件信息
* @return mixed
*/
public function getFileInfo()
{
return $this->engine->getFileInfo();
}
/**
* 获取当前的存储引擎
* @param null|string $storage 指定存储方式,如不指定则为系统默认
* @return mixed
* @throws Exception
*/
private function getEngineClass($storage = null)
{
$engineName = is_null($storage) ? $this->config['default'] : $storage;
$classSpace = __NAMESPACE__ . '\\engine\\' . ucfirst($engineName);
if (!class_exists($classSpace)) {
throw new Exception('未找到存储引擎类: ' . $engineName);
}
if($engineName == 'local') {
return new $classSpace();
}
return new $classSpace($this->config['engine'][$engineName]);
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,116 @@
<?php
namespace app\common\service\storage\engine;
use Exception;
use Qcloud\Cos\Client;
/**
* 腾讯云存储引擎 (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;
}
}

View File

@ -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;
}
}

View File

@ -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}";
}
}