2020-01-25 22:22:56 +08:00
|
|
|
<?php
|
|
|
|
namespace catcher;
|
|
|
|
|
|
|
|
use catchAdmin\system\model\Attachments;
|
2020-04-21 17:07:19 +08:00
|
|
|
use catchAdmin\system\model\Config;
|
2020-02-02 22:10:14 +08:00
|
|
|
use catcher\exceptions\FailedException;
|
|
|
|
use catcher\exceptions\ValidateFailedException;
|
|
|
|
use think\exception\ValidateException;
|
2020-01-25 22:22:56 +08:00
|
|
|
use think\facade\Filesystem;
|
|
|
|
use think\file\UploadedFile;
|
|
|
|
|
|
|
|
class CatchUpload
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 阿里云
|
|
|
|
*/
|
|
|
|
public const OSS = 'oss';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 腾讯云
|
|
|
|
*/
|
|
|
|
public const QCLOUD = 'qcloud';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 七牛
|
|
|
|
*/
|
|
|
|
public const QIQNIU = 'qiniu';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 本地
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $driver = 'local';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 本地
|
|
|
|
*/
|
|
|
|
public const LOCAL = 'local';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* path
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $path = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* upload files
|
|
|
|
*
|
|
|
|
* @param UploadedFile $file
|
2020-02-02 22:10:14 +08:00
|
|
|
* @return string
|
2020-01-25 22:22:56 +08:00
|
|
|
* @author JaguarJack
|
|
|
|
* @email njphper@gmail.com
|
|
|
|
* @time 2020/1/25
|
|
|
|
*/
|
2020-02-02 22:10:14 +08:00
|
|
|
public function upload(UploadedFile $file): string
|
2020-01-25 22:22:56 +08:00
|
|
|
{
|
2020-04-21 17:07:19 +08:00
|
|
|
$this->initUploadConfig();
|
|
|
|
|
2020-01-25 22:22:56 +08:00
|
|
|
$path = Filesystem::disk($this->getDriver())->putFile($this->getPath(), $file);
|
|
|
|
|
|
|
|
if ($path) {
|
2020-04-29 09:02:43 +08:00
|
|
|
$url = self::getCloudDomain($this->getDriver()) . $path;
|
2020-02-02 22:10:14 +08:00
|
|
|
|
2020-06-01 21:25:46 +08:00
|
|
|
event('attachment', [
|
2020-02-02 22:10:14 +08:00
|
|
|
'path' => $path,
|
2020-06-01 21:25:46 +08:00
|
|
|
'url' => $url,
|
|
|
|
'driver' => $this->getDriver(),
|
|
|
|
'file' => $file,
|
|
|
|
]);
|
2020-02-02 22:10:14 +08:00
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new FailedException('Upload Failed, Try Again!');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 多文件上传
|
|
|
|
*
|
|
|
|
* @author JaguarJack
|
|
|
|
* @email njphper@gmail.com
|
|
|
|
* @time 2020/2/1
|
|
|
|
* @param $attachments
|
|
|
|
* @return array|string
|
|
|
|
*/
|
|
|
|
public function multiUpload($attachments)
|
|
|
|
{
|
|
|
|
if (!is_array($attachments)) {
|
|
|
|
return $this->upload($attachments);
|
|
|
|
}
|
|
|
|
|
|
|
|
$paths = [];
|
|
|
|
foreach ($attachments as $attachment) {
|
|
|
|
$paths[] = $this->upload($attachment);
|
2020-01-25 22:22:56 +08:00
|
|
|
}
|
|
|
|
|
2020-02-02 22:10:14 +08:00
|
|
|
return $paths;
|
2020-01-25 22:22:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get upload driver
|
|
|
|
*
|
|
|
|
* @author JaguarJack
|
|
|
|
* @email njphper@gmail.com
|
|
|
|
* @time 2020/1/25
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getDriver(): string
|
|
|
|
{
|
|
|
|
return $this->driver;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set driver
|
|
|
|
*
|
|
|
|
* @author JaguarJack
|
|
|
|
* @email njphper@gmail.com
|
|
|
|
* @time 2020/1/25
|
|
|
|
* @param $driver
|
|
|
|
* @throws \Exception
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setDriver($driver): self
|
|
|
|
{
|
|
|
|
if (!in_array($driver, [self::OSS, self::QCLOUD, self::QIQNIU, self::LOCAL])) {
|
|
|
|
throw new \Exception(sprintf('Upload Driver [%s] Not Supported', $driver));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->driver = $driver;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author JaguarJack
|
|
|
|
* @email njphper@gmail.com
|
|
|
|
* @time 2020/1/25
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getPath()
|
|
|
|
{
|
|
|
|
return $this->path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author JaguarJack
|
|
|
|
* @email njphper@gmail.com
|
|
|
|
* @time 2020/1/25
|
|
|
|
* @param string $path
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setPath(string $path)
|
|
|
|
{
|
|
|
|
$this->path = $path;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2020年01月25日
|
|
|
|
* @param UploadedFile $file
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function data(UploadedFile $file)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'file_size' => $file->getSize(),
|
|
|
|
'mime_type' => $file->getMime(),
|
2020-02-02 22:10:14 +08:00
|
|
|
'file_ext' => $file->getOriginalExtension(),
|
2020-01-25 22:22:56 +08:00
|
|
|
'filename' => $file->getOriginalName(),
|
|
|
|
'driver' => $this->getDriver(),
|
|
|
|
];
|
|
|
|
}
|
2020-02-02 22:10:14 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 验证图片
|
|
|
|
*
|
|
|
|
* @author JaguarJack
|
|
|
|
* @email njphper@gmail.com
|
|
|
|
* @time 2020/2/1
|
|
|
|
* @param array $images
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function checkImages(array $images)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
validate(['image' => config('catch.upload.image')])->check($images);
|
|
|
|
} catch (ValidateException $e) {
|
|
|
|
throw new ValidateFailedException($e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 验证文件
|
|
|
|
*
|
|
|
|
* @author JaguarJack
|
|
|
|
* @email njphper@gmail.com
|
|
|
|
* @time 2020/2/1
|
|
|
|
* @param array $files
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function checkFiles(array $files)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
validate(['file' => config('catch.upload.file')])->check($files);
|
|
|
|
} catch (ValidateException $e) {
|
|
|
|
throw new ValidateFailedException($e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2020-04-21 17:07:19 +08:00
|
|
|
|
2020-06-01 21:25:46 +08:00
|
|
|
/**
|
|
|
|
* 初始化配置
|
|
|
|
*
|
|
|
|
* @time 2020年06月01日
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-04-21 17:07:19 +08:00
|
|
|
protected function initUploadConfig()
|
|
|
|
{
|
|
|
|
$configModel = app(Config::class);
|
|
|
|
|
|
|
|
$upload = $configModel->where('key', 'upload')->find();
|
|
|
|
|
|
|
|
if ($upload) {
|
|
|
|
$disk = app()->config->get('filesystem.disks');
|
2020-06-01 21:25:46 +08:00
|
|
|
|
2020-04-21 17:07:19 +08:00
|
|
|
$uploadConfigs = $configModel->getConfig($upload->id);
|
2020-06-01 21:25:46 +08:00
|
|
|
if (!empty($uploadConfigs)) {
|
|
|
|
// 读取上传可配置数据
|
|
|
|
foreach ($uploadConfigs as $key => &$config) {
|
|
|
|
// $disk[$key]['type'] = $key;
|
|
|
|
// 腾讯云配置处理
|
|
|
|
if (strtolower($key) == 'qcloud') {
|
|
|
|
$config['credentials'] = [
|
|
|
|
'appId' => $config['app_id'] ?? '',
|
|
|
|
'secretKey' => $config['secret_key'] ?? '',
|
|
|
|
'secretId' => $config['secret_id'] ?? '',
|
|
|
|
];
|
|
|
|
$readFromCdn = $config['read_from_cdn'] ?? false;
|
|
|
|
$config['read_from_cdn'] = $readFromCdn ? true : false;
|
|
|
|
}
|
|
|
|
// OSS 配置
|
|
|
|
if (strtolower($key) == 'oss') {
|
|
|
|
$isCname = $config['is_cname'] ?? false;
|
|
|
|
$config['is_cname'] = $isCname ? true : false;
|
|
|
|
}
|
2020-04-21 17:07:19 +08:00
|
|
|
}
|
|
|
|
|
2020-06-01 21:25:46 +08:00
|
|
|
// 合并数组
|
|
|
|
array_walk($disk, function (&$item, $key) use ($uploadConfigs) {
|
|
|
|
if (!in_array($key, ['public', 'local'])) {
|
|
|
|
if ($uploadConfigs[$key] ?? false) {
|
|
|
|
foreach ($uploadConfigs[$key] as $k => $value) {
|
|
|
|
$item[$k] = $value;
|
|
|
|
}
|
2020-04-21 17:07:19 +08:00
|
|
|
}
|
|
|
|
}
|
2020-06-01 21:25:46 +08:00
|
|
|
});
|
|
|
|
// 重新分配配置
|
|
|
|
app()->config->set([
|
|
|
|
'disk' => $disk,
|
|
|
|
], 'filesystem');
|
|
|
|
}
|
2020-04-21 17:07:19 +08:00
|
|
|
}
|
|
|
|
}
|
2020-04-29 09:02:43 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取云存储的域名
|
|
|
|
*
|
|
|
|
* @time 2020年01月25日
|
|
|
|
* @param $driver
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getCloudDomain($driver): ?string
|
|
|
|
{
|
|
|
|
$driver = \config('filesystem.disks.' . $driver);
|
|
|
|
|
|
|
|
switch ($driver['type']) {
|
|
|
|
case CatchUpload::QIQNIU:
|
|
|
|
case CatchUpload::LOCAL:
|
|
|
|
return $driver['domain'];
|
|
|
|
case CatchUpload::OSS:
|
|
|
|
return $driver['end_point'];
|
|
|
|
case CatchUpload::QCLOUD:
|
|
|
|
return $driver['cdn'];
|
|
|
|
default:
|
|
|
|
throw new FailedException(sprintf('Driver [%s] Not Supported.', $driver));
|
|
|
|
}
|
|
|
|
}
|
2020-01-25 22:22:56 +08:00
|
|
|
}
|