diff --git a/core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php b/core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php index 979c05cb8594bf0e9f2587f3184f651c6f008a30..6682bb3a6f8ef7430dc4e56e60230f8100d9abba 100644 --- a/core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php +++ b/core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php @@ -159,7 +159,8 @@ public function __construct(LoggerInterface $logger, FileSystemInterface $file_s */ public function handleFileUploadForField(FieldDefinitionInterface $field_definition, $filename, AccountInterface $owner) { assert(is_a($field_definition->getClass(), FileFieldItemList::class, TRUE)); - $destination = $this->getUploadLocation($field_definition->getSettings()); + $settings = $field_definition->getSettings(); + $destination = $this->getUploadLocation($settings); // Check the destination file path is writable. if (!$this->fileSystem->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY)) { @@ -172,6 +173,9 @@ public function handleFileUploadForField(FieldDefinitionInterface $field_definit // Create the file. $file_uri = "{$destination}/{$prepared_filename}"; + if ($destination === $settings['uri_scheme'] . '://') { + $file_uri = "{$destination}{$prepared_filename}"; + } $temp_file_path = $this->streamUploadData(); diff --git a/core/modules/jsonapi/tests/src/Functional/FileUploadTest.php b/core/modules/jsonapi/tests/src/Functional/FileUploadTest.php index 34e0ae2bbe92b8823a5d753c9fbc41b6689d779a..a7b3465c8d839c120e75580698745b6b8e09a9c1 100644 --- a/core/modules/jsonapi/tests/src/Functional/FileUploadTest.php +++ b/core/modules/jsonapi/tests/src/Functional/FileUploadTest.php @@ -747,6 +747,27 @@ public function testFileUploadNoExtensionSetting() { $this->assertFileExists('public://foobar/example.txt'); } + /** + * Tests using the file upload POST route no directory configured. + */ + public function testFileUploadNoDirectorySetting() { + $this->setUpAuthorization('POST'); + $this->config('jsonapi.settings')->set('read_only', FALSE)->save(TRUE); + + $uri = Url::fromUri('base:' . static::$postUri); + + $this->field->setSetting('file_directory', '') + ->save(); + + $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'filename="example.txt"']); + $expected = $this->getExpectedDocument(1, 'example.txt', TRUE); + $expected['data']['attributes']['uri']['value'] = 'public://example.txt'; + $expected['data']['attributes']['uri']['url'] = base_path() . $this->siteDirectory . '/files/example.txt'; + + $this->assertResponseData($expected, $response); + $this->assertFileExists('public://example.txt'); + } + /** * {@inheritdoc} */