diff --git a/src/Controller/ApiFileController.php b/src/Controller/ApiFileController.php new file mode 100644 index 0000000000000000000000000000000000000000..84e649c1c6c32a72f9b4e0b7fc8c775a17e43569 --- /dev/null +++ b/src/Controller/ApiFileController.php @@ -0,0 +1,41 @@ +files->get('file'); + if (!$uploadedFile) { + throw new BadRequestHttpException('"file" is required'); + } + + $newFileName = $uploadedFile->getClientOriginalName() . '_' . + substr(hash('sha1', $uploadedFile->getClientOriginalName()), 0, 8) . + '.' . $uploadedFile->getClientOriginalExtension(); + $file = new FileManaged(); + $file->setMimeType($uploadedFile->getMimeType()); + $file->setOriginName($uploadedFile->getClientOriginalName()); + $file->setFileName($newFileName); + $file->setFileSize($uploadedFile->getSize()); + $file->setPath('/uploads/images/' . $newFileName); + + $em->persist($file); + $em->flush(); + + $uploadedFile->move($projectDir . '/public/uploads/images', $newFileName); + + return $file; + } +} \ No newline at end of file diff --git a/src/Entity/FileManaged.php b/src/Entity/FileManaged.php index 9c7f5261c5fb551eaefe8e2530fbb72c0e11b5ba..0add817700046ed27d6a731c3e136f6975235559 100644 --- a/src/Entity/FileManaged.php +++ b/src/Entity/FileManaged.php @@ -3,6 +3,7 @@ namespace App\Entity; use ApiPlatform\Core\Annotation\ApiResource; +use App\Controller\ApiFileController; use App\Repository\FileManagedRepository; use Doctrine\ORM\Mapping as ORM; @@ -10,7 +11,36 @@ use Doctrine\ORM\Mapping as ORM; * @ORM\Entity(repositoryClass=FileManagedRepository::class) * @ORM\Table(name="file") */ -#[ApiResource] +#[ApiResource( + collectionOperations: [ + 'get', + 'post' => [ + 'controller' => ApiFileController::class, + 'method' => 'post', + 'deserialize' => false, + 'openapi_context' => [ + 'requestBody' => [ + 'content' => [ + 'multipart/form-data' => [ + 'schema' => [ + 'type' => 'object', + 'properties' => [ + 'file' => [ + 'type' => 'string', + 'format' => 'binary', + ], + ], + ], + ], + ], + ], + ], + ] +], + itemOperations: [ + 'get' + ] +)] class FileManaged { /** @@ -45,6 +75,8 @@ class FileManaged */ private $path; + private $fileUrl; + public function getId(): ?int { return $this->id; @@ -109,4 +141,22 @@ class FileManaged return $this; } + + /** + * @return mixed + */ + public function getFileUrl() + { + return $this->fileUrl; + } + + /** + * @param mixed $fileUrl + */ + public function setFileUrl($fileUrl): void + { + $this->fileUrl = $fileUrl; + } + + } diff --git a/src/Serializer/Normalizer/FileAwareNormalizer.php b/src/Serializer/Normalizer/FileAwareNormalizer.php new file mode 100644 index 0000000000000000000000000000000000000000..6c9e276d4320c5758f224bb1771b8b32f63a7ac1 --- /dev/null +++ b/src/Serializer/Normalizer/FileAwareNormalizer.php @@ -0,0 +1,56 @@ +requestStack = $requestStack; + } + + private const FILE_NORMALIZER_ALREADY_CALLED = 'file_normalizer_already_called'; + + public function supportsNormalization($data, string $format = null, array $context = []) + { + if (isset($context[self::FILE_NORMALIZER_ALREADY_CALLED])) + { + return false; + } + return $data instanceof FileManaged; + } + + /** + * @param FileManaged $object + * @param string|null $format + * @param array $context + * @return array|\ArrayObject|bool|float|int|string|null + * @throws ExceptionInterface + */ + public function normalize($object, string $format = null, array $context = []) + { + $context[self::FILE_NORMALIZER_ALREADY_CALLED] = true; + + $object->setFileUrl($this->requestStack->getCurrentRequest()->getSchemeAndHttpHost() . $object->getPath()); + return $this->normalizer->normalize($object, $format, $context); + } +} \ No newline at end of file