初始化本地仓库

This commit is contained in:
2026-08-31 11:33:59 +08:00
commit 663f74b633
2390 changed files with 402918 additions and 0 deletions
+129
View File
@@ -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,
];
}
}
+129
View File
@@ -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.
}
}
+133
View File
@@ -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;
}
}
+113
View File
@@ -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,
];
}
}
+128
View File
@@ -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,
];
}
}