Problem/Motivation
In #2940383: [PP-4] Unify file upload logic of REST and JSON:API we are wanting to upload files directly via octet-stream and Content-Disposition header, and because it doesn't use $_POST variables we can't use Symfony UploadedFile object. However, in \Drupal\file\Upload\FileUploadHandler::handleFileUpload() we call isValid() and assume we have an instance of UploadedFile.
This is most likely due to copying existing code.
Steps to reproduce
Proposed resolution
- Deprecate
\Drupal\file\Upload\UploadedFileInterface::isValid()::getError()and::getErrorMessage() - Add an \Drupal\file\Validation\UploadedFileValidator that validates uploaded files using \Drupal\file\Validation\Constraint\UploadedFileConstraint and \Drupal\file\Validation\Constraint\UploadedFileConstraintValidator
- Remove the checks for
isValid()and throwing exceptions inFileUploadHandler::handleFileUpload() - Call UploadedFileValidator::validate() and check for constraint violations instead
Remaining tasks
Do it.
User interface changes
API changes
\Drupal\file\Upload\UploadedFileInterface::isValid()::getError() and ::getErrorMessage() are deprecated
A new UploadedFileInterface::validate() method is added, that throws \Symfony\Component\HttpFoundation\File\Exception\FileException and sub-classes.