Browse Source

chunk upload

moell 10 months ago
parent
commit
898756ae37
1 changed files with 40 additions and 19 deletions
  1. 40 19
      app/Services/File/BIM/BlackHole/BlackHole.php

+ 40 - 19
app/Services/File/BIM/BlackHole/BlackHole.php

@@ -5,6 +5,7 @@ namespace App\Services\File\BIM\BlackHole;
 use App\Models\Enums\BimFileConvertStatus;
 use App\Services\File\BIM\BIMDriverEnum;
 use App\Services\File\BIM\Contacts\BIMContact;
+use GuzzleHttp\Psr7;
 use Illuminate\Http\UploadedFile;
 
 class BlackHole implements BIMContact
@@ -14,24 +15,34 @@ class BlackHole implements BIMContact
         $projectId = config("bim.black_hole.project_id");
         $treeInfo = $this->getTreeById($projectId);
 
-        $modelFile = $this->modelFileCreate($projectId, $treeInfo['subNodes'][0]['nodeId'], $file);
-        $dataSetId = $modelFile['dataSetId'];
+        $chunkSize = 1024 * 1024 * 10; // 10M
+        $chunkCount = (int)ceil($file->getSize() / $chunkSize);
 
-        $uploadFormData = [
-            ['name' => 'FileId', 'contents' => $modelFile['fileId']],
-            ['name' => 'FileInfo', 'contents' => fopen($file, 'r'), 'filename' => $file->getClientOriginalName()],
-            ['name' => 'FileDataId', 'contents' => $modelFile['fileDataId']],
-            ['name' => 'Key', 'contents' => $modelFile['extraProperties']['ResumableKey']],
-            ['name' => 'BlobIndex', 'contents' => 0],
-        ];
+        $modelFile = $this->modelFileCreate($projectId, $treeInfo['subNodes'][0]['nodeId'], $file, $chunkSize, $chunkCount);
+        $dataSetId = $modelFile['dataSetId'];
 
-        Client::getInstance()->post("/blackHole3D/project/modelFile/append", [
-            'multipart' => $uploadFormData,
-            'headers' => [
-                'fileUploadScheme' => 'Bim',
-                'fileUploadMode' => 'ResumableAppend',
-            ],
-        ]);
+        $fileResource = fopen($file, 'r');
+        for ($i = 0; $i < $chunkCount; $i++) {
+            $chunk = fread($fileResource, $chunkSize);
+
+            $stream = Psr7\Utils::streamFor($chunk);
+
+            $uploadFormData = [
+                ['name' => 'FileId', 'contents' => $modelFile['fileId']],
+                ['name' => 'FileInfo', 'contents' => $stream, 'filename' => $file->getClientOriginalName()],
+                ['name' => 'FileDataId', 'contents' => $modelFile['fileDataId']],
+                ['name' => 'Key', 'contents' => $modelFile['extraProperties']['ResumableKey']],
+                ['name' => 'BlobIndex', 'contents' => $i],
+            ];
+
+            Client::getInstance()->post("/blackHole3D/project/modelFile/append", [
+                'multipart' => $uploadFormData,
+                'headers' => [
+                    'fileUploadScheme' => 'Bim',
+                    'fileUploadMode' => 'ResumableAppend',
+                ],
+            ]);
+        }
 
         return [
             'bim_data_set_id' => $dataSetId,
@@ -94,7 +105,17 @@ class BlackHole implements BIMContact
     }
 
 
-    protected function modelFileCreate(string $projectId, string $nodeId, UploadedFile $file)
+    /**
+     * 模型文件上传准备
+     *
+     * @param string $projectId
+     * @param string $nodeId
+     * @param UploadedFile $file
+     * @param int $chunkSize
+     * @param int $chunkCount
+     * @return array|mixed
+     */
+    protected function modelFileCreate(string $projectId, string $nodeId, UploadedFile $file, int $chunkSize, int $chunkCount)
     {
         $formData = [
             'ProjId' => $projectId,
@@ -103,8 +124,8 @@ class BlackHole implements BIMContact
             'FileSize' => $file->getSize(),
             'FileType' => $file->getExtension() ? $file->getExtension() : pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION),
             'Scheme' => 'Bim',
-            'BlobCount' => 1,
-            'BlobSize' => $file->getSize(),
+            'BlobCount' => $chunkCount,
+            'BlobSize' => $chunkSize,
             'UploadWay' => 0
         ];