chore: 重写初始提交(清空历史,整理后全量提交)
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace ywxapp\utils\storage;
|
||||
|
||||
use OSS\OssClient;
|
||||
use OSS\Core\OssException;
|
||||
use think\file\UploadedFile;
|
||||
|
||||
/**
|
||||
* AliOssStorage 类
|
||||
*
|
||||
* @author ywxapp <admin@ywxapp.cn>
|
||||
*/
|
||||
class AliOssStorage implements StorageInterface
|
||||
{
|
||||
protected $config;
|
||||
protected $ossClient;
|
||||
|
||||
|
||||
public function __construct(array $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->ossClient = new OssClient(
|
||||
$config['access_key_id'],
|
||||
$config['access_key_secret'],
|
||||
$config['endpoint']
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function upload(UploadedFile $file, string $path, array $options = []): array
|
||||
{
|
||||
try {
|
||||
$this->ossClient->uploadFile(
|
||||
$this->config['bucket'],
|
||||
$path,
|
||||
$file->getRealPath()
|
||||
);
|
||||
|
||||
return [
|
||||
'url' => $this->getUrl($path),
|
||||
'path' => $path,
|
||||
'storage' => 'alioss'
|
||||
];
|
||||
} catch (OssException $e) {
|
||||
throw new \Exception('阿里云上传失败: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function delete(string $path): bool
|
||||
{
|
||||
try {
|
||||
$this->ossClient->deleteObject($this->config['bucket'], $path);
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function getUrl(string $path): string
|
||||
{
|
||||
// 如果配置了CDN域名,优先使用
|
||||
if (!empty($this->config['cdn_domain'])) {
|
||||
return rtrim($this->config['cdn_domain'], '/') . '/' . ltrim($path, '/');
|
||||
}
|
||||
// 否则使用OSS默认域名
|
||||
return "https://{$this->config['bucket']}.{$this->config['endpoint']}/{$path}";
|
||||
}
|
||||
|
||||
|
||||
public function exists(string $path): bool
|
||||
{
|
||||
try {
|
||||
$this->ossClient->getObjectMeta($this->config['bucket'], $path);
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace ywxapp\utils\storage;
|
||||
|
||||
use think\file\UploadedFile;
|
||||
|
||||
/**
|
||||
* LocalStorage 类
|
||||
*
|
||||
* @author ywxapp <admin@ywxapp.cn>
|
||||
*/
|
||||
class LocalStorage implements StorageInterface
|
||||
{
|
||||
protected $config;
|
||||
|
||||
|
||||
public function __construct(array $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析存储根目录(支持绝对路径与相对 public 目录的相对路径)
|
||||
*/
|
||||
protected function getRoot(): string
|
||||
{
|
||||
$root = $this->config['root'] ?? 'storage';
|
||||
// 绝对路径(Windows 盘符 或 Unix 根目录)直接使用,不再拼接 public_path
|
||||
if (preg_match('#^[a-zA-Z]:[\\\\/]|^/#', $root)) {
|
||||
return rtrim($root, '/\\');
|
||||
}
|
||||
return rtrim(public_path() . ltrim($root, '/\\'), '/\\');
|
||||
}
|
||||
|
||||
|
||||
public function upload(UploadedFile $file, string $path, array $options = []): array
|
||||
{
|
||||
// 移动文件到指定目录
|
||||
$root = $this->getRoot();
|
||||
$url = $this->config['url'] ?? '/storage';
|
||||
|
||||
$dir = $root . DIRECTORY_SEPARATOR . ltrim($path, '/\\');
|
||||
if (! is_dir($dir)) {
|
||||
mkdir($dir, 0777, true);
|
||||
}
|
||||
|
||||
$ext = $file->extension() ?: pathinfo($file->getOriginalName(), PATHINFO_EXTENSION);
|
||||
$savename = date('His') . '_' . uniqid() . '.' . $ext;
|
||||
$file->move($dir, $savename);
|
||||
|
||||
$realName = $file->getSaveName() ?: $savename;
|
||||
$relative = ltrim($path, '/\\') . '/' . $realName;
|
||||
return [$realName, $relative, rtrim($url, '/') . '/' . $relative];
|
||||
}
|
||||
|
||||
|
||||
public function delete(string $path): bool
|
||||
{
|
||||
$fullPath = $this->getRoot() . DIRECTORY_SEPARATOR . ltrim($path, '/\\');
|
||||
return file_exists($fullPath) && unlink($fullPath);
|
||||
}
|
||||
|
||||
|
||||
public function getUrl(string $path): string
|
||||
{
|
||||
$domain = $this->config['domain'] ?? '';
|
||||
return rtrim($domain, '/') . '/' . ltrim($path, '/');
|
||||
}
|
||||
|
||||
|
||||
public function exists(string $path): bool
|
||||
{
|
||||
$fullPath = $this->getRoot() . DIRECTORY_SEPARATOR . ltrim($path, '/\\');
|
||||
return file_exists($fullPath);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace ywxapp\utils\storage;
|
||||
|
||||
use think\file\UploadedFile;
|
||||
|
||||
/**
|
||||
* 存储驱动统一接口
|
||||
* 定义了所有云存储平台必须实现的方法
|
||||
*/
|
||||
interface StorageInterface
|
||||
{
|
||||
/**
|
||||
* 上传文件
|
||||
* @param UploadedFile $file 上传的文件对象
|
||||
* @param string $path 存储路径
|
||||
* @param array $options 上传选项
|
||||
* @return array 包含url、path等信息的结果集
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function upload(UploadedFile $file, string $path, array $options = []): array;
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param string $path 文件路径
|
||||
* @return bool
|
||||
*/
|
||||
public function delete(string $path): bool;
|
||||
|
||||
/**
|
||||
* 获取文件访问URL
|
||||
* @param string $path 文件路径
|
||||
* @return string
|
||||
*/
|
||||
public function getUrl(string $path): string;
|
||||
|
||||
/**
|
||||
* 判断文件是否存在
|
||||
* @param string $path
|
||||
* @return bool
|
||||
*/
|
||||
public function exists(string $path): bool;
|
||||
}
|
||||
Reference in New Issue
Block a user