102 lines
3.4 KiB
PHP
102 lines
3.4 KiB
PHP
<?php
|
||
|
||
namespace App\Service\common;
|
||
|
||
use App\BaseApp\BaseService;
|
||
use App\Service\common\oss\OssRuntimeConfigService;
|
||
use App\Service\common\oss\OssStorageFactory;
|
||
use App\Service\common\oss\OssStorageInterface;
|
||
use App\Service\FileService;
|
||
use Illuminate\Support\Str;
|
||
|
||
/**
|
||
* 统一上传入口:按数据库启用的 OSS 配置,经工厂分发到对应存储实现
|
||
*/
|
||
class UploadService extends BaseService
|
||
{
|
||
private OssStorageInterface $uploadService;
|
||
|
||
public function __construct()
|
||
{
|
||
parent::__construct();
|
||
try {
|
||
$config = OssRuntimeConfigService::getInstance()->getActiveConfig();
|
||
$this->uploadService = OssStorageFactory::getInstance()->make($config);
|
||
} catch (\Throwable $e) {
|
||
// 配置异常时回退本地,避免整站上传不可用
|
||
$this->uploadService = OssStorageFactory::getInstance()->make([
|
||
'driver' => 'local',
|
||
'path_prefix' => 'uploads',
|
||
'domain' => '',
|
||
]);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 上传图片到当前启用的存储
|
||
*
|
||
* @param mixed $file 上传文件对象
|
||
*/
|
||
public function uploadImage($file): array|bool
|
||
{
|
||
$ext = $file->getClientOriginalExtension();
|
||
$key = 'spa/image/' . date('Ymd') . '/' . 'cc_upload_' . Str::random() . uniqid() . '.' . $ext;
|
||
$result = $this->uploadService->uploadVideo($file, $key);
|
||
if (!$result) {
|
||
$this->utils->errorThrow('图片上传失败');
|
||
}
|
||
FileService::getInstance()->create([
|
||
'user_id' => $this->userId,
|
||
'url' => $result['url'],
|
||
]);
|
||
return $result;
|
||
}
|
||
|
||
/**
|
||
* 上传视频到当前启用的存储
|
||
*
|
||
* @param mixed $file 上传文件对象
|
||
*/
|
||
public function uploadVideo($file): array|bool
|
||
{
|
||
$ext = $file->getClientOriginalExtension();
|
||
$key = 'spa/video/' . date('Ymd') . '/' . 'cc_upload_' . Str::random() . uniqid() . '.' . $ext;
|
||
$result = $this->uploadService->uploadVideo($file, $key);
|
||
if (!$result) {
|
||
$this->utils->errorThrow('视频上传失败');
|
||
}
|
||
FileService::getInstance()->create([
|
||
'user_id' => $this->userId,
|
||
'url' => $result['url'],
|
||
]);
|
||
return $result;
|
||
}
|
||
|
||
/**
|
||
* 上传文档(商品图册的 PDF、订单转账凭证的 PDF 等)
|
||
*
|
||
* 驱动层的 uploadVideo 就是通用的 put,图册 PDF 之前只能借 video 通道上传,
|
||
* 结果 key 落在 spa/video 下,素材库按目录归类时全错位,故单独开一路。
|
||
*
|
||
* @param mixed $file 上传文件对象
|
||
*/
|
||
public function uploadDocument($file): array|bool
|
||
{
|
||
$ext = strtolower($file->getClientOriginalExtension());
|
||
$allowed = ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'csv', 'zip'];
|
||
if (!in_array($ext, $allowed, true)) {
|
||
$this->utils->errorThrow('不支持的文件类型:' . $ext);
|
||
}
|
||
$key = 'spa/file/' . date('Ymd') . '/' . 'cc_upload_' . Str::random() . uniqid() . '.' . $ext;
|
||
$result = $this->uploadService->uploadVideo($file, $key);
|
||
if (!$result) {
|
||
$this->utils->errorThrow('文件上传失败');
|
||
}
|
||
FileService::getInstance()->create([
|
||
'user_id' => $this->userId,
|
||
'url' => $result['url'],
|
||
]);
|
||
return $result;
|
||
}
|
||
}
|