Issue #3214675 by el7cosmos, bbrala, hehongbo, larowlan, alexpott: JSON:API Cannot upload files to public file root (Gets 422 Unprocessable Entity)

merge-requests/1216/head
Alex Pott 2021-09-20 14:21:36 +01:00
parent 61b7e46ba0
commit a8d3f780cc
No known key found for this signature in database
GPG Key ID: 31905460D4A69276
2 changed files with 26 additions and 1 deletions

View File

@ -159,7 +159,8 @@ class TemporaryJsonapiFileFieldUploader {
*/
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 @@ class TemporaryJsonapiFileFieldUploader {
// Create the file.
$file_uri = "{$destination}/{$prepared_filename}";
if ($destination === $settings['uri_scheme'] . '://') {
$file_uri = "{$destination}{$prepared_filename}";
}
$temp_file_path = $this->streamUploadData();

View File

@ -740,6 +740,27 @@ class FileUploadTest extends ResourceTestBase {
$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}
*/