You are here

public function DropzoneJsUploadSave::createFile in DropzoneJS 8

Same name and namespace in other branches
  1. 8.2 src/DropzoneJsUploadSave.php \Drupal\dropzonejs\DropzoneJsUploadSave::createFile()

Creates a file entity form an uploaded file.

Note: files being created using this method are flagged as temporary and not saved yet.

Parameters

string $uri: The path to the file we want to upload.

string $destination: A string containing the URI that the file should be copied to. This must be a stream wrapper URI.

string $extensions: A space separated list of valid extensions.

\Drupal\Core\Session\AccountProxyInterface $user: The owner of the file.

array $validators: (Optional) Associative array of callback functions used to validate the file. See file_validate() for more documentation. Note that we add file_validate_extensions and file_validate_name_length in this method already.

Return value

\Drupal\file\FileInterface|bool The file entity of the newly uploaded file or false in case of a failure. The file isn't saved yet. That should be handled by the caller.

Overrides DropzoneJsUploadSaveInterface::createFile

File

src/DropzoneJsUploadSave.php, line 107

Class

DropzoneJsUploadSave
A service that saves files uploaded by the dropzonejs element as files.

Namespace

Drupal\dropzonejs

Code

public function createFile($uri, $destination, $extensions, AccountProxyInterface $user, array $validators = []) {

  // Create the file entity.
  $uri = file_stream_wrapper_uri_normalize($uri);
  $file_info = new \SplFileInfo($uri);

  /** @var \Drupal\file\FileInterface $file */
  $file = $this->entityTypeManager
    ->getStorage('file')
    ->create([
    'uid' => $user
      ->id(),
    'status' => 0,
    'filename' => $file_info
      ->getFilename(),
    'uri' => $uri,
    'filesize' => $file_info
      ->getSize(),
    'filemime' => $this->mimeTypeGuesser
      ->guess($uri),
  ]);

  // Replace tokens. As the tokens might contain HTML we convert it to plain
  // text.
  $destination = PlainTextOutput::renderFromHtml($this->token
    ->replace($destination));

  // Handle potentialy dangerous extensions.
  $renamed = $this
    ->renameExecutableExtensions($file);

  // The .txt extension may not be in the allowed list of extensions. We have
  // to add it here or else the file upload will fail.
  if ($renamed && !empty($extensions)) {
    $extensions .= ' txt';
    drupal_set_message($this
      ->t('For security reasons, your upload has been renamed to %filename.', [
      '%filename' => $file
        ->getFilename(),
    ]));
  }

  // Validate the file.
  $errors = $this
    ->validateFile($file, $extensions, $validators);
  if (!empty($errors)) {
    $message = [
      'error' => [
        '#markup' => $this
          ->t('The specified file %name could not be uploaded.', [
          '%name' => $file
            ->getFilename(),
        ]),
      ],
      'item_list' => [
        '#theme' => 'item_list',
        '#items' => $errors,
      ],
    ];
    drupal_set_message($this->renderer
      ->renderPlain($message), 'error');
    return FALSE;
  }

  // Prepare destination.
  if (!$this
    ->prepareDestination($file, $destination)) {
    drupal_set_message($this
      ->t('The file could not be uploaded because the destination %destination is invalid.', [
      '%destination' => $destination,
    ]), 'error');
    return FALSE;
  }

  // Move uploaded files from PHP's upload_tmp_dir to destination.
  $move_result = file_unmanaged_move($uri, $file
    ->getFileUri());
  if (!$move_result) {
    drupal_set_message($this
      ->t('File upload error. Could not move uploaded file.'), 'error');
    $this->logger
      ->notice('Upload error. Could not move uploaded file %file to destination %destination.', [
      '%file' => $file
        ->getFilename(),
      '%destination' => $file
        ->getFileUri(),
    ]);
    return FALSE;
  }

  // Set the permissions on the new file.
  $this->fileSystem
    ->chmod($file
    ->getFileUri());
  return $file;
}