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; } }