Browse Source

Download shared files

moell 10 months ago
parent
commit
671609e43e

+ 8 - 1
app/Http/Controllers/API/FileController.php

@@ -89,13 +89,20 @@ class FileController extends Controller
         ]);
     }
 
-    protected function downloadAllLatest(DownloadService $service, string $objectType, string $objectId)
+    public function downloadAllLatest(DownloadService $service, string $objectType, string $objectId)
     {
         return $this->success([
             'data' => $service->downloadAllLatest($objectType, $objectId),
         ]);
     }
 
+    public function downloadShareFile(DownloadService $service, string $uuid)
+    {
+        return $this->success([
+            'data' => $service->downloadShareFile($uuid),
+        ]);
+    }
+
     /**
      * 文件上传
      *

+ 21 - 0
app/Services/File/DownloadService.php

@@ -5,8 +5,10 @@ namespace App\Services\File;
 use App\Http\Resources\API\FileDownloadResource;
 use App\Models\Enums\FileObjectType;
 use App\Models\Enums\FolderObjectType;
+use App\Models\Enums\ShareFileObjectType;
 use App\Models\File;
 use App\Models\Folder;
+use App\Models\ShareFile;
 use Illuminate\Support\Collection;
 
 class DownloadService
@@ -18,6 +20,25 @@ class DownloadService
         return $this->filesFormat($files);
     }
 
+    /**
+     * 下载分享文件
+     *
+     * @param string $uuid
+     * @return array
+     */
+    public function downloadShareFile(string $uuid)
+    {
+        $shareFile = ShareFile::query()->where("uuid", $uuid)->firstOrFail();
+
+        $objectType = ShareFileObjectType::from($shareFile->object_type);
+
+        $objectType->modelBuilderAllowed($shareFile->object_id)->findOrFail($shareFile->object_id);
+
+        $files = File::query()->with(['folder'])->whereIn("id", $shareFile->files)->get();
+
+        return $this->filesFormat($files);
+    }
+
     public function downloadAllLatest(string $objectType, string $objectId)
     {
         $folderObjectType = FolderObjectType::from($objectType);

+ 3 - 0
routes/api.php

@@ -160,6 +160,9 @@ Route::middleware(['auth:sanctum'])->group(function () {
         Route::post("file/download-zip", [API\FileController::class, "downloadZip"])->name("file.download-zip");
         Route::get("file/download-all-latest/{object_type}/{object_id}", [API\FileController::class, "downloadAllLatest"])
             ->name("file.download-all-latest");
+        Route::get("file/download/{uuid}/share-file", [API\FileController::class, "downloadShareFile"])
+            ->name("file.download-share-file");
+
         Route::patch("file/{file}/change-name", [API\FileController::class, "changeName"])->name("file.change-name");
         Route::delete("file/{file}/destroy", [API\FileController::class, "destroy"])->name("file.destroy");
         Route::post("file/keep-directory-upload", [API\FileController::class, "keepDirectoryUpload"])->name("file.keep-directory-upload");