Unverified Commit 384d615a authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3245244 by kim.pepper, dww: Make FileUploadHandler re-usuable for non-form file uploads

parent 8065ee3e
Loading
Loading
Loading
Loading
+3 −1
Changes for core/modules/file/file.module: 3 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
use Drupal\file\Upload\FileValidationException;
use Drupal\file\Upload\FormUploadedFile;
use Symfony\Component\HttpFoundation\File\Exception\FileException as SymfonyFileException;
use Symfony\Component\HttpFoundation\File\Exception\FormSizeFileException;
use Symfony\Component\HttpFoundation\File\Exception\IniSizeFileException;
@@ -913,7 +914,8 @@ function file_save_upload($form_field_name, $validators = [], $destination = FAL
  /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $uploaded_file */
  foreach ($uploaded_files as $i => $uploaded_file) {
    try {
      $result = $file_upload_handler->handleFileUpload($uploaded_file, $validators, $destination, $replace);
      $form_uploaded_file = new FormUploadedFile($uploaded_file);
      $result = $file_upload_handler->handleFileUpload($form_uploaded_file, $validators, $destination, $replace);
      $file = $result->getFile();
      // If the filename has been modified, let the user know.
      if ($result->isRenamed()) {
+24 −4
Changes for core/modules/file/src/Upload/FileUploadHandler.php: 24 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@
use Symfony\Component\HttpFoundation\File\Exception\NoFileException;
use Symfony\Component\HttpFoundation\File\Exception\NoTmpDirFileException;
use Symfony\Component\HttpFoundation\File\Exception\PartialFileException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Mime\MimeTypeGuesserInterface;

@@ -115,7 +114,7 @@ public function __construct(FileSystemInterface $fileSystem, EntityTypeManagerIn
  /**
   * Creates a file from an upload.
   *
   * @param \Symfony\Component\HttpFoundation\File\UploadedFile $uploadedFile
   * @param \Drupal\file\Upload\UploadedFileInterface $uploadedFile
   *   The uploaded file object.
   * @param array $validators
   *   The validators to run against the uploaded file.
@@ -140,7 +139,7 @@ public function __construct(FileSystemInterface $fileSystem, EntityTypeManagerIn
   * @throws \Drupal\file\Upload\FileValidationException
   *   Thrown when file validation fails.
   */
  public function handleFileUpload(UploadedFile $uploadedFile, array $validators = [], string $destination = 'temporary://', int $replace = FileSystemInterface::EXISTS_REPLACE): FileUploadResult {
  public function handleFileUpload(UploadedFileInterface $uploadedFile, array $validators = [], string $destination = 'temporary://', int $replace = FileSystemInterface::EXISTS_REPLACE): FileUploadResult {
    $originalName = $uploadedFile->getClientOriginalName();

    if (!$uploadedFile->isValid()) {
@@ -217,7 +216,8 @@ public function handleFileUpload(UploadedFile $uploadedFile, array $validators =
    }

    $file->setFileUri($destinationFilename);
    if (!$this->fileSystem->moveUploadedFile($uploadedFile->getRealPath(), $file->getFileUri())) {

    if (!$this->moveUploadedFile($uploadedFile, $file->getFileUri())) {
      throw new FileWriteException('File upload error. Could not move uploaded file.');
    }

@@ -270,6 +270,26 @@ public function handleFileUpload(UploadedFile $uploadedFile, array $validators =
    return $result;
  }

  /**
   * Move the uploaded file from the temporary path to the destination.
   *
   * @todo Allows a sub-class to override this method in order to handle
   * raw file uploads in https://www.drupal.org/project/drupal/issues/2940383.
   *
   * @param \Drupal\file\Upload\UploadedFileInterface $uploadedFile
   *   The uploaded file.
   * @param string $uri
   *   The destination URI.
   *
   * @return bool
   *   Returns FALSE if moving failed.
   *
   * @see https://www.drupal.org/project/drupal/issues/2940383
   */
  protected function moveUploadedFile(UploadedFileInterface $uploadedFile, string $uri) {
    return $this->fileSystem->moveUploadedFile($uploadedFile->getRealPath(), $uri);
  }

  /**
   * Gets the list of allowed extensions and updates the validators.
   *
+85
Changes for core/modules/file/src/Upload/FormUploadedFile.php: 85 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\file\Upload;

use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * Provides a bridge to Symfony UploadedFile.
 */
class FormUploadedFile implements UploadedFileInterface {

  /**
   * The wrapped uploaded file.
   *
   * @var \Symfony\Component\HttpFoundation\File\UploadedFile
   */
  protected $uploadedFile;

  /**
   * Creates a new FormUploadedFile.
   *
   * @param \Symfony\Component\HttpFoundation\File\UploadedFile $uploadedFile
   *   The wrapped Symfony uploaded file.
   */
  public function __construct(UploadedFile $uploadedFile) {
    $this->uploadedFile = $uploadedFile;
  }

  /**
   * {@inheritdoc}
   */
  public function getClientOriginalName(): string {
    return $this->uploadedFile->getClientOriginalName();
  }

  /**
   * {@inheritdoc}
   */
  public function isValid(): bool {
    return $this->uploadedFile->isValid();
  }

  /**
   * {@inheritdoc}
   */
  public function getErrorMessage(): string {
    return $this->uploadedFile->getErrorMessage();
  }

  /**
   * {@inheritdoc}
   */
  public function getError(): int {
    return $this->uploadedFile->getError();
  }

  /**
   * {@inheritdoc}
   */
  public function getSize(): int {
    return $this->uploadedFile->getSize();
  }

  /**
   * {@inheritdoc}
   */
  public function getRealPath() {
    return $this->uploadedFile->getRealPath();
  }

  /**
   * {@inheritdoc}
   */
  public function getPathname(): string {
    return $this->uploadedFile->getPathname();
  }

  /**
   * {@inheritdoc}
   */
  public function getFilename(): string {
    return $this->uploadedFile->getFilename();
  }

}
+88
Changes for core/modules/file/src/Upload/UploadedFileInterface.php: 88 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\file\Upload;

/**
 * Provides an interface for uploaded files.
 */
interface UploadedFileInterface {

  /**
   * Returns the original file name.
   *
   * The file name is extracted from the request that uploaded the file and as
   * such should not be considered a safe value.
   *
   * @return string
   *   The original file name supplied by the client.
   */
  public function getClientOriginalName(): string;

  /**
   * Returns whether the file was uploaded successfully.
   *
   * @return bool
   *   TRUE if the file has been uploaded with HTTP and no error occurred.
   */
  public function isValid(): bool;

  /**
   * Returns an informative upload error message.
   *
   * @return string
   *   The error message regarding a failed upload.
   */
  public function getErrorMessage(): string;

  /**
   * Returns the upload error code.
   *
   * If the upload was successful, the constant UPLOAD_ERR_OK is returned.
   * Otherwise, one of the other UPLOAD_ERR_XXX constants is returned.
   *
   * @return int
   *   The upload error code.
   */
  public function getError(): int;

  /**
   * Gets file size.
   *
   * @return int
   *   The filesize in bytes.
   *
   * @see https://www.php.net/manual/en/splfileinfo.getsize.php
   */
  public function getSize(): int;

  /**
   * Gets the absolute path to the file.
   *
   * @return string|false
   *   The path to the file, or FALSE if the file does not exist.
   *
   * @see https://php.net/manual/en/splfileinfo.getrealpath.php
   */
  public function getRealPath();

  /**
   * Gets the path to the file.
   *
   * @return string
   *   The path to the file.
   *
   * @see https://php.net/manual/en/splfileinfo.getpathname.php
   */
  public function getPathname(): string;

  /**
   * Gets the filename.
   *
   * @return string
   *   The filename.
   *
   * @see https://php.net/manual/en/splfileinfo.getfilename.php
   */
  public function getFilename(): string;

}
+2 −2
Changes for core/modules/file/tests/src/Kernel/FileUploadHandlerTest.php: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -3,9 +3,9 @@
namespace Drupal\Tests\file\Kernel;

use Drupal\Component\Utility\Environment;
use Drupal\file\Upload\UploadedFileInterface;
use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\HttpFoundation\File\Exception\FormSizeFileException;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * Tests the file upload handler.
@@ -39,7 +39,7 @@ protected function setUp(): void {
   */
  public function testFileSaveUploadSingleErrorFormSize() {
    $file_name = $this->randomMachineName();
    $file_info = $this->createMock(UploadedFile::class);
    $file_info = $this->createMock(UploadedFileInterface::class);
    $file_info->expects($this->once())->method('getError')->willReturn(UPLOAD_ERR_FORM_SIZE);
    $file_info->expects($this->once())->method('getClientOriginalName')->willReturn($file_name);
    $file_info->expects($this->once())->method('getErrorMessage')->willReturn(sprintf('The file "%s" could not be saved because it exceeds %s, the maximum allowed size for uploads.', $file_name, format_size(Environment::getUploadMaxSize())));