diff --git a/src/Controller/Component/ModulesComponent.php b/src/Controller/Component/ModulesComponent.php index 0e87f8601..9fb409b12 100644 --- a/src/Controller/Component/ModulesComponent.php +++ b/src/Controller/Component/ModulesComponent.php @@ -344,17 +344,20 @@ public function upload(array &$requestData): void /** @var \Laminas\Diactoros\UploadedFile $file */ $file = $requestData['file']; + $filepath = $file->getStream()->getMetadata('uri'); + $content = file_get_contents($filepath); // upload file $filename = basename($file->getClientFileName()); - $filepath = $file->getStream()->getMetadata('uri'); $headers = ['Content-Type' => $file->getClientMediaType()]; $apiClient = ApiClientProvider::getApiClient(); - $response = $apiClient->upload($filename, $filepath, $headers); - - // assoc stream to media - $streamId = $response['data']['id']; - $requestData['id'] = $this->assocStreamToMedia($streamId, $requestData, $filename); + $type = $this->getController()->getRequest()->getParam('object_type'); + $response = $apiClient->post( + sprintf('/%s/upload/%s', $type, $filename), + $content, + $headers + ); + $requestData['id'] = Hash::get($response, 'data.id'); } unset($requestData['file'], $requestData['remote_url']); } @@ -391,6 +394,8 @@ public function removeStream(array $requestData): bool * @param array $requestData The request data * @param string $defaultTitle The default title for media * @return string The media ID + * @deprecated 5.15.4 This method is no longer used and will be removed in a future version. + * @codeCoverageIgnore */ public function assocStreamToMedia(string $streamId, array &$requestData, string $defaultTitle): string { diff --git a/tests/TestCase/Controller/Component/ModulesComponentTest.php b/tests/TestCase/Controller/Component/ModulesComponentTest.php index fb8b9338c..dbc3d0d93 100644 --- a/tests/TestCase/Controller/Component/ModulesComponentTest.php +++ b/tests/TestCase/Controller/Component/ModulesComponentTest.php @@ -858,9 +858,9 @@ public function testBeforeRender($userId, $modules, ?string $currentModule, arra public function uploadProvider(): array { $filename = sprintf('%s/tests/files/%s', getcwd(), 'test.png'); - $file = new UploadedFile($filename, filesize($filename), 0, $filename); - $fileErr = new UploadedFile($filename, filesize($filename), 1, $filename); - $fileEmpty = new UploadedFile($filename, filesize($filename), 4, $filename); + $file = new UploadedFile($filename, filesize($filename), 0, $filename, 'image/png'); + $fileErr = new UploadedFile($filename, filesize($filename), 1, $filename, 'image/png'); + $fileEmpty = new UploadedFile($filename, filesize($filename), 4, $filename, 'image/png'); return [ 'no file' => [ @@ -870,6 +870,7 @@ public function uploadProvider(): array ], null, false, + null, ], 'model-type empty' => [ [ @@ -878,6 +879,7 @@ public function uploadProvider(): array ], new InternalErrorException('Invalid form data: model-type'), false, + null, ], 'model-type not a string' => [ [ @@ -887,6 +889,7 @@ public function uploadProvider(): array ], new InternalErrorException('Invalid form data: model-type'), false, + null, ], 'upload ok' => [ [ @@ -896,6 +899,7 @@ public function uploadProvider(): array ], null, true, + 'image/png', ], 'generic upload error' => [ [ @@ -905,6 +909,7 @@ public function uploadProvider(): array ], new UploadException(null, 1), // !UPLOAD_ERR_OK true, + 'image/png', ], 'save with empty file' => [ [ @@ -914,6 +919,7 @@ public function uploadProvider(): array ], null, false, + null, ], 'upload remote url' => [ [ @@ -926,6 +932,7 @@ public function uploadProvider(): array 'provider' => 'YouTube', 'provider_uid' => 'v=fE50xrnJnR8', ], + null, ], ]; } @@ -936,14 +943,14 @@ public function uploadProvider(): array * @param array $requestData The request data * @param \Exception|null $expectedException The exception expected * @param array|bool $uploaded The upload result (boolean or expected requestdata) + * @param string|null $contentType The content type of the uploaded file * @return void * @covers ::upload() * @covers ::removeStream() - * @covers ::assocStreamToMedia() * @covers ::checkRequestForUpload() * @dataProvider uploadProvider() */ - public function testUpload(array $requestData, $expectedException, $uploaded): void + public function testUpload(array $requestData, $expectedException, $uploaded, ?string $contentType): void { // if upload failed, verify exception if ($expectedException != null) { @@ -957,6 +964,9 @@ public function testUpload(array $requestData, $expectedException, $uploaded): v if ($requestData['upload_behavior'] === 'file') { // do component call + if (array_key_exists('model-type', $requestData)) { + $this->Modules->getController()->setRequest($this->Modules->getController()->getRequest()->withParam('object_type', $requestData['model-type'])); + } $this->Modules->upload($requestData); } else { // mock for ModulesComponent @@ -991,7 +1001,7 @@ public function objectTypes(?bool $abstract = null): array // test upload of another file to change stream $filename = sprintf('%s/tests/files/%s', getcwd(), 'test2.png'); - $file = new UploadedFile($filename, filesize($filename), 0, $filename); + $file = new UploadedFile($filename, filesize($filename), 0, $filename, $contentType); $requestData = [ 'file' => $file, 'model-type' => 'images',