diff --git a/src/Controller/BookController.php b/src/Controller/BookController.php index 4b571f5..6dcc09c 100644 --- a/src/Controller/BookController.php +++ b/src/Controller/BookController.php @@ -99,9 +99,9 @@ class BookController extends AbstractController $ebook->move( $this->getParameter('book_files'), - 'ebook_' . $book->getId() . '_' . md5( - $ebook->getClientOriginalName() - ) . '.' . $ebook->guessClientExtension() + 'ebook_' . $book->getId() . '_' . + md5($file->getFileName()) . '.' . + $file->getExtension() ); } } @@ -126,16 +126,4 @@ class BookController extends AbstractController return $this->redirectToRoute('app_book_index', [], Response::HTTP_SEE_OTHER); } - - #[Route('/download/{id}', name: 'app_book_download', methods: ['GET'])] - public function downloadBook(Book $book): BinaryFileResponse - { - return $this->file( - $this->getParameter('book_files') . '/' . 'ebook_' . $book->getId() . '.' . pathinfo( - $book->getOriginalFilename(), - PATHINFO_EXTENSION - ), - $book->getOriginalFilename() - ); - } } diff --git a/src/Controller/FileController.php b/src/Controller/FileController.php new file mode 100644 index 0000000..dd835fd --- /dev/null +++ b/src/Controller/FileController.php @@ -0,0 +1,53 @@ +render('file/index.html.twig', [ + 'controller_name' => 'FileController', + ]); + } + + #[Route('/file/delete/{id}', name: 'app_file_delete')] + public function delete( + Request $request, + FileRepository $fileRepository, + File $file, + Filesystem $filesystem + ): Response { + $filePath = $this->getParameter('book_files') . '/' . 'ebook_' . $file->getBook()->getId() . '_' . + md5($file->getFileName()) . '.' . + $file->getExtension(); + $filesystem->remove($filePath); + + if (!$filesystem->exists($filePath)) { + $fileRepository->remove($file, 'true'); + } + return $this->redirect($request->headers->get('referer')); + } + + #[Route('/file/{id}', name: 'app_file_download', methods: ['GET'])] + public function get(File $file) + { + return $this->file( + $filePath = $this->getParameter('book_files') . '/' . 'ebook_' . $file->getBook()->getId() . '_' . + md5($file->getFileName()) . '.' . + $file->getExtension(), + $file->getFileName() + ); + } +} diff --git a/src/Twig/AppExtension.php b/src/Twig/AppExtension.php index a53d704..fabd62c 100644 --- a/src/Twig/AppExtension.php +++ b/src/Twig/AppExtension.php @@ -4,6 +4,7 @@ namespace App\Twig; use Twig\Extension\AbstractExtension; +use Twig\TwigFilter; use Twig\TwigFunction; class AppExtension extends AbstractExtension @@ -15,8 +16,22 @@ class AppExtension extends AbstractExtension ]; } + public function getFilters() + { + return [ + new TwigFilter('bytes_format', [$this, 'bytes_format']), + ]; + } + public function file_exists(string $file): bool { return file_exists(ltrim($file, '/')); } + + public function bytes_format(int $bytes): string + { + $base = log($bytes) / log(1024); + $suffix = array("B", "KB", "MB", "GB", "TB")[floor($base)]; + return round(pow(1024, $base - floor($base)), 2) . $suffix; + } } \ No newline at end of file diff --git a/templates/book/show.html.twig b/templates/book/show.html.twig index 450a76d..896ee5c 100644 --- a/templates/book/show.html.twig +++ b/templates/book/show.html.twig @@ -50,10 +50,10 @@ {{ file.id }} {{ file.fileName }} - {{ file.fileSize }} + {{ file.fileSize | bytes_format }} {{ file.extension }} - download - remove + download + remove {% else %} diff --git a/templates/file/index.html.twig b/templates/file/index.html.twig new file mode 100644 index 0000000..fa7d483 --- /dev/null +++ b/templates/file/index.html.twig @@ -0,0 +1,20 @@ +{% extends 'base.html.twig' %} + +{% block title %}Hello FileController!{% endblock %} + +{% block body %} + + +
+

Hello {{ controller_name }}! ✅

+ + This friendly message is coming from: + +
+{% endblock %}