Unverified Commit 7d047b20 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3214675 by el7cosmos, bbrala, hehongbo, larowlan, alexpott: JSON:API...

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

(cherry picked from commit a8d3f780)
parent fbb8e860
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -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();

+21 −0
Original line number Diff line number Diff line
@@ -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}
   */