Browse Source

文件上传调整支持bim检测和上传

moell 10 months ago
parent
commit
7609052327

+ 15 - 0
app/Models/BimFile.php

@@ -0,0 +1,15 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class BimFile extends Model
+{
+    use HasFactory;
+
+    protected $fillable = [
+        'file_id', 'bim_driver', 'bim_data_set_id', 'bim_file_id', 'convert_status', 'error'
+    ];
+}

+ 8 - 0
app/Services/File/BIM/BIMDriverEnum.php

@@ -0,0 +1,8 @@
+<?php
+
+namespace App\Services\File\BIM;
+
+enum BIMDriverEnum: string
+{
+    case BLACK_HOLE = "black_hole";
+}

+ 3 - 1
app/Services/File/BIM/BlackHole/BlackHole.php

@@ -2,6 +2,7 @@
 
 namespace App\Services\File\BIM\BlackHole;
 
+use App\Services\File\BIM\BIMDriverEnum;
 use App\Services\File\BIM\Contacts\BIMContact;
 use Illuminate\Http\UploadedFile;
 
@@ -35,6 +36,7 @@ class BlackHole implements BIMContact
             'bim_data_set_id' => $dataSetId,
             'bim_file_id' => $modelFile['fileId'],
             'bim_convert_status' => 0,
+            'bim_driver' => BIMDriverEnum::BLACK_HOLE->value,
         ];
     }
 
@@ -88,7 +90,7 @@ class BlackHole implements BIMContact
         $formData = [
             'ProjId' => $projectId,
             'DataSetId' => $nodeId,
-            'FileName' => uniqid() . $file->getClientOriginalName(),
+            'FileName' => uniqid() . "_" . $file->getClientOriginalName(),
             'FileSize' => $file->getSize(),
             'FileType' => $file->getExtension() ? $file->getExtension() : pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION),
             'Scheme' => 'Bim',

+ 1 - 1
app/Services/File/Upload/FilesUploadService.php

@@ -24,7 +24,7 @@ class FilesUploadService
         foreach ($this->request->file("files") as $index => $file) {
 
             $item = $this->uploadFile($this->request, $file);
-            $item['title'] = $fileNames[$index] ?? $item['title'];
+            $item['file']['title'] = $fileNames[$index] ?? $item['title'];
 
             $items[] = $item;
         }

+ 33 - 21
app/Services/File/Upload/FilesUploadTrait.php

@@ -3,6 +3,7 @@
 namespace App\Services\File\Upload;
 
 use App\Http\Resources\API\FileUploadSuccessResource;
+use App\Models\BimFile;
 use App\Models\ContainerContent;
 use App\Models\Enums\FileObjectType;
 use App\Models\File;
@@ -42,21 +43,25 @@ trait FilesUploadTrait
 
     protected function uploadFile(Request $request, UploadedFile $file): array
     {
-        /*$pathname = $file->storeAs(
+        $pathname = $file->storeAs(
             sprintf("c%s/%s/%s", Auth::user()->company_id, $request->get("object_type"), date("Ymd")),
             sprintf("%s.%s", md5(uniqid()), $file->extension())
-        );*/
+        );
 
-        $pathname = "/test/test.jpg";
+        $data = [
+            'bim' => [],
+        ];
 
         $extension = $file->extension() ? $file->extension() : pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
+        $isBimFile = 0;
         if (in_array($extension, config("bim.extensions")) && $request->object_type == FileObjectType::CONTAINER->value) {
-            $this->uploadToBIM($file);
+            $isBimFile = true;
+            $data['bim'] = BIMFactory::make()->uploadFile($file);
         }
 
         throw_validation_if(! $pathname, "File upload failed.");
 
-        return [
+        $data['file'] = [
             'pathname' => $pathname,
             'title' => $file->getClientOriginalName(),
             'size' => $file->getSize(),
@@ -67,29 +72,33 @@ trait FilesUploadTrait
             'company_id' => Auth::user()->company_id,
             'source' => $request->get("source", 1),
             'uuid' => $request->get("uuid"),
+            'is_bim' => $isBimFile,
         ];
+
+        return $data;
     }
 
     protected function storeFiles(array $items)
     {
         $uploadedFiles = [];
         foreach ($items as $item) {
-            if ($item['object_id'] && $item['source'] == 1||$item['uuid'] && $item['source']== 1) {
+            $fileInfo = $item['file'];
+            if ($fileInfo['object_id'] && $fileInfo['source'] == 1||$fileInfo['uuid'] && $fileInfo['source']== 1) {
                 $version = File::query()
-                    ->where('object_type', $item['object_type'])
-                    ->where('object_id', $item['object_id'])
-                    ->where("title", $item['title'])
+                    ->where('object_type', $fileInfo['object_type'])
+                    ->where('object_id', $fileInfo['object_id'])
+                    ->where("title", $fileInfo['title'])
                     ->where("source", 1)
-                    ->where("folder_id", $item['folder_id'] ?? 0)
+                    ->where("folder_id", $fileInfo['folder_id'] ?? 0)
                     ->count();
-                $item['version'] = $version + 1;
-                $item['is_latest_version'] = 1;
+                $fileInfo['version'] = $version + 1;
+                $fileInfo['is_latest_version'] = 1;
 
                 File::query()
-                    ->where('object_type', $item['object_type'])
-                    ->where('object_id', $item['object_id'])
-                    ->where("title", $item['title'])
-                    ->where("folder_id", $item['folder_id'] ?? 0)
+                    ->where('object_type', $fileInfo['object_type'])
+                    ->where('object_id', $fileInfo['object_id'])
+                    ->where("title", $fileInfo['title'])
+                    ->where("folder_id", $fileInfo['folder_id'] ?? 0)
                     ->where("source", 1)
                     ->where("is_latest_version", 1)
                     ->update([
@@ -97,7 +106,14 @@ trait FilesUploadTrait
                     ]);
             }
 
-            $file = File::query()->create($item);
+            $file = File::query()->create($fileInfo);
+
+            if (isset($item['bim']) && $item['bim']) {
+                BimFile::query()->create([
+                    'file_id' => $file->id,
+                    ...$item['bim']
+                ]);
+            }
 
             $uploadedFiles[] = new FileUploadSuccessResource($file);
         }
@@ -107,10 +123,6 @@ trait FilesUploadTrait
         return $uploadedFiles;
     }
 
-    protected function uploadToBIM(UploadedFile $file)
-    {
-        BIMFactory::make()->uploadFile($file);
-    }
 
     protected function updateObjectVersion()
     {

+ 2 - 2
app/Services/File/Upload/KeepDirectoryUploadService.php

@@ -39,8 +39,8 @@ class KeepDirectoryUploadService
         foreach ($this->request->file("files") as $index => $file) {
 
             $item = $this->uploadFile($this->request, $file);
-            $item['folder_id'] = $folderRelations[$index] ?? 0;
-            $item['title'] = $fileNames[$index] ?? $item['title'];
+            $item['file']['folder_id'] = $folderRelations[$index] ?? 0;
+            $item['file']['title'] = $fileNames[$index] ?? $item['file']['title'];
 
             $items[] = $item;
         }

+ 2 - 1
database/migrations/2024_05_11_171639_create_bim_files_table.php

@@ -13,11 +13,12 @@ return new class extends Migration
     {
         Schema::create('bim_files', function (Blueprint $table) {
             $table->id();
-            $table->index("file_id");
+            $table->integer("file_id")->index();
             $table->string("bim_driver")->nullable();
             $table->string("bim_data_set_id", 40)->nullable();
             $table->string("bim_file_id", 40)->nullable();
             $table->tinyInteger("convert_status")->default(0);
+            $table->string("error")->nullable();
             $table->timestamps();
         });
     }