The current state of file upload in api platform is unfortunately still a difficulty. I understand that it might not be in the scope of the project to handle uploads, but I think file upload is a really common feature in any web app and the project should make it easier for everyone to integrate upload in their api-platform app.
By the recent research, I found two schools for file upload:
Small uploads can be formatted as a JSON structure and sent to the API. No big deal to implement. There are already plenty of resource available in the issues: See https://github.com/api-platform/api-platform/issues/12 or https://github.com/benjaminrau/media-api.
Large uploads are more problematic. They cannot be put in a JSON structure. It forces the user to create a specific controller outside of the api-platform realm. It thus bypass the event system, and all the benefits of them (auto-persistence, auto serialization of created entity, auto serialization of errors, auto-endpoint documentation, etc).
What would be the best way to create a custom controller and still benefits from api-platform? Have you solve these issues? If so, would you mind sharing them so that we can update the documentation accordingly?
Thanks!
To try to advance the discussion, what do you think about an integration of
https://github.com/flowjs/flow-php-server
Seems work fine for large file upload using chunk ?
Maybe with an "uploadOperation" ApiResource configuration like collection/itemOperation ?
Here is the code of an action to upload a file: https://gist.github.com/marcw/7dfbb224c8973d6fa1df79e07c5e0a7c
I think it's still best done outside of API Platform. Don't see how a file upload can benefit from what API Platform provides...
It's quite possible to build a REST-ful hypermedia file upload API. But that would be a separate project altogether...
@teohhanhui with the raise of our frontend tools, it's a must have feature for an upcoming full-stack framework.
Full-stack frameworks never die, do they? I thought we'd be rid of them already... :stuck_out_tongue_winking_eye:
"decoupled full-stack framework" 馃槺
Swagger support multipart/form-data uploads (https://swagger.io/docs/specification/file-upload/ )
That could be interesting benefit for simple upload integration ?
@teohhanhui Having it inside the framework will greatly improve DX
Unfortunately, we are limited by PHP here. The Serializer cannot handle multipart/form-data easily. See:
symfony/symfony#9226
symfony/symfony#10381
If we want to do this, we'd need to find (or write?) a reliable decoder.
Any news ?
Someone wants to take this ?
IMHO the best way is to document how to handle file upload using VichFileBundle and a custom controller. Uploading with json seems like a waste of resources.
@marcw Do you have some time to do it ? I think I will close this one and create an issue linking to this one in the docs repo.
I can't do it now, but please close and open an issue in the docs repo and mention me. I'll do that in may.
Thank you @marcw for doing this
There is two things that should be, I think, in the doc :
Anyone can point a link on an exemple about how to correctly handle file definition / upload in the Entity ?
Thank you @teohhanhui. That is for the entity side but how to make uploading available inside API doc and Admin.
Doc section "Managing Files and Images" say to "find every ImageObject resources. For each contentUrl fields, we will use ImageField as field and ImageInput as input." => Is there somewhere an exemple of such a definition ?
It also tells you to create a special action "image/upload" : Is there an exemple for that ?
Cheers
I'll let others help you with the admin / client part as I'm not familiar with React. :smile:
It also tells you to create a special action "image/upload" : Is there an exemple for that ?
For the /images/upload, just create a custom operation which uses your own action / controller class: https://api-platform.com/docs/core/operations/#creating-custom-operations-and-controllers
You will probably use the Symfony Form component in that action / controller. VichUploaderBundle provides the relevant form types:
https://github.com/dustin10/VichUploaderBundle/blob/1.8.3/Resources/doc/form/vich_file_type.md
https://github.com/dustin10/VichUploaderBundle/blob/1.8.3/Resources/doc/form/vich_image_type.md
Something like that would be great https://github.com/fre5h/VichUploaderSerializationBundle
Perhaps VichUploaderBundle itself could provide the required normalizer for the Symfony Serializer...
Most helpful comment
"decoupled full-stack framework" 馃槺