Added twig extension to format file size, links to download and delete files attached to the book.
This commit is contained in:
@@ -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()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
53
src/Controller/FileController.php
Normal file
53
src/Controller/FileController.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Book;
|
||||
use App\Entity\File;
|
||||
use App\Repository\FileRepository;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
class FileController extends AbstractController
|
||||
{
|
||||
#[Route('/file', name: 'app_file')]
|
||||
public function index(): Response
|
||||
{
|
||||
return $this->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()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -50,10 +50,10 @@
|
||||
<tr>
|
||||
<td>{{ file.id }}</td>
|
||||
<td>{{ file.fileName }}</td>
|
||||
<td>{{ file.fileSize }}</td>
|
||||
<td>{{ file.fileSize | bytes_format }}</td>
|
||||
<td>{{ file.extension }}</td>
|
||||
<td>download</td>
|
||||
<td>remove</td>
|
||||
<td><a href="{{ path('app_file_download', {id: file.id }) }}" class="link-secondary">download</a></td>
|
||||
<td><a href="{{ path('app_file_delete', {id: file.id}) }}" class="link-danger">remove</a></td>
|
||||
</tr>
|
||||
{% else %}
|
||||
<tr>
|
||||
|
||||
20
templates/file/index.html.twig
Normal file
20
templates/file/index.html.twig
Normal file
@@ -0,0 +1,20 @@
|
||||
{% extends 'base.html.twig' %}
|
||||
|
||||
{% block title %}Hello FileController!{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<style>
|
||||
.example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px/1.5 sans-serif; }
|
||||
.example-wrapper code { background: #F5F5F5; padding: 2px 6px; }
|
||||
</style>
|
||||
|
||||
<div class="example-wrapper">
|
||||
<h1>Hello {{ controller_name }}! ✅</h1>
|
||||
|
||||
This friendly message is coming from:
|
||||
<ul>
|
||||
<li>Your controller at <code><a href="{{ 'C:/laragon/www/biblio/src/Controller/FileController.php'|file_link(0) }}">src/Controller/FileController.php</a></code></li>
|
||||
<li>Your template at <code><a href="{{ 'C:/laragon/www/biblio/templates/file/index.html.twig'|file_link(0) }}">templates/file/index.html.twig</a></code></li>
|
||||
</ul>
|
||||
</div>
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user