Skip to content

Commit

Permalink
fix: user can now update their profite picture
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryde committed Jul 6, 2024
1 parent 305515a commit 256362b
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 59 deletions.
4 changes: 2 additions & 2 deletions assets/js/views/user/Settings/Picture/FormChangePicture.vue
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ export default {
if (canvas) {
const form = new FormData();
canvas.toBlob(async blob => {
form.append('file', blob);
form.append('imageFile', blob);
try {
await userApi.changePicture(form);
this.$root.$emit(EVENT_PROFILE_PICTURE_SUCCESS)
await this.$store.dispatch('user/refresh');
this.$root.$emit(EVENT_PROFILE_PICTURE_MODAL_CLOSE);
this.$emit('close');
} catch (e) {
this.errors = e.response.data.map(error => error.message);
this.errors = e.response.data.violations.map(error => error.message);
}
}, 'image/jpeg');
}
Expand Down
2 changes: 1 addition & 1 deletion config/packages/api_platform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ api_platform:
extra_properties:
standard_put: true
rfc_7807_compliant_errors: true
event_listeners_backward_compatibility_layer: false
use_symfony_listeners: false
keep_legacy_inflector: false
exception_to_status:
# Symfony
Expand Down
45 changes: 0 additions & 45 deletions src/Controller/Api/Media/User/CreateUserProfilePictureAction.php

This file was deleted.

23 changes: 12 additions & 11 deletions src/Entity/Image/UserProfilePicture.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Post;
use App\ApiResource\Search\MusicianSearchResult;
use App\Controller\Api\Media\User\CreateUserProfilePictureAction;
use ApiPlatform\OpenApi\Model;
use App\Entity\Comment\Comment;
use App\Entity\Forum\ForumPost;
use App\Entity\Message\MessageThreadMeta;
use App\Entity\User;
use App\State\Processor\User\UserProfilePictureProcessor;
use DateTimeImmutable;
use DateTimeInterface;
use Doctrine\DBAL\Types\Types;
Expand All @@ -25,27 +26,27 @@
#[ApiResource(
operations: [
new Post(
controller: CreateUserProfilePictureAction::class,
openapiContext: [
'requestBody' => [
'content' => [
inputFormats: ['multipart' => ['multipart/form-data']],
openapi: new Model\Operation(
requestBody: new Model\RequestBody(
content: new \ArrayObject([
'multipart/form-data' => [
'schema' => [
'type' => 'object',
'properties' => [
'file' => [
'imageFile' => [
'type' => 'string',
'format' => 'binary'
]
]
]
]
]
]
],
])
)
),
security: "is_granted('IS_AUTHENTICATED_REMEMBERED')",
deserialize: false,
name: 'api_user_profile_picture_post'
name: 'api_user_profile_picture_post',
processor: UserProfilePictureProcessor::class
)
],
)]
Expand Down
27 changes: 27 additions & 0 deletions src/Serializer/Denormalizer/UploadedFileDenormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Serializer\Denormalizer;

use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
class UploadedFileDenormalizer implements DenormalizerInterface
{
public function denormalize($data, string $type, string $format = null, array $context = []): File
{
return $data;
}

public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
{
return $data instanceof File;
}

public function getSupportedTypes(?string $format): array
{
return [
'object' => null,
'*' => false,
File::class => true,
];
}
}
35 changes: 35 additions & 0 deletions src/Serializer/Encoder/MultipartDecoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Serializer\Encoder;

use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Serializer\Encoder\DecoderInterface;

final class MultipartDecoder implements DecoderInterface
{
public const FORMAT = 'multipart';

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

public function decode(string $data, string $format, array $context = []): ?array
{
$request = $this->requestStack->getCurrentRequest();
if (!$request) {
return null;
}

return array_map(static function (string $element) {
// Multipart form values will be encoded in JSON.
$decoded = json_decode($element, true);

return \is_array($decoded) ? $decoded : $element;
}, $request->request->all()) + $request->files->all();
}

public function supportsDecoding(string $format): bool
{
return self::FORMAT === $format;
}
}
42 changes: 42 additions & 0 deletions src/State/Processor/User/UserProfilePictureProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\State\Processor\User;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\Entity\Image\UserProfilePicture;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\SecurityBundle\Security;

class UserProfilePictureProcessor implements ProcessorInterface
{
public function __construct(
private readonly Security $security,
private readonly EntityManagerInterface $entityManager
) {
}

/**
* @param UserProfilePicture $data
*/
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
{
dump($data);
/** @var User $user */
$user = $this->security->getUser();
$previousProfilePicture = $user->getProfilePicture() ?: null;
if ($previousProfilePicture) {
$user->setProfilePicture(null);
$this->entityManager->flush();
$this->entityManager->remove($previousProfilePicture);
$this->entityManager->flush();
}

$data->setUser($user);
$user->setProfilePicture($data);
$this->entityManager->flush();

return $data;
}
}

0 comments on commit 256362b

Please sign in to comment.