Browse Source

文件夹和文件上传刷新容器版本

moell 10 months ago
parent
commit
bcd89c30b6

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

@@ -117,13 +117,15 @@ class FileController extends Controller
      * 文件夹上传
      *
      * @param KeepDirectoryUploadRequest $request
-     * @return \Illuminate\Http\Response
+     * @return FileUploadSuccessResource|\Illuminate\Http\JsonResponse
      */
     public function keepDirectoryUpload(KeepDirectoryUploadRequest $request)
     {
-        (new KeepDirectoryUploadService($request))->upload();
+        $uploadedFiles = (new KeepDirectoryUploadService($request))->upload();
 
-        return $this->created();
+        return $this->success([
+            'data' => $uploadedFiles
+        ]);
     }
 
     public function byObject(string $objectType, string $objectId)

+ 35 - 2
app/Services/File/Upload/FilesUploadTrait.php

@@ -3,6 +3,7 @@
 namespace App\Services\File\Upload;
 
 use App\Http\Resources\API\FileUploadSuccessResource;
+use App\Models\ContainerContent;
 use App\Models\Enums\FileObjectType;
 use App\Models\File;
 use Illuminate\Http\Request;
@@ -11,12 +12,16 @@ use Illuminate\Support\Facades\Auth;
 
 trait FilesUploadTrait
 {
+    protected ?FileObjectType $fileObjectType = null;
+
+    protected $object = null;
+
     protected function checkRequestData(Request $request): int
     {
-        $fileObjectType = FileObjectType::from($request->object_type);
+        $this->fileObjectType = FileObjectType::from($request->object_type);
 
         if ($request->object_id) {
-            $fileObjectType->modelBuilderAllowed($request->object_id)->findOrFail($request->object_id);
+            $this->object = $this->fileObjectType->modelBuilderAllowed($request->object_id)->findOrFail($request->object_id);
         }
 
         $filesSize = 0;
@@ -88,9 +93,37 @@ trait FilesUploadTrait
             $uploadedFiles[] = new FileUploadSuccessResource($file);
         }
 
+        $this->updateObjectVersion();
+
         return $uploadedFiles;
     }
 
+    protected function updateObjectVersion()
+    {
+        if (!$this->object) {
+            return;
+        }
+
+        if ($this->fileObjectType == FileObjectType::CONTAINER) {
+            $version = $this->object->version;
+
+            $this->object->version++;
+            $this->object->save();
+
+            $fileIds = File::query()->where([
+                'object_type' => $this->fileObjectType->value,
+                'object_id' => $this->object->id
+            ])->pluck("id");
+
+            ContainerContent::query()->create([
+                ...$this->object->content($version)->first()->toArray(),
+                'created_by' => Auth::id(),
+                'files' => $fileIds->join(","),
+                'version' => $this->object->version,
+            ]);
+        }
+    }
+
     protected function updateUsedStorageCapacity(int $filesSize)
     {
         $company = Auth::user()->company;

+ 0 - 1
app/Services/File/Upload/KeepDirectoryUploadService.php

@@ -25,7 +25,6 @@ class KeepDirectoryUploadService
             'object_type' => $this->request->get("object_type"),
         ];
 
-
         $this->parentFolder = $this->request->get("folder_id")
             ? Folder::query()->where($this->objectWhere)->findOrFail($this->request->get("folder_id"))
             : null;

+ 28 - 0
database/migrations/2024_04_29_211400_change_files_to_container_contents_table.php

@@ -0,0 +1,28 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     */
+    public function up(): void
+    {
+        Schema::table('container_contents', function (Blueprint $table) {
+            $table->text("files")->nullable()->change();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     */
+    public function down(): void
+    {
+        Schema::table('container_contents', function (Blueprint $table) {
+            $table->string("files")->nullable()->change();
+        });
+    }
+};