Browse Source

黑洞bim模型对接,客户端封装

moell 10 months ago
parent
commit
02af776f85

+ 6 - 1
app/Http/Requests/API/File/KeepDirectoryUploadRequest.php

@@ -41,7 +41,12 @@ class KeepDirectoryUploadRequest extends FormRequest
             $rules['uuid'] = "required|max:40";
         }
 
-        $rules['files.*'][] = File::types(['txt', 'jpeg', 'png', 'gif', 'pdf', 'xls', 'xlsx', 'zip', 'wps', 'docx', 'doc'])->max("2gb");
+        $extensions = ['txt', 'jpeg', 'png', 'gif', 'pdf', 'xls', 'xlsx', 'zip', 'wps', 'docx', 'doc'];
+        if ($this->request->get("object_type") == FileObjectType::CONTAINER->value) {
+            $extensions = [...$extensions, ...config("bim.extensions")];
+        }
+
+        //$rules['files.*'][] = File::types($extensions)->max("2gb"); // 临时关闭,无法校验rvt文件后缀
 
         return $rules;
     }

+ 18 - 0
app/Services/File/BIM/BIMFactory.php

@@ -0,0 +1,18 @@
+<?php
+
+namespace App\Services\File\BIM;
+
+use App\Services\File\BIM\BlackHole\BlackHole;
+use App\Services\File\BIM\Contacts\BIMContact;
+
+class BIMFactory
+{
+    public static function make(string $driver = null): BIMContact
+    {
+        $driver = $driver ? $driver : config("bim.default");
+
+        return match ($driver) {
+            "black_hole" => new BlackHole(),
+        };
+    }
+}

+ 87 - 0
app/Services/File/BIM/BlackHole/BlackHole.php

@@ -0,0 +1,87 @@
+<?php
+
+namespace App\Services\File\BIM\BlackHole;
+
+use App\Services\File\BIM\Contacts\BIMContact;
+use Illuminate\Http\UploadedFile;
+
+class BlackHole implements BIMContact
+{
+    public function uploadFile(UploadedFile $file, array $params = [])
+    {
+        //$this->addLeafNodes(['Node1', 'Node2'], config("bim.black_hole.project_id"));
+
+        $projectId = config("bim.black_hole.project_id");
+        $treeInfo = $this->getTreeById($projectId);
+
+        $modelFile = $this->modelFileCreate($projectId, $treeInfo['subNodes'][0]['nodeId'], $file);
+        dump($modelFile);
+
+        //$this->modelFileCreate(config("bim.black_hole.project_id"), $file);
+
+
+    }
+
+    public function downloadSourceFile()
+    {
+
+    }
+
+    protected function modelFileCreate(string $projectId, string $nodeId, UploadedFile $file)
+    {
+        $formData = [
+            'ProjId' => $projectId,
+            'DataSetId' => $nodeId,
+            'FileName' => $file->getClientOriginalName(),
+            'FileSize' => $file->getSize(),
+            'FileType' => $file->getExtension() ? $file->getExtension() : pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION),
+            'Scheme' => 'Bim',
+            'BlobCount' => 1,
+            'BlobSize' => $file->getSize(),
+            'UploadWay' => 0
+        ];
+
+        $result = Client::getInstance()->request("POST", "/blackHole3D/project/modelFile/create", [
+            'form_params' => $formData,
+            'headers' => [
+                'fileUploadScheme' => 'Bim',
+                'fileUploadMode' => 'ResumableCreate'
+            ]
+        ]);
+
+        return $result['data'] ?? [];
+    }
+
+    protected function getTreeById(string $projectId)
+    {
+        $result = Client::getInstance()->request("POST", "/blackHole3D/project/modelTree/getTreeById", [
+            'json' => [
+                'projId' => $projectId
+            ]
+        ]);
+
+        return $result['data'] ?? [];
+    }
+
+    protected function addLeafNodes(array $nodes, string $parentId = "")
+    {
+        $items = [];
+        foreach ($nodes as $node) {
+            $items[]['dataSetName'] = $node;
+        }
+
+        $result = Client::getInstance()->request("POST", "/blackHole3D/project/modelTree/addLevelNodes", [
+            'json' => [
+                'parentId' => $parentId,
+                'dataSets' => $items
+            ]
+        ]);
+
+        dump([
+            'parentId' => $parentId,
+            'dataSets' => $items
+        ]);
+
+        dump($result);
+    }
+}

+ 55 - 0
app/Services/File/BIM/BlackHole/Client.php

@@ -0,0 +1,55 @@
+<?php
+
+namespace App\Services\File\BIM\BlackHole;
+
+
+use App\Services\File\BIM\Exceptions\FailedException;
+use Psr\Http\Message\ResponseInterface;
+
+
+class Client {
+
+    protected static ?Client $instance = null;
+
+    protected \GuzzleHttp\Client $client;
+
+    protected function __construct()
+    {
+        $this->client = new \GuzzleHttp\Client([
+            'base_uri' => config("bim.black_hole.host"),
+            'http_errors' => false,
+            'headers' => [
+                'Accept' => "application/json",
+                'ClientId' => config("bim.black_hole.client_id"),
+                'SecretKey' => config("bim.black_hole.secret_key"),
+                'UserId' => config("bim.black_hole.user_id"),
+            ]
+        ]);
+    }
+
+    public static function getInstance(): Client
+    {
+        if (! self::$instance) {
+            self::$instance = new self();
+        }
+
+        return self::$instance;
+    }
+
+    public function request(string $method, string $uri, array $option = []): ?array
+    {
+        return $this->parseResponse($this->client->request($method, $uri, $option));
+    }
+
+    protected function parseResponse(ResponseInterface $response): ?array
+    {
+        $body = json_decode((string)$response->getBody(), true);
+        dump((string)$response->getBody());
+        $isJson = json_last_error() == JSON_ERROR_NONE;
+
+        return match ($response->getStatusCode()) {
+            200, 201, 204 => $isJson ? $body : null,
+            default => throw new FailedException($body['message'] ?? $response->getReasonPhrase()),
+        };
+    }
+}

+ 12 - 0
app/Services/File/BIM/Contacts/BIMContact.php

@@ -0,0 +1,12 @@
+<?php
+
+namespace App\Services\File\BIM\Contacts;
+
+use Illuminate\Http\UploadedFile;
+
+interface BIMContact {
+
+    public function uploadFile(UploadedFile $file, array $params = []);
+
+    public function downloadSourceFile();
+}

+ 8 - 0
app/Services/File/BIM/Exceptions/FailedException.php

@@ -0,0 +1,8 @@
+<?php
+
+namespace App\Services\File\BIM\Exceptions;
+
+class FailedException extends \Exception
+{
+
+}

+ 16 - 3
app/Services/File/Upload/FilesUploadTrait.php

@@ -6,6 +6,7 @@ use App\Http\Resources\API\FileUploadSuccessResource;
 use App\Models\ContainerContent;
 use App\Models\Enums\FileObjectType;
 use App\Models\File;
+use App\Services\File\BIM\BIMFactory;
 use Illuminate\Http\Request;
 use Illuminate\Http\UploadedFile;
 use Illuminate\Support\Facades\Auth;
@@ -41,10 +42,17 @@ 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";
+
+        $extension = $file->extension() ? $file->extension() : pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
+        if (in_array($extension, config("bim.extensions")) && $request->object_type == FileObjectType::CONTAINER->value) {
+            $this->uploadToBIM($file);
+        }
 
         throw_validation_if(! $pathname, "File upload failed.");
 
@@ -52,7 +60,7 @@ trait FilesUploadTrait
             'pathname' => $pathname,
             'title' => $file->getClientOriginalName(),
             'size' => $file->getSize(),
-            'extension' => $file->extension(),
+            'extension' => $extension,
             'object_type' => $request->object_type,
             'object_id' => $request->object_id,
             'created_by' => Auth::id(),
@@ -99,6 +107,11 @@ trait FilesUploadTrait
         return $uploadedFiles;
     }
 
+    protected function uploadToBIM(UploadedFile $file)
+    {
+        BIMFactory::make()->uploadFile($file);
+    }
+
     protected function updateObjectVersion()
     {
         if (!$this->object) {

+ 15 - 0
config/bim.php

@@ -0,0 +1,15 @@
+<?php
+
+return [
+    'default' => env('BIM_DRIVER', 'black_hole'),
+
+    'extensions' => ['rvt', 'ifc', 'fbx', '3dxml', 'gim', 'igms', 'stp', 'gltf', '3dm', 'skp', 'glb', 'dgn', 'nwd', 'nwc'],
+
+    'black_hole' => [
+        'host' => env("BLACK_HOLE_HOST", "http://106.52.199.205:9012/"),
+        'client_id' => env("BLACK_HOLE_CLIENT_ID"),
+        'secret_key' => env("BLACK_HOLE_SECRET_KEY"),
+        'user_id' => env("BLACK_HOLE_USER_ID"),
+        'project_id' => env("BLACK_HOLE_PROJECT_ID"),
+    ]
+];