Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove a deprecation in upload docs #1871

Merged
merged 3 commits into from
Jan 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 25 additions & 27 deletions core/file-upload.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ to make it look like this.

```yaml
# api/config/packages/vich_uploader.yaml

vich_uploader:
db_driver: orm
metadata:
Expand All @@ -34,7 +35,7 @@ vich_uploader:
uri_prefix: /media
upload_destination: '%kernel.project_dir%/public/media'
# Will rename uploaded files using a uniqueid as a prefix.
namer: Vich\UploaderBundle\Naming\OrignameNamer
namer: Vich\UploaderBundle\Naming\SmartUniqueNamer
```

## Uploading to a Dedicated Resource
Expand All @@ -53,6 +54,7 @@ The `MediaObject` resource is implemented like this:
```php
<?php
// api/src/Entity/MediaObject.php

namespace App\Entity;

use ApiPlatform\Metadata\ApiProperty;
Expand Down Expand Up @@ -109,7 +111,7 @@ class MediaObject
#[Groups(['media_object:read'])]
public ?string $contentUrl = null;

#[Vich\UploadableField(mapping: "media_object", fileNameProperty: "filePath")]
#[Vich\UploadableField(mapping: 'media_object', fileNameProperty: 'filePath')]
#[Assert\NotNull(groups: ['media_object_create'])]
public ?File $file = null;

Expand Down Expand Up @@ -172,12 +174,11 @@ A [normalizer](serialization.md#normalization) could be used to set the `content
namespace App\Serializer;

use App\Entity\MediaObject;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Vich\UploaderBundle\Storage\StorageInterface;

final class MediaObjectNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
final class MediaObjectNormalizer implements NormalizerAwareInterface
{
use NormalizerAwareTrait;

Expand Down Expand Up @@ -224,17 +225,23 @@ your data, you will get a response looking like this:

### Accessing Your Media Objects Directly

You will need to modify your Caddyfile to allow the above `contentUrl` to be accessed directly. If you followed the above configuration for the VichUploaderBundle, that will be in `api/public/media`. Add your folder to the list of path matches, e.g. `|^/media/|`:

```caddyfile
...
# Matches requests for HTML documents, for static files and for Next.js files,
# except for known API paths and paths with extensions handled by API Platform
@pwa expression `(
{header.Accept}.matches("\\btext/html\\b")
&& !{path}.matches("(?i)(?:^/docs|^/graphql|^/bundles/|^/media/|^/_profiler|^/_wdt|\\.(?:json|html$|csv$|ya?ml$|xml$))")
...
You will need to modify your `Caddyfile` to allow the above `contentUrl` to be accessed directly. If you followed the above configuration for the VichUploaderBundle, that will be in `api/public/media`. Add your folder to the list of path matches, e.g. `|^/media/|`:

<!-- markdownlint-disable no-hard-tabs -->
```patch
# Matches requests for HTML documents, for static files and for Next.js files,
# except for known API paths and paths with extensions handled by API Platform
@pwa expression `(
header({'Accept': '*text/html*'})
&& !path(
- '/docs*', '/graphql*', '/bundles*', '/media*', '/contexts*', '/_profiler*', '/_wdt*',
+ '/media*', '/docs*', '/graphql*', '/bundles*', '/media*', '/contexts*', '/_profiler*', '/_wdt*',
'*.json*', '*.html', '*.csv', '*.yml', '*.yaml', '*.xml'
)
)
|| path('/favicon.ico', '/manifest.json', '/robots.txt', '/_next*', '/sitemap*')`
```
<!-- markdownlint-enable no-hard-tabs -->

### Linking a MediaObject Resource to Another Resource

Expand All @@ -246,6 +253,7 @@ We first need to edit our Book resource, and add a new property called `image`.
```php
<?php
// api/src/Entity/Book.php

namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
Expand Down Expand Up @@ -406,11 +414,10 @@ final class MultipartDecoder implements DecoderInterface
{
public const FORMAT = 'multipart';

public function __construct(private RequestStack $requestStack) {}
public function __construct(private RequestStack $requestStack)
{
}

/**
* {@inheritdoc}
*/
public function decode(string $data, string $format, array $context = []): ?array
{
$request = $this->requestStack->getCurrentRequest();
Expand All @@ -427,9 +434,6 @@ final class MultipartDecoder implements DecoderInterface
}, $request->request->all()) + $request->files->all();
}

/**
* {@inheritdoc}
*/
public function supportsDecoding(string $format): bool
{
return self::FORMAT === $format;
Expand All @@ -452,17 +456,11 @@ use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

final class UploadedFileDenormalizer implements DenormalizerInterface
{
/**
* {@inheritdoc}
*/
public function denormalize($data, string $type, string $format = null, array $context = []): UploadedFile
{
return $data;
}

/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null): bool
{
return $data instanceof UploadedFile;
Expand Down