浏览代码

附件删除,附件改名,附件下载返回路径(不再使用Storage::download)

kely 11 月之前
父节点
当前提交
eeea63dfd4
共有 3 个文件被更改,包括 40 次插入4 次删除
  1. 38 3
      app/Http/Controllers/API/FileController.php
  2. 1 1
      app/Http/Resources/API/FileDownloadResource.php
  3. 1 0
      routes/api.php

+ 38 - 3
app/Http/Controllers/API/FileController.php

@@ -10,6 +10,7 @@ use App\Http\Resources\API\FileDownloadResource;
 use App\Http\Resources\API\FileUploadSuccessResource;
 use App\Models\Enums\FileObjectType;
 use App\Models\File;
+use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Storage;
 
@@ -22,11 +23,45 @@ class FileController extends Controller
 
         $fileObjectType = FileObjectType::from($file->object_type);
         $fileObjectType->modelBuilderAllowed($file->object_id)->findOrFail($file->object_id);
+        if(! $fileObjectType){
+            return $this->badRequest(sprintf("File ID: %s, no permission to access", $file->id));
+        }
+//        return Storage::download(
+//            $file->pathname,
+//            sprintf("%s.%s", rtrim($file->title, "." . $file->extension), $file->extension)
+//        );
+        return new FileDownloadResource($file);
+    }
 
-        return Storage::download(
-            $file->pathname,
-            sprintf("%s.%s", rtrim($file->title, "." . $file->extension), $file->extension)
+    public function changeName(Request $request,string $id)
+    {
+        $file = File::query()->findOrFail($id);
+        $fileObjectType = FileObjectType::from($file->object_type);
+        $fileObjectType->modelBuilderAllowed($file->object_id)->findOrFail($file->object_id);
+        if(! $fileObjectType){
+            return $this->badRequest(sprintf("File ID: %s, no permission to access", $file->id));
+        }
+
+        File::query()->where('title',$file->title)->where('object_type',$file->object_type)->update(
+            [
+                'title' => $request->get('title'),
+            ]
         );
+
+        return $this->noContent();
+    }
+
+    public function destroy(string $id){
+        $file = File::query()->findOrFail($id);
+        $fileObjectType = FileObjectType::from($file->object_type);
+        $fileObjectType->modelBuilderAllowed($file->object_id)->findOrFail($file->object_id);
+        if(! $fileObjectType){
+            return $this->badRequest(sprintf("File ID: %s, no permission to access", $file->id));
+        }
+
+        File::query()->where('title',$file->title)->where('object_type',$file->object_type)->delete();
+
+        return $this->noContent();
     }
 
     public function downloadZip(DownloadZipRequest $request)

+ 1 - 1
app/Http/Resources/API/FileDownloadResource.php

@@ -20,7 +20,7 @@ class FileDownloadResource extends JsonResource
             'title' => $this->title,
             'download_url' => Storage::url($this->pathname),
             'extension' => $this->extension,
-            'size' => $this->extension,
+            'size' => $this->size,
         ];
     }
 }

+ 1 - 0
routes/api.php

@@ -129,5 +129,6 @@ Route::middleware(['auth:sanctum'])->group(function () {
         Route::get("file/{object_type}/{object_id}", [API\FileController::class, "byObject"])->name("file.by-object");
         Route::post("file/{file}/download", [API\FileController::class, "download"])->name("file.download");
         Route::post("file/download-zip", [API\FileController::class, "downloadZip"])->name("file.download-zip");
+        Route::delete("file/{file}/destroy", [API\FileController::class, "destroy"])->name("file.destroy");
     });
 });