diff --git a/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php b/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php index 7be618c13c8..35211cbf84b 100644 --- a/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php +++ b/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php @@ -330,6 +330,12 @@ class FileUploadResource extends ResourceBase { } $file->setFileUri($file_uri); + // Update the filename with any changes as a result of security or renaming + // due to an existing file. + // @todo Remove this duplication by replacing with FileUploadHandler. See + // https://www.drupal.org/project/drupal/issues/3401734 + $file->setFilename($this->fileSystem->basename($file->getFileUri())); + // Move the file to the correct location after validation. Use // FileSystemInterface::EXISTS_ERROR as the file location has already been // determined above in FileSystem::getDestinationFilename(). diff --git a/core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php b/core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php index 864f5e36810..7e348af06d8 100644 --- a/core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php +++ b/core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php @@ -245,6 +245,13 @@ class TemporaryJsonapiFileFieldUploader { } $file->setFileUri($file_uri); + + // Update the filename with any changes as a result of security or renaming + // due to an existing file. + // @todo Remove this duplication by replacing with FileUploadHandler. See + // https://www.drupal.org/project/drupal/issues/3401734 + $file->setFilename($this->fileSystem->basename($file->getFileUri())); + // Move the file to the correct location after validation. Use // FileSystemInterface::EXISTS_ERROR as the file location has already been // determined above in FileSystem::getDestinationFilename(). diff --git a/core/modules/jsonapi/tests/src/Functional/FileUploadTest.php b/core/modules/jsonapi/tests/src/Functional/FileUploadTest.php index d028d094992..07f9dedd807 100644 --- a/core/modules/jsonapi/tests/src/Functional/FileUploadTest.php +++ b/core/modules/jsonapi/tests/src/Functional/FileUploadTest.php @@ -249,7 +249,7 @@ class FileUploadTest extends ResourceTestBase { // header with no 'file' prefix. $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'filename="example.txt"']); $this->assertSame(201, $response->getStatusCode()); - $expected = $this->getExpectedDocument(2, 'example_0.txt'); + $expected = $this->getExpectedDocument(2, 'example_0.txt', TRUE); $this->assertResponseData($expected, $response); // Check the actual file data. @@ -340,7 +340,7 @@ class FileUploadTest extends ResourceTestBase { 'data' => [ 0 => $this->getExpectedDocument(1, 'existing.txt', TRUE, TRUE)['data'], 1 => $this->getExpectedDocument(2, 'example.txt', TRUE, TRUE)['data'], - 2 => $this->getExpectedDocument(3, 'example_0.txt', FALSE, TRUE)['data'], + 2 => $this->getExpectedDocument(3, 'example_0.txt', TRUE, TRUE)['data'], ], ]; $this->assertResponseData($expected, $response); @@ -448,7 +448,7 @@ class FileUploadTest extends ResourceTestBase { $this->assertSame(201, $response->getStatusCode()); // Loading expected normalized data for file 2, the duplicate file. - $expected = $this->getExpectedDocument(2, 'example_0.txt'); + $expected = $this->getExpectedDocument(2, 'example_0.txt', TRUE); $this->assertResponseData($expected, $response); // Check the actual file data. diff --git a/core/modules/rest/tests/src/Functional/FileUploadResourceTestBase.php b/core/modules/rest/tests/src/Functional/FileUploadResourceTestBase.php index 7ca019aeda1..712250ed82d 100644 --- a/core/modules/rest/tests/src/Functional/FileUploadResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/FileUploadResourceTestBase.php @@ -195,7 +195,7 @@ abstract class FileUploadResourceTestBase extends ResourceTestBase { // header with no 'file' prefix. $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'filename="example.txt"']); $this->assertSame(201, $response->getStatusCode()); - $expected = $this->getExpectedNormalizedEntity(2, 'example_0.txt'); + $expected = $this->getExpectedNormalizedEntity(2, 'example_0.txt', TRUE); $this->assertResponseData($expected, $response); // Check the actual file data. @@ -317,7 +317,7 @@ abstract class FileUploadResourceTestBase extends ResourceTestBase { $this->assertSame(201, $response->getStatusCode()); // Loading expected normalized data for file 2, the duplicate file. - $expected = $this->getExpectedNormalizedEntity(2, 'example_0.txt'); + $expected = $this->getExpectedNormalizedEntity(2, 'example_0.txt', TRUE); $this->assertResponseData($expected, $response); // Check the actual file data.