I am using Vich Uploader Bundle in combination with API Platform to store files. I've followed the instructions from the official documentation https://api-platform.com/docs/core/file-upload/#handling-file-upload
Everything works well except I want to include PUT calls to replace existing MediaObject entities. My attempt was as follows:
I have created a custom operationand mapped it to the PUT method in the entity as follows:
<?php
// api/src/Entity/MediaObject.php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Controller\CreateMediaObject;
use App\Controller\EditMediaObject;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity
* @ApiResource(
*
* ...
*
* itemOperations={
* "get",
* "put"={
* "controller"=EditMediaObject::class,
* "deserialize"=false,
* "validation_groups"={"Default", "media_object_create"},
* "swagger_context"={
* "consumes"={
* "multipart/form-data",
* },
* "parameters"={
* {
* "in"="formData",
* "name"="file",
* "type"="file",
* "description"="The file to upload",
* },
* },
* },
* },
* "delete"
* },
* )
* @Vich\Uploadable
*/
class MediaObject
{
/**
* @var int|null
*
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
* @ORM\Id
*/
protected $id;
/**
* @var string|null
*
* @ApiProperty(iri="http://schema.org/contentUrl")
* @Groups({"media_object_read"})
*/
public $contentUrl;
/**
* @var File|null
*
* @Assert\NotNull(groups={"media_object_create"})
* @Vich\UploadableField(mapping="media_object", fileNameProperty="filePath")
*/
public $file;
/**
* @var string|null
*
* @ORM\Column(nullable=true)
*/
public $filePath;
public function getId(): ?int
{
return $this->id;
}
}
The correponding Controller:
<?php
// api/src/Controller/EditMediaObject.php
namespace App\Controller;
use App\Entity\MediaObject;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
final class EditMediaObject
{
/**
* @var RequestStack
*/
private $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public function __invoke(MediaObject $data): MediaObject
{
$request = $this->requestStack->getCurrentRequest();
$uploadedFile = $request->attributes->get('file');
if (!$uploadedFile) {
throw new BadRequestHttpException ('"file" is required');
}
$data->file = $uploadedFile;
return $data;
}
}
The $data object is susccessfully populated. However I dont have access to the initial request parameters (particularly 'file').
Is this because of the preliminary OPTIONS call? If so, how can I forward / preserve the file to attach it to the MediaObject?
Thanks in advance
Edit:
In case anyone is interested, it is NOT possible to send file parameters over PUT requests in PHP. (source: PHP multipart form data PUT request?)