Loading config/services.yaml +3 −0 Original line number Diff line number Diff line Loading @@ -22,3 +22,6 @@ services: # add more service definitions when explicit configuration is needed # please note that last definitions always *replace* previous ones App\Service\FileUploader: arguments: $targetDirectory: '%kernel.project_dir%/public/files/' No newline at end of file src/Controller/HomeController.php +6 −3 Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ namespace App\Controller; use App\Action\Functions; use App\Service\FileUploader; use App\Requests\{ SortPriceRequest, SearchRequest, Loading @@ -12,6 +13,7 @@ use App\Requests\{ }; use DateTimeImmutable; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Attribute\Route; Loading Loading @@ -104,10 +106,11 @@ class HomeController extends AbstractController } #[Route('/readLogFile', name: 'readLogFile')] public function readLogFile(Request $request): Response public function readLogFile(Request $request, FileUploader $fileUploader): Response { $filePath = $this->getParameter('kernel.project_dir') . "/public/files/"; $text = $this->functions->readLogFile($filePath . $fileName); $file = $request->files->get('file'); $fileName = $fileUploader->upload($file); $text = $this->functions->readLogFile($fileName); $response = new JsonResponse($text); $response->setEncodingOptions(JSON_UNESCAPED_UNICODE); return $response; Loading src/Service/FileUploader.php 0 → 100644 +33 −0 Original line number Diff line number Diff line <?php namespace App\Service; use Symfony\Component\HttpFoundation\File\Exception\FileException; use Symfony\Component\HttpFoundation\File\UploadedFile; class FileUploader { private $targetDirectory; public function __construct($targetDirectory) { $this->targetDirectory = $targetDirectory; } public function upload(UploadedFile $file) { $fileName = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME) .'.'. $file->guessExtension(); try { $file->move($this->getTargetDirectory(), $fileName); } catch (FileException $e) { throw new FileException($e->getMessage()); } return $this->getTargetDirectory() . $fileName; } public function getTargetDirectory() { return $this->targetDirectory; } } No newline at end of file Loading
config/services.yaml +3 −0 Original line number Diff line number Diff line Loading @@ -22,3 +22,6 @@ services: # add more service definitions when explicit configuration is needed # please note that last definitions always *replace* previous ones App\Service\FileUploader: arguments: $targetDirectory: '%kernel.project_dir%/public/files/' No newline at end of file
src/Controller/HomeController.php +6 −3 Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ namespace App\Controller; use App\Action\Functions; use App\Service\FileUploader; use App\Requests\{ SortPriceRequest, SearchRequest, Loading @@ -12,6 +13,7 @@ use App\Requests\{ }; use DateTimeImmutable; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Attribute\Route; Loading Loading @@ -104,10 +106,11 @@ class HomeController extends AbstractController } #[Route('/readLogFile', name: 'readLogFile')] public function readLogFile(Request $request): Response public function readLogFile(Request $request, FileUploader $fileUploader): Response { $filePath = $this->getParameter('kernel.project_dir') . "/public/files/"; $text = $this->functions->readLogFile($filePath . $fileName); $file = $request->files->get('file'); $fileName = $fileUploader->upload($file); $text = $this->functions->readLogFile($fileName); $response = new JsonResponse($text); $response->setEncodingOptions(JSON_UNESCAPED_UNICODE); return $response; Loading
src/Service/FileUploader.php 0 → 100644 +33 −0 Original line number Diff line number Diff line <?php namespace App\Service; use Symfony\Component\HttpFoundation\File\Exception\FileException; use Symfony\Component\HttpFoundation\File\UploadedFile; class FileUploader { private $targetDirectory; public function __construct($targetDirectory) { $this->targetDirectory = $targetDirectory; } public function upload(UploadedFile $file) { $fileName = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME) .'.'. $file->guessExtension(); try { $file->move($this->getTargetDirectory(), $fileName); } catch (FileException $e) { throw new FileException($e->getMessage()); } return $this->getTargetDirectory() . $fileName; } public function getTargetDirectory() { return $this->targetDirectory; } } No newline at end of file