Problem/Motivation
In #2940383: [META] Unify file upload logic of REST and JSON:API we are trying to re-use \Drupal\file\Upload\FileUploadHandler::handleFileUpload() However, that method takes a \Symfony\Component\HttpFoundation\File\UploadedFile as its argument.
Through investigation, we have found that UploadedFile is only really mean for multi-part form file uploads which set the $_FILES superglobal. The methods UploadedFile::isValid() andUploadedFile::move() call the PHP is_uploaded_file() and move_uploaded_file() methods which only work for files uploaded via forms.
We don't call UploadedFile::move() directly because \Drupal\file\Upload\FileUploadHandler::handleFileUpload() makes a call to our own \Drupal\Core\File\FileSystemInterface::moveUploadedFile(). However that does call move_uploaded_file() in its implementation.
On the otherhand, the REST and JSON API approaches do not use multi-part form file uploads. The POST the files as the body, and use the content-disposition header to specify the filename. We need to read the file contents from the php://input and save it to a temp directory ourselves, then move it to the destination directory once validation is complete.
Steps to reproduce
Proposed resolution
We can replace UploadedFile with a new UploadedFileInterface and pass that to FileUploadHandler::handleFileUpload(). This will allow us to have a bridge to the Symfony UploadedFile and allow us to create our own RawUploadedFile to handle raw uploaded files for JSON API and REST modules.
NB. GraphQL module is using Symfony UploadedFile so not sure how they get around those limitations.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | 3245244--interdiff.txt | 655 bytes | kim.pepper |
| #6 | 3245244-6.patch | 9.88 KB | kim.pepper |
Comments
Comment #2
larowlanThis is a 9.3 target, so bumping to major
Comment #3
kim.pepperInitial patch for proposed solution.
Comment #4
dwwThanks for getting this going, @kim.pepper. Definitely important to land this quickly so we're not trapped.
Most of this code looks very familiar from reviewing #2940383: [META] Unify file upload logic of REST and JSON:API. It's all pretty solid and straight forward.
I found a bunch of tiny docs nits, which I'm fixing via a patch/interdiff instead of a long dreditor review. Also added return types for
getPathname()andgetFilename(). Can't cleanly do so forgetRealPath()until we require PHP8 and can use a union type (e.g.string|FALSE).My only concerns before RTBC:
But the method seems to directly assume a
FormUploadedFile. Or maybe the intention is thatNonFormUploadedFile::getRealPath()(for lack of a better name) will actually be what provides the magic to allow reuse? In that case, maybe the above comment is misplaced?My sense is that it's not gonna fly to commit this without proof that the reuse is actually possible, or at least a documented plan on when/where that's gonna happen.
But overall, this seems like a small, simple, important change so the new
core/modules/file/src/Upload/FileUploadHandler.phpin 9.3.x isn't locked in with a public API we can't reuse.Comment #5
kim.pepperRe: #4
\Drupal\Tests\file\Functional\SaveUploadTestand sub-class.The intention in #2940383: [META] Unify file upload logic of REST and JSON:API is to sub-class
FileUploadHandlerand override this method. Because both form and raw uploads are moved by calling services it made it hard implement that inside an UploadedFileInterface implementation. Happy for alternate suggestions!Comment #6
kim.pepperFix code style.
Comment #7
dwwRe: #5:
core/modules/file/tests/src/Functional/SaveUploadTest.php. When I re-brokeFormUploadedFilein the way I described in #4.1 and ran that, I got a sea of errors. 🎉 Noice. Removing the tag.Given that all I touched in here were nitpicky docs improvements, I think I'm still qualified to RTBC this. Since it's urgent to fix this before 9.3.0-alpha1, let's get it in. It gives us a level of indirection to help us get around limitations in various underlying systems that are standing in the way of a unified solution at #2940383: [META] Unify file upload logic of REST and JSON:API. We might not get that completely done before alpha deadline, but at least we won't paint ourselves into a corner with "generalizations" that only work with form uploads.
Hopefully core committers will agree that we're gonna need this, even if we're not actually using it just yet. 🤞
Thanks!
-Derek
Comment #8
larowlanFixed on commit
It took me a while to work out why this was needed, but then I realised that the move code in the sub-class in #2940383: [META] Unify file upload logic of REST and JSON:API is using
FileSystemInterface;:moveand here we useFileSystemInterface::moveUploadedFileCommitted 384d615 and pushed to 9.3.x. Thanks!