diff --git a/app/common/controller/UploadController.php b/app/common/controller/UploadController.php index 1637350..678034b 100644 --- a/app/common/controller/UploadController.php +++ b/app/common/controller/UploadController.php @@ -4,6 +4,7 @@ namespace app\common\controller; use app\common\service\OssService; use app\common\service\UploadedFilesService; +use app\common\service\UploadService; use app\common\traits\ResponseTrait; use support\Log; use support\Request; @@ -44,7 +45,7 @@ class UploadController $category = $request->input('category', 'general'); $fileInfo = [ - 'file_name' => $fileName, + 'file_name' => basename($fileName), 'file_path' => $pathUrl, 'file_size' => $file->getSize(), 'file_type' => $file->getUploadMimeType(), @@ -162,7 +163,7 @@ class UploadController $partition = $scene; $fileInfo = [ - 'file_name' => $result->file_name, + 'file_name' => basename($result->file_name), 'file_path' => $result->file_name, // OSS存储路径 'file_size' => $file->getSize(), 'file_type' => $file->getUploadMimeType(), @@ -220,12 +221,26 @@ class UploadController { try { $fileId = $request->input('id'); - - if (!$fileId) { - return $this->error('文件ID不能为空'); - } - + $path = $request->input('path'); + $uploadedFilesService = new UploadedFilesService(); + if(!empty($path) && empty($fileId)){ + $where = ['file_path' => $path]; + $file = $uploadedFilesService->getFile($where); + $fileId = $file->id; + }elseif(empty($path) && !empty($fileId)){ + $where = ['id' => $fileId]; + $file = $uploadedFilesService->getFile($where); + $path = $file->file_path; + } + if (!$fileId) { + return $this->error('文件ID与路径不能为空'); + } + + // 删除 OSS 上的文件 + $ossService = new OssService(); + $ossService->deleteFile($path); + $result = $uploadedFilesService->deleteFile($fileId); if ($result) { diff --git a/app/common/service/OssService.php b/app/common/service/OssService.php index 4d75b1f..72a956e 100644 --- a/app/common/service/OssService.php +++ b/app/common/service/OssService.php @@ -56,4 +56,24 @@ class OssService // 生成 OSS 私有 URL(有效期为 3600 秒) return $this->ossClient->signUrl($this->bucket, $objectKey, $expireTime); } + + /** + * 删除 OSS 上的文件 + * @param string $objectKey OSS上存储的对象名称 + * @return bool 删除是否成功 + */ + public function deleteFile($objectKey) + { + try { + // 删除 OSS 上的文件 + $this->ossClient->deleteObject($this->bucket, $objectKey); + return true; + } catch (OssException $e) { + // 错误处理 + throw new \RuntimeException('OSS Delete Error: ' . $e->getMessage()); + } catch (\Throwable $th) { + // 错误处理 + throw new BusinessException('Delete Error: ' . $th->getMessage()); + } + } } \ No newline at end of file diff --git a/app/common/service/UploadService.php b/app/common/service/UploadService.php index bec4942..33b7293 100644 --- a/app/common/service/UploadService.php +++ b/app/common/service/UploadService.php @@ -61,7 +61,7 @@ class UploadService // 记录文件信息到数据库 $uploadedFilesService = new UploadedFilesService(); $fileInfo = [ - 'file_name' => $objectKey, + 'file_name' => basename($objectKey), 'file_path' => $objectKey, 'file_size' => $size, 'file_type' => $mime, @@ -84,4 +84,63 @@ class UploadService throw new BusinessException($e->getMessage()); } } + + /** + * 删除 OSS 上的文件 + * + * @param array $param + * - object_key: OSS 对象键名-oss路径(必需) + * - file_id: 数据库文件记录ID(可选,用于删除数据库记录) + * @return bool 删除是否成功 + * + * 示例 + * $uploadService = new UploadService(); + * + * 只删除 OSS 文件 + * $uploadService->deleteOssFile([ + * 'object_key' => 'vehicle_album/documents/20241201/abc123.pdf' + * ]); + * + * 同时删除 OSS 文件和数据库记录 + * $uploadService->deleteOssFile([ + * 'object_key' => 'vehicle_album/images/20241201/abc123.jpg', + * 'file_id' => 123 + * ]); + */ + public function deleteOssFile($objectKey,$fileId = null) + { + try { + + if (empty($objectKey)) { + Log::error('OSS对象键名不能为空', $objectKey); + throw new BusinessException('OSS对象键名不能为空'); + } + + $uploadedFilesService = new UploadedFilesService(); + // 如果提供了文件ID,同时删除数据库记录 + if ($fileId) { + $uploadedFilesService->deleteFile($fileId); + }else{ + $uploadedFilesService = new UploadedFilesService(); + if (empty($fileId)) { + $where = ['file_path' => $objectKey]; + $file = $uploadedFilesService->getFile($where); + $fileId = $file->id; + } + if(!empty($fileId)){ + $uploadedFilesService->deleteFile($fileId); + } + } + + // 删除 OSS 上的文件 + $ossService = new OssService(); + $ossService->deleteFile($objectKey); + + Log::info('OSS文件删除成功', ['object_key' => $objectKey, 'file_id' => $fileId]); + return true; + } catch (\Throwable $e) { + Log::error('删除OSS文件失败: ' . $e->getMessage()); + throw new BusinessException('删除OSS文件失败: ' . $e->getMessage()); + } + } } \ No newline at end of file diff --git a/app/common/service/UploadedFilesService.php b/app/common/service/UploadedFilesService.php index 1319ab3..3689ac0 100644 --- a/app/common/service/UploadedFilesService.php +++ b/app/common/service/UploadedFilesService.php @@ -134,26 +134,37 @@ class UploadedFilesService ]; } + /** + * 获取文件 + * + * @param string $path + * @return bool|\Illuminate\Database\Eloquent\Model + */ + public function getFile(array $where) + { + $file = $this->logicShow(['where' => $where]); + + if (!$file) { + return false; + } + + return $file; + } + /** * 删除文件 - * + * * @param int $fileId * @return bool */ public function deleteFile(int $fileId): bool { $file = $this->logicShow(['where' => ['id' => $fileId]]); - + if (!$file) { return false; } - // 删除物理文件 - $fullPath = public_path() . $file->file_path; - if (file_exists($fullPath)) { - unlink($fullPath); - } - // 删除数据库记录 return $this->logicDeleteBy(['where' => ['id' => $fileId]]); } diff --git a/config/route.php b/config/route.php index b1eb26e..69c2d56 100644 --- a/config/route.php +++ b/config/route.php @@ -37,6 +37,7 @@ Route::any('/feishu/callback', [app\common\controller\NotifyController::class, ' Route::any('/proxy/ossurl', [app\common\controller\UploadController::class, 'proxy']); Route::any('/upload', [app\common\controller\UploadController::class, 'uploadFile']); Route::any('/upload/image', [app\common\controller\UploadController::class, 'uploadImage']); +Route::any('/upload/delete', [app\common\controller\UploadController::class, 'deleteFile']); // 公共 Route::group('/common', function () {