初始化本地仓库
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace core\upload;
|
||||
|
||||
use core\upload\UploadFileInterface;
|
||||
use core\exception\handler\UploadException;
|
||||
use Webman\Http\UploadFile;
|
||||
|
||||
abstract class BaseUpload implements UploadFileInterface
|
||||
{
|
||||
|
||||
protected bool $_isFileUpload;
|
||||
protected string $dirSeparator = '/';
|
||||
protected array $files = [];
|
||||
protected array $includes = [];
|
||||
protected array $excludes = [];
|
||||
protected int $singleLimit = 0;
|
||||
protected int $totalLimit = 0;
|
||||
protected int $nums = 0;
|
||||
protected array $config = [];
|
||||
protected string $algo = 'md5';
|
||||
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
$this->loadConfig($config);
|
||||
$this->_isFileUpload = $config['_is_file_upload'] ?? true;
|
||||
|
||||
if ($this->_isFileUpload) {
|
||||
$this->files = request()->file();
|
||||
$this->verify();
|
||||
}
|
||||
}
|
||||
|
||||
abstract function uploadFile(array $options): mixed;
|
||||
|
||||
abstract function uploadServerFile(string $filePath): mixed;
|
||||
|
||||
abstract public function uploadBase64(string $base64, string $extension = 'JPEG'): mixed;
|
||||
|
||||
protected function loadConfig(array $config): void
|
||||
{
|
||||
$this->config = $config;
|
||||
// 处理 root_dir 配置(作为根目录)
|
||||
if (isset($this->config['root_dir']) && is_callable($this->config['root_dir'])) {
|
||||
$this->config['root_dir'] = (string)$this->config['root_dir']() ?: $this->config['root_dir'];
|
||||
}
|
||||
// 处理 dirname 配置(作为根目录,保持兼容)
|
||||
if (isset($this->config['dirname']) && is_callable($this->config['dirname'])) {
|
||||
$this->config['dirname'] = (string)$this->config['dirname']() ?: $this->config['dirname'];
|
||||
}
|
||||
// 处理 sub_dir 配置(作为子目录)
|
||||
if (isset($this->config['sub_dir']) && is_callable($this->config['sub_dir'])) {
|
||||
$this->config['sub_dir'] = (string)$this->config['sub_dir']() ?: $this->config['sub_dir'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件校验
|
||||
*/
|
||||
protected function verify(): void
|
||||
{
|
||||
if (empty($this->files)) {
|
||||
throw new UploadException('未找到符合条件的文件资源');
|
||||
}
|
||||
foreach ($this->files as $file) {
|
||||
if (!$file->isValid()) {
|
||||
throw new UploadException('未选择文件或者无效的文件');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件大小
|
||||
*
|
||||
* @param \Webman\Http\UploadFile $file
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function getSize(UploadFile $file): int
|
||||
{
|
||||
return $file->getSize();
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算文件哈希值
|
||||
*
|
||||
* @param string $pathname
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getUniqueId(string $pathname): string
|
||||
{
|
||||
return hash_file($this->algo, $pathname);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace core\upload;
|
||||
|
||||
use app\service\admin\system\ConfigService;
|
||||
use core\exception\handler\UploadException;
|
||||
use support\Container;
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @author Mr.April
|
||||
* @since 1.0
|
||||
* @method static uploadFile()
|
||||
*/
|
||||
class UploadFile
|
||||
{
|
||||
|
||||
static array $allowStorage = [];
|
||||
|
||||
protected static function init(): void
|
||||
{
|
||||
$configAllowStorage = config('core.upload.app.adapter_classes');
|
||||
self::$allowStorage = array_unique(array_merge([
|
||||
'local',
|
||||
'oss',
|
||||
'cos',
|
||||
'qiniu',
|
||||
's3',
|
||||
], array_keys($configAllowStorage)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置信息
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $default
|
||||
*
|
||||
* @return array|null
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function config(string $name = '', array $default = []): ?array
|
||||
{
|
||||
/** @var ConfigService $systemConfigService */
|
||||
$systemConfigService = Container::make(ConfigService::class);
|
||||
$config = $systemConfigService->config($name, $default);
|
||||
return $config ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \core\exception\handler\UploadException
|
||||
*/
|
||||
public static function disk(string|null $storage = null, bool $is_file_upload = true): UploadFileInterface
|
||||
{
|
||||
self::init();
|
||||
$configService = Container::make(ConfigService::class);
|
||||
$defaultConfig = $configService->config('upload', [
|
||||
'mode' => 'local',
|
||||
'single_limit' => 1024 * 1024,
|
||||
'total_limit' => 1024 * 1024,
|
||||
'nums' => 10,
|
||||
'exclude' => ['php', 'ext', 'exe'],
|
||||
]);
|
||||
if (empty($storage)) {
|
||||
$adapter = $defaultConfig['mode'];
|
||||
$adapterConfig = $configService->config($adapter);
|
||||
} else {
|
||||
$adapter = $storage;
|
||||
$adapterConfig = $configService->config($storage);
|
||||
}
|
||||
if (!in_array($adapter, self::$allowStorage)) {
|
||||
throw new UploadException("不支持的存储类型:" . $adapter);
|
||||
}
|
||||
$config = array_merge($defaultConfig, $adapterConfig, ['_is_file_upload' => $is_file_upload]);
|
||||
$handle = config('core.upload.app.adapter_classes.' . $adapter);
|
||||
if (!$handle) {
|
||||
throw new UploadException("未找到适配器处理器:" . $handle);
|
||||
}
|
||||
return new $handle($config);
|
||||
}
|
||||
|
||||
public static function __callStatic(string $name, array $arguments)
|
||||
{
|
||||
return static::disk()->{$name}(...$arguments);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
namespace core\upload;
|
||||
|
||||
interface UploadFileInterface
|
||||
{
|
||||
/**
|
||||
* @desc: 上传文件
|
||||
*
|
||||
* @param array $options
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function uploadFile(array $options): mixed;
|
||||
|
||||
/**
|
||||
* @desc: 上传服务端文件
|
||||
*
|
||||
* @param string $filePath
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function uploadServerFile(string $filePath): mixed;
|
||||
|
||||
/**
|
||||
* @desc: Base64上传文件
|
||||
*
|
||||
* @param string $base64
|
||||
* @param string $extension
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function uploadBase64(string $base64, string $extension = 'image'): mixed;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
use core\upload\storage\Cos;
|
||||
use core\upload\storage\Local;
|
||||
use core\upload\storage\Oss;
|
||||
use core\upload\storage\Qiniu;
|
||||
use core\upload\storage\S3;
|
||||
|
||||
return [
|
||||
'enable' => true,
|
||||
'debug' => config('app.debug'),
|
||||
'cdn_url' => '',
|
||||
'cdn_url_params' => '',
|
||||
'default_avatar' => '/upload/avatar.jpeg',
|
||||
'config_key' => [
|
||||
'local' => '',
|
||||
'oss' => '',
|
||||
'cos' => '',
|
||||
'qiniu' => '',
|
||||
's3' => '',
|
||||
],
|
||||
'adapter_classes' => [
|
||||
'local' => Local::class,
|
||||
'oss' => Oss::class,
|
||||
'cos' => Cos::class,
|
||||
'qiniu' => Qiniu::class,
|
||||
's3' => S3::class,
|
||||
],
|
||||
];
|
||||
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
namespace core\upload\storage;
|
||||
|
||||
|
||||
use core\exception\handler\UploadException;
|
||||
use core\upload\BaseUpload;
|
||||
use Qcloud\Cos\Client;
|
||||
|
||||
/**
|
||||
*
|
||||
* Cos 文件上传
|
||||
* @author Mr.April
|
||||
* @since 1.0
|
||||
*/
|
||||
class Cos extends BaseUpload
|
||||
{
|
||||
protected ?Client $instance = null;
|
||||
|
||||
/**
|
||||
* 获取实例
|
||||
* @return \Qcloud\Cos\Client|null
|
||||
*/
|
||||
public function getInstance(): ?Client
|
||||
{
|
||||
if (is_null($this->instance)) {
|
||||
$this->instance = new Client([
|
||||
'region' => $this->config['region'] ?? 'ap-shanghai',
|
||||
'schema' => 'https',
|
||||
'credentials' => [
|
||||
'secretId' => $this->config['secretId'],
|
||||
'secretKey' => $this->config['secretKey'],
|
||||
],
|
||||
]);
|
||||
}
|
||||
return $this->instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
* @param array $options
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function uploadFile(array $options = []): array
|
||||
{
|
||||
$result = [];
|
||||
$dirname = $this->config['dirname'] ?? '';
|
||||
$domain = trim($this->config['domain']);
|
||||
|
||||
foreach ($this->files as $key => $file) {
|
||||
$uniqueId = $this->getUniqueId($file->getPathname());
|
||||
$saveName = $uniqueId . '.' . $file->getUploadExtension();
|
||||
$object = !empty($dirname) ? $dirname . $this->dirSeparator . $saveName : $saveName;
|
||||
|
||||
$this->getInstance()->putObject([
|
||||
'Bucket' => $this->config['bucket'],
|
||||
'Key' => $object,
|
||||
'Body' => fopen($file->getPathname(), 'rb'),
|
||||
]);
|
||||
|
||||
$result[] = [
|
||||
'key' => $key,
|
||||
'origin_name' => $file->getUploadName(),
|
||||
'save_name' => $saveName,
|
||||
'save_path' => $object,
|
||||
'url' => $domain . $this->dirSeparator . $object,
|
||||
'unique_id' => $uniqueId,
|
||||
'size' => $file->getSize(),
|
||||
'mime_type' => $file->getUploadMimeType(),
|
||||
'extension' => $file->getUploadExtension(),
|
||||
'base_path' =>$this->dirSeparator . $object
|
||||
];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function uploadServerFile(string $filePath): array
|
||||
{
|
||||
$file = new \SplFileInfo($filePath);
|
||||
if (!$file->isFile()) {
|
||||
throw new UploadException('请检查上传文件是否是一个有效的文件,文件不存在' . $filePath);
|
||||
}
|
||||
|
||||
$uniqueId = $this->getUniqueId($file->getPathname());
|
||||
$dirname = $this->config['dirname'] ?? '';
|
||||
$object = !empty($dirname) ? $dirname . $this->dirSeparator . $uniqueId . '.' . $file->getExtension() : $uniqueId . '.' . $file->getExtension();
|
||||
|
||||
$this->getInstance()->putObject([
|
||||
'Bucket' => $this->config['bucket'],
|
||||
'Key' => $object,
|
||||
'Body' => fopen($file->getPathname(), 'rb'),
|
||||
]);
|
||||
|
||||
return [
|
||||
'origin_name' => $file->getRealPath(),
|
||||
'save_path' => $object,
|
||||
'url' => $this->config['domain'] . $this->dirSeparator . $object,
|
||||
'unique_id' => $uniqueId,
|
||||
'size' => $file->getSize(),
|
||||
'extension' => $file->getExtension(),
|
||||
];
|
||||
}
|
||||
|
||||
public function uploadBase64(string $base64, string $extension = 'png'): array
|
||||
{
|
||||
$base64 = explode(',', $base64);
|
||||
$uniqueId = date('YmdHis') . uniqid();
|
||||
$object = !empty($this->config['dirname']) ? $this->config['dirname'] . $this->dirSeparator . $uniqueId . '.' . $extension : $uniqueId . '.' . $extension;
|
||||
|
||||
$this->getInstance()->putObject([
|
||||
'Bucket' => $this->config['bucket'],
|
||||
'Key' => $object,
|
||||
'Body' => base64_decode($base64[1]),
|
||||
]);
|
||||
|
||||
$fileSize = strlen(base64_decode($base64[1]));
|
||||
|
||||
return [
|
||||
'save_path' => $object,
|
||||
'url' => $this->config['domain'] . $this->dirSeparator . $object,
|
||||
'unique_id' => $uniqueId,
|
||||
'size' => $fileSize,
|
||||
'extension' => $extension,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
namespace core\upload\storage;
|
||||
|
||||
use core\exception\handler\UploadException;
|
||||
use core\upload\BaseUpload;
|
||||
|
||||
/**
|
||||
* 本地上传
|
||||
*
|
||||
* @author Mr.April
|
||||
* @since 1.0
|
||||
*/
|
||||
class Local extends BaseUpload
|
||||
{
|
||||
public function uploadFile(array $options = []): array
|
||||
{
|
||||
$result = [];
|
||||
$root = $this->getRootPath();
|
||||
$rootDir = $this->config['root_dir'] ?? $this->config['dirname'] ?? ''; // 根目录
|
||||
$basePath = rtrim($root, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
|
||||
|
||||
if (!$this->createDir($basePath)) {
|
||||
throw new UploadException('文件夹创建失败,请核查是否有对应权限。');
|
||||
}
|
||||
$domain = rtrim($this->config['domain'] ?? '', '/');
|
||||
foreach ($this->files as $key => $file) {
|
||||
$uniqueId = $this->getUniqueId($file->getPathname());
|
||||
$saveFilename = $uniqueId . '.' . $file->getUploadExtension();
|
||||
$savePath = $basePath . $saveFilename;
|
||||
$url = $domain . $this->dirSeparator . $saveFilename;
|
||||
$basePathUrl = $this->dirSeparator . $saveFilename;
|
||||
|
||||
if (!empty($rootDir)) {
|
||||
// 生成子目录
|
||||
$subDir = $this->getSubdir($options);
|
||||
$fullDir = $rootDir . ($subDir ? $this->dirSeparator . $subDir : '');
|
||||
$savePath = $root . $this->dirSeparator . $fullDir . $this->dirSeparator . $saveFilename;
|
||||
$url = $domain . $this->dirSeparator . $fullDir . $this->dirSeparator . $saveFilename;
|
||||
$basePathUrl = $this->dirSeparator . $fullDir . $this->dirSeparator . $saveFilename;
|
||||
}
|
||||
|
||||
// 确保目录存在
|
||||
$dirPath = dirname($savePath);
|
||||
if (!is_dir($dirPath) && !$this->createDir($dirPath)) {
|
||||
throw new UploadException('子目录创建失败,请核查是否有对应权限。');
|
||||
}
|
||||
|
||||
$temp = [
|
||||
'key' => $key,
|
||||
'origin_name' => $file->getUploadName(),
|
||||
'save_name' => $saveFilename,
|
||||
'save_path' => $savePath,
|
||||
'url' => $url,
|
||||
'unique_id' => $uniqueId,
|
||||
'size' => $file->getSize(),
|
||||
'mime_type' => $file->getUploadMimeType(),
|
||||
'extension' => $file->getUploadExtension(),
|
||||
'base_path' => $basePathUrl
|
||||
];
|
||||
$file->move($savePath);
|
||||
$result[] = $temp;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取子目录
|
||||
*/
|
||||
protected function getSubdir(array $options = []): string
|
||||
{
|
||||
// 优先使用 options 中的子目录(支持 sub_dir 和 subdirectory、subdir 两种键名)
|
||||
if (isset($options['sub_dir']) && !empty($options['sub_dir'])) {
|
||||
return (string)$options['sub_dir'];
|
||||
} elseif (isset($options['subdirectory']) && !empty($options['subdirectory'])) {
|
||||
return (string)$options['subdirectory'];
|
||||
} elseif (isset($options['subdir']) && !empty($options['subdir'])) {
|
||||
return (string)$options['subdir'];
|
||||
}
|
||||
|
||||
// 其次使用配置中的 sub_dir 作为子目录
|
||||
if (isset($this->config['sub_dir']) && !empty($this->config['sub_dir'])) {
|
||||
return (string)$this->config['sub_dir'];
|
||||
} elseif (isset($this->config['directory']) && !empty($this->config['directory'])) {
|
||||
return (string)$this->config['directory'];
|
||||
}
|
||||
|
||||
// 默认使用年月格式 YYYYMM
|
||||
$subdirFormat = $this->config['subdir_format'] ?? 'Ym';
|
||||
|
||||
return match ($subdirFormat) {
|
||||
'Ymd' => date('Ymd'),
|
||||
'Ym' => date('Ym'),
|
||||
default => date('Ym')
|
||||
};
|
||||
}
|
||||
|
||||
protected function createDir(string $path): bool
|
||||
{
|
||||
if (is_dir($path)) {
|
||||
return true;
|
||||
}
|
||||
$parent = dirname($path);
|
||||
if (!is_dir($parent) && !$this->createDir($parent)) {
|
||||
return false;
|
||||
}
|
||||
return mkdir($path, 0755, true);
|
||||
}
|
||||
|
||||
private function getRootPath(): string
|
||||
{
|
||||
$root = $this->config['root'] ?? '';
|
||||
return match ($root) {
|
||||
'public' => public_path(),
|
||||
'runtime' => runtime_path(),
|
||||
'default' => runtime_path(),
|
||||
};
|
||||
}
|
||||
|
||||
function uploadServerFile(string $filePath): mixed
|
||||
{
|
||||
// TODO: Implement uploadServerFile() method.
|
||||
}
|
||||
|
||||
public function uploadBase64(string $base64, string $extension = 'JPEG'): mixed
|
||||
{
|
||||
// TODO: Implement uploadBase64() method.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
namespace core\upload\storage;
|
||||
|
||||
|
||||
use core\exception\handler\UploadException;
|
||||
use core\upload\BaseUpload;
|
||||
use OSS\Core\OssException;
|
||||
use OSS\OssClient;
|
||||
|
||||
class Oss extends BaseUpload
|
||||
{
|
||||
protected ?OssClient $instance = null;
|
||||
|
||||
/**
|
||||
* @desc: OSS实例
|
||||
*/
|
||||
public function getInstance(): OssClient
|
||||
{
|
||||
if ($this->instance === null) {
|
||||
$this->instance = new OssClient(
|
||||
$this->config['accessKeyId'],
|
||||
$this->config['accessKeySecret'],
|
||||
$this->config['endpoint']
|
||||
);
|
||||
}
|
||||
return $this->instance;
|
||||
}
|
||||
|
||||
public function uploadFile(array $options = []): array
|
||||
{
|
||||
$result = [];
|
||||
$dirname = $this->config['dirname'] ?? '';
|
||||
$domain = rtrim($this->config['domain'], '/');
|
||||
|
||||
foreach ($this->files as $key => $file) {
|
||||
$uniqueId = $this->getUniqueId($file->getPathname());
|
||||
$saveName = $uniqueId . '.' . $file->getUploadExtension();
|
||||
$object = !empty($dirname) ? $dirname . $this->dirSeparator . $saveName : $saveName;
|
||||
|
||||
$temp = [
|
||||
'key' => $key,
|
||||
'origin_name' => $file->getUploadName(),
|
||||
'save_name' => $saveName,
|
||||
'save_path' => $object,
|
||||
'url' => $domain . $this->dirSeparator . $object,
|
||||
'unique_id' => $uniqueId,
|
||||
'size' => $file->getSize(),
|
||||
'mime_type' => $file->getUploadMimeType(),
|
||||
'extension' => $file->getUploadExtension(),
|
||||
'base_path' =>$this->dirSeparator . $object
|
||||
];
|
||||
|
||||
try {
|
||||
$upload = $this->getInstance()->uploadFile($this->config['bucket'], $object, $file->getPathname());
|
||||
if (!isset($upload['info']) || $upload['info']['http_code'] !== 200) {
|
||||
throw new UploadException('Upload failed: ' . json_encode($upload));
|
||||
}
|
||||
$result[] = $temp;
|
||||
} catch (OssException $exception) {
|
||||
throw new UploadException($exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function uploadBase64(string $base64, string $extension = 'image'): array|bool
|
||||
{
|
||||
$base64 = explode(',', $base64);
|
||||
$uniqueId = date('YmdHis') . uniqid();
|
||||
$object = $uniqueId . '.' . $extension;
|
||||
$dirname = $this->config['dirname'] ?? '';
|
||||
if (!empty($dirname)) {
|
||||
$object = $dirname . $this->dirSeparator . $object;
|
||||
}
|
||||
|
||||
try {
|
||||
$result = $this->getInstance()->putObject($this->config['bucket'], $object, base64_decode($base64[1]));
|
||||
if (!isset($result['info']) || $result['info']['http_code'] !== 200) {
|
||||
throw new UploadException('Upload failed: ' . json_encode($result));
|
||||
}
|
||||
} catch (OssException $e) {
|
||||
throw new UploadException($e->getMessage());
|
||||
}
|
||||
|
||||
$imgLen = strlen($base64[1]);
|
||||
$fileSize = $imgLen - ($imgLen / 8) * 2;
|
||||
|
||||
return [
|
||||
'save_path' => $object,
|
||||
'url' => $this->config['domain'] . $this->dirSeparator . $object,
|
||||
'unique_id' => $uniqueId,
|
||||
'size' => $fileSize,
|
||||
'extension' => $extension,
|
||||
];
|
||||
}
|
||||
|
||||
public function uploadServerFile(string $filePath): array
|
||||
{
|
||||
$file = new \SplFileInfo($filePath);
|
||||
if (!$file->isFile()) {
|
||||
throw new UploadException('请检查上传文件是否是一个有效的文件,文件不存在: ' . $filePath);
|
||||
}
|
||||
|
||||
$uniqueId = hash_file('sha256', $file->getPathname());
|
||||
$dirname = $this->config['dirname'] ?? '';
|
||||
$object = $uniqueId . '.' . $file->getExtension();
|
||||
if (!empty($dirname)) {
|
||||
$object = $dirname . $this->dirSeparator . $object;
|
||||
}
|
||||
|
||||
$result = [
|
||||
'origin_name' => $file->getRealPath(),
|
||||
'save_path' => $object,
|
||||
'url' => $this->config['domain'] . $this->dirSeparator . $object,
|
||||
'unique_id' => $uniqueId,
|
||||
'size' => $file->getSize(),
|
||||
'extension' => $file->getExtension(),
|
||||
];
|
||||
|
||||
try {
|
||||
$upload = $this->getInstance()->uploadFile($this->config['bucket'], $object, $file->getRealPath());
|
||||
if (!isset($upload['info']) || $upload['info']['http_code'] !== 200) {
|
||||
throw new UploadException('Upload failed: ' . json_encode($upload));
|
||||
}
|
||||
} catch (OssException $exception) {
|
||||
throw new UploadException($exception->getMessage());
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace core\upload\storage;
|
||||
|
||||
use core\exception\handler\UploadException;
|
||||
use core\upload\BaseUpload;
|
||||
use Qiniu\Auth;
|
||||
use Qiniu\Storage\UploadManager;
|
||||
|
||||
class Qiniu extends BaseUpload
|
||||
{
|
||||
protected ?UploadManager $instance = null;
|
||||
protected ?string $uploadToken = null;
|
||||
|
||||
public function getInstance(): UploadManager
|
||||
{
|
||||
return $this->instance ??= new UploadManager();
|
||||
}
|
||||
|
||||
public function getUploadToken(): string
|
||||
{
|
||||
return $this->uploadToken ??= (new Auth($this->config['accessKey'], $this->config['secretKey']))->uploadToken($this->config['bucket']);
|
||||
}
|
||||
|
||||
public function uploadFile(array $options = []): array
|
||||
{
|
||||
$result = [];
|
||||
$dirname = $this->config['dirname'] ?? '';
|
||||
$domain = rtrim($this->config['domain'], '/');
|
||||
|
||||
foreach ($this->files as $key => $file) {
|
||||
$uniqueId = $this->getUniqueId($file->getPathname());
|
||||
$saveName = $uniqueId . '.' . $file->getUploadExtension();
|
||||
$object = !empty($dirname) ? $dirname . $this->dirSeparator . $saveName : $saveName;
|
||||
|
||||
$temp = [
|
||||
'key' => $key,
|
||||
'origin_name' => $file->getUploadName(),
|
||||
'save_name' => $saveName,
|
||||
'save_path' => $object,
|
||||
'url' => $domain . $this->dirSeparator . $object,
|
||||
'unique_id' => $uniqueId,
|
||||
'size' => $file->getSize(),
|
||||
'mime_type' => $file->getUploadMimeType(),
|
||||
'extension' => $file->getUploadExtension(),
|
||||
'base_path' =>$this->dirSeparator . $object
|
||||
];
|
||||
|
||||
try {
|
||||
[$ret, $err] = $this->getInstance()->putFile($this->getUploadToken(), $object, $file->getPathname());
|
||||
if ($err) {
|
||||
throw new UploadException((string)$err);
|
||||
}
|
||||
$result[] = $temp;
|
||||
} catch (\Throwable $exception) {
|
||||
throw new UploadException($exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function uploadServerFile(string $filePath): array
|
||||
{
|
||||
$file = new \SplFileInfo($filePath);
|
||||
if (!$file->isFile()) {
|
||||
throw new UploadException('请检查上传文件是否是一个有效的文件,文件不存在: ' . $filePath);
|
||||
}
|
||||
|
||||
$uniqueId = hash_file('sha256', $file->getPathname());
|
||||
$dirname = $this->config['dirname'] ?? '';
|
||||
$object = !empty($dirname) ? $dirname . $this->dirSeparator . $uniqueId . '.' . $file->getExtension() : $uniqueId . '.' . $file->getExtension();
|
||||
|
||||
$result = [
|
||||
'origin_name' => $file->getRealPath(),
|
||||
'save_path' => $object,
|
||||
'url' => $this->config['domain'] . $this->dirSeparator . $object,
|
||||
'unique_id' => $uniqueId,
|
||||
'size' => $file->getSize(),
|
||||
'extension' => $file->getExtension(),
|
||||
];
|
||||
|
||||
[$ret, $err] = $this->getInstance()->putFile($this->getUploadToken(), $object, $file->getPathname());
|
||||
if ($err) {
|
||||
throw new UploadException((string)$err);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function uploadBase64(string $base64, string $extension = 'png'): array
|
||||
{
|
||||
$base64 = explode(',', $base64);
|
||||
$uniqueId = date('YmdHis') . uniqid();
|
||||
$object = !empty($this->config['dirname']) ? $this->config['dirname'] . $this->dirSeparator . $uniqueId . '.' . $extension : $uniqueId . '.' . $extension;
|
||||
|
||||
[$ret, $err] = $this->getInstance()->put($this->getUploadToken(), $object, base64_decode($base64[1]));
|
||||
if ($err) {
|
||||
throw new UploadException((string)$err);
|
||||
}
|
||||
|
||||
$imgLen = strlen($base64[1]);
|
||||
$fileSize = $imgLen - ($imgLen / 8) * 2;
|
||||
|
||||
return [
|
||||
'save_path' => $object,
|
||||
'url' => $this->config['domain'] . $this->dirSeparator . $object,
|
||||
'unique_id' => $uniqueId,
|
||||
'size' => $fileSize,
|
||||
'extension' => $extension,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
namespace core\upload\storage;
|
||||
|
||||
use Aws\S3\S3Client;
|
||||
use core\exception\handler\UploadException;
|
||||
use core\upload\BaseUpload;
|
||||
use Throwable;
|
||||
|
||||
class S3 extends BaseUpload
|
||||
{
|
||||
protected ?S3Client $instance = null;
|
||||
|
||||
|
||||
public function getInstance(): S3Client
|
||||
{
|
||||
return $this->instance ??= new S3Client([
|
||||
'version' => $this->config['version'],
|
||||
'endpoint' => $this->config['endpoint'],
|
||||
'region' => $this->config['region'],
|
||||
'use_path_style_endpoint' => $this->config['use_path_style_endpoint'],
|
||||
'credentials' => [
|
||||
'key' => $this->config['key'],
|
||||
'secret' => $this->config['secret'],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function uploadFile(array $options = []): array
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach ($this->files as $key => $file) {
|
||||
$uniqueId = hash_file($this->algo, $file->getPathname());
|
||||
$saveName = $uniqueId . '.' . $file->getUploadExtension();
|
||||
$object = $this->config['dirname'] . $this->dirSeparator . $saveName;
|
||||
|
||||
$temp = [
|
||||
'key' => $key,
|
||||
'origin_name' => $file->getUploadName(),
|
||||
'save_name' => $saveName,
|
||||
'save_path' => $object,
|
||||
'url' => $this->config['domain'] . $this->dirSeparator . $object,
|
||||
'unique_id' => $uniqueId,
|
||||
'size' => $file->getSize(),
|
||||
'mime_type' => $file->getUploadMimeType(),
|
||||
'extension' => $file->getUploadExtension(),
|
||||
'base_path' =>$this->dirSeparator . $object
|
||||
];
|
||||
|
||||
try {
|
||||
$this->getInstance()->putObject([
|
||||
'Bucket' => $this->config['bucket'],
|
||||
'Key' => $object,
|
||||
'Body' => fopen($file->getPathname(), 'rb'),
|
||||
'ACL' => $this->config['acl'],
|
||||
]);
|
||||
$result[] = $temp;
|
||||
} catch (Throwable $exception) {
|
||||
throw new UploadException('上传文件失败: ' . $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function uploadServerFile(string $filePath): array
|
||||
{
|
||||
$file = new \SplFileInfo($filePath);
|
||||
if (!$file->isFile()) {
|
||||
throw new UploadException('不是一个有效的文件: ' . $filePath);
|
||||
}
|
||||
|
||||
$uniqueId = hash_file($this->algo, $file->getPathname());
|
||||
$object = $this->config['dirname'] . $this->dirSeparator . $uniqueId . '.' . $file->getExtension();
|
||||
|
||||
$result = [
|
||||
'origin_name' => $file->getRealPath(),
|
||||
'save_path' => $object,
|
||||
'url' => $this->config['domain'] . $this->dirSeparator . $object,
|
||||
'unique_id' => $uniqueId,
|
||||
'size' => $file->getSize(),
|
||||
'extension' => $file->getExtension(),
|
||||
];
|
||||
|
||||
try {
|
||||
$this->getInstance()->putObject([
|
||||
'Bucket' => $this->config['bucket'],
|
||||
'Key' => $object,
|
||||
'Body' => fopen($file->getPathname(), 'rb'),
|
||||
'ACL' => $this->config['acl'],
|
||||
]);
|
||||
} catch (Throwable $exception) {
|
||||
throw new UploadException('上传服务端文件失败: ' . $exception->getMessage());
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function uploadBase64(string $base64, string $extension = 'png'): array
|
||||
{
|
||||
$base64 = explode(',', $base64);
|
||||
$uniqueId = date('YmdHis') . uniqid();
|
||||
$object = $this->config['dirname'] . $this->dirSeparator . $uniqueId . '.' . $extension;
|
||||
|
||||
try {
|
||||
$this->getInstance()->putObject([
|
||||
'Bucket' => $this->config['bucket'],
|
||||
'Key' => $object,
|
||||
'Body' => base64_decode($base64[1]),
|
||||
'ACL' => $this->config['acl'],
|
||||
]);
|
||||
} catch (Throwable $exception) {
|
||||
throw new UploadException('上传Base64失败: ' . $exception->getMessage());
|
||||
}
|
||||
|
||||
$imgLen = strlen($base64[1]);
|
||||
$fileSize = $imgLen - ($imgLen / 8) * 2;
|
||||
|
||||
return [
|
||||
'save_path' => $object,
|
||||
'url' => $this->config['domain'] . $this->dirSeparator . $object,
|
||||
'unique_id' => $uniqueId,
|
||||
'size' => $fileSize,
|
||||
'extension' => $extension,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user