forked from teebbstudios/teebblog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ae1b6b
commit 3f1fec8
Showing
3 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
|
||
namespace App\Controller; | ||
|
||
|
||
use App\Entity\FileManaged; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\File\UploadedFile; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | ||
|
||
class ApiFileController extends AbstractController | ||
{ | ||
public function __invoke(Request $request, EntityManagerInterface $em, string $projectDir) | ||
{ | ||
/**@var UploadedFile $uploadedFile * */ | ||
$uploadedFile = $request->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
|
||
namespace App\Serializer\Normalizer; | ||
|
||
|
||
use App\Entity\FileManaged; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\Serializer\Exception\CircularReferenceException; | ||
use Symfony\Component\Serializer\Exception\ExceptionInterface; | ||
use Symfony\Component\Serializer\Exception\InvalidArgumentException; | ||
use Symfony\Component\Serializer\Exception\LogicException; | ||
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; | ||
|
||
class FileAwareNormalizer implements NormalizerAwareInterface, ContextAwareNormalizerInterface | ||
{ | ||
use NormalizerAwareTrait; | ||
|
||
/** | ||
* @var RequestStack | ||
*/ | ||
private RequestStack $requestStack; | ||
|
||
public function __construct(RequestStack $requestStack) | ||
{ | ||
$this->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); | ||
} | ||
} |