Skip to content

Commit 39655fc

Browse files
Remove processor from file upload (#1956)
* docs: remove processor for file uploading Remove processor class from file uploading documentation since Vich manage the file creation and set the filePath * chore: typo in MediaObjectNormalizer.php * remove unnecessary line --------- Co-authored-by: soyuka <[email protected]>
1 parent 4714e7b commit 39655fc

File tree

1 file changed

+5
-56
lines changed

1 file changed

+5
-56
lines changed

core/file-upload.md

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,10 @@ before proceeding. It will help you get a grasp on how the bundle works, and why
1111
**Note**: Uploading files won't work in `PUT` or `PATCH` requests, you must use `POST` method to upload files.
1212
See [the related issue on Symfony](https://github.com/symfony/symfony/issues/9226) and [the related bug in PHP](https://bugs.php.net/bug.php?id=55815) talking about this behavior.
1313

14-
Previously to API Platform 3.3, file upload was using controllers that needs:
14+
Enable the multipart format globally in order to use it as the input format of your resource:
1515

1616
```yaml
1717
api_platform:
18-
use_symfony_listeners: true
19-
```
20-
21-
Since 3.3, we recommend to use a Processor, note that you need to enable the multipart format globally:
22-
23-
```yaml
24-
api_platform:
25-
use_symfony_listeners: false
2618
formats:
2719
multipart: ['multipart/form-data']
2820
jsonld: ['application/ld+json']
@@ -61,9 +53,6 @@ In this example, we will create a `MediaObject` API resource. We will post files
6153
to this resource endpoint, and then link the newly created resource to another
6254
resource (in our case: `Book`).
6355

64-
This example will use a custom controller to receive the file.
65-
The second example will use a custom `multipart/form-data` decoder to deserialize the resource instead.
66-
6756
### Configuring the Resource Receiving the Uploaded File
6857

6958
The `MediaObject` resource is implemented like this:
@@ -98,9 +87,6 @@ use Vich\UploaderBundle\Mapping\Annotation as Vich;
9887
new GetCollection(),
9988
new Post(
10089
inputFormats: ['multipart' => ['multipart/form-data']],
101-
processor: SaveMediaObject::class,
102-
deserialize: false,
103-
validationContext: ['groups' => ['Default', 'media_object_create']],
10490
openapi: new Model\Operation(
10591
requestBody: new Model\RequestBody(
10692
content: new \ArrayObject([
@@ -126,14 +112,15 @@ class MediaObject
126112
#[ORM\Id, ORM\Column, ORM\GeneratedValue]
127113
private ?int $id = null;
128114
129-
#[ApiProperty(types: ['https://schema.org/contentUrl'])]
115+
#[ApiProperty(types: ['https://schema.org/contentUrl'], writable: false)]
130116
#[Groups(['media_object:read'])]
131117
public ?string $contentUrl = null;
132118
133119
#[Vich\UploadableField(mapping: 'media_object', fileNameProperty: 'filePath')]
134-
#[Assert\NotNull(groups: ['media_object_create'])]
120+
#[Assert\NotNull]
135121
public ?File $file = null;
136122
123+
#[ApiProperty(writable: false)]
137124
#[ORM\Column(nullable: true)]
138125
public ?string $filePath = null;
139126
@@ -145,45 +132,6 @@ class MediaObject
145132
```
146133
Note: From V3.3 onwards, `'multipart/form-data'` must either be including in the global API-Platform config, either in `formats` or `defaults->inputFormats`, or defined as an `inputFormats` parameter on an operation by operation basis.
147134

148-
### Creating the Processor
149-
150-
At this point, the entity is configured, but we still need to write the processor
151-
that handles the file upload.
152-
153-
```php
154-
<?php
155-
// api/src/State/SaveMediaObject.php
156-
157-
namespace App\State;
158-
159-
use ApiPlatform\Metadata\Operation;
160-
use ApiPlatform\State\ProcessorInterface;
161-
use App\Entity\MediaObject;
162-
use Symfony\Component\DependencyInjection\Attribute\Autowire;
163-
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
164-
use Vich\UploaderBundle\Storage\StorageInterface;
165-
166-
final class SaveMediaObject implements ProcessorInterface
167-
{
168-
public function __construct(
169-
#[Autowire('@api_platform.doctrine.orm.state.persist_processor')]
170-
private readonly ProcessorInterface $processor
171-
) {}
172-
173-
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
174-
{
175-
$uploadedFile = $context['request']->files->get('file');
176-
if (!$uploadedFile) {
177-
throw new BadRequestHttpException('"file" is required');
178-
}
179-
180-
$mediaObject = new MediaObject();
181-
$mediaObject->file = $uploadedFile;
182-
return $this->processor->process($mediaObject, $operation, $uriVariables, $context);
183-
}
184-
}
185-
```
186-
187135
### Resolving the File URL
188136

189137
Returning the plain file path on the filesystem where the file is stored is not useful for the client, which needs a
@@ -193,6 +141,7 @@ A [normalizer](serialization.md#normalization) could be used to set the `content
193141

194142
```php
195143
<?php
144+
// api/src/Serializer/MediaObjectNormalizer.php
196145
197146
namespace App\Serializer;
198147

0 commit comments

Comments
 (0)