- FileSystemManager.php refactoring of all write/read actions on filesystem

Deleted:
- old ToolbarManager.php
This commit is contained in:
Jérémy Guillot
2024-07-24 17:21:44 +02:00
parent 7068bd1a34
commit 7eba0981c8
10 changed files with 142 additions and 197 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Controller;
use App\Manager\FileSystemManager;
use App\Repository\ChapterRepository;
use App\Repository\MangaRepository;
use App\Service\CbrToCbzConverter;
@@ -19,10 +20,8 @@ use Symfony\Component\String\Slugger\SluggerInterface;
class ImportController extends AbstractController
{
private const string UPLOADS_DIRECTORY = 'public/uploads';
public function __construct(
private readonly string $projectDir,
private readonly FileSystemManager $fileSystemManager,
private readonly CbzService $cbzService,
private readonly MangaImportService $mangaImportService,
private readonly NotificationService $notificationService,
@@ -43,13 +42,16 @@ class ImportController extends AbstractController
foreach ($files as $file) {
if ($file && in_array($file->getClientOriginalExtension(), ['cbz', 'cbr'])) {
$originalFileName = $file->getClientOriginalName();
$filename = uniqid() . '.' . $file->getClientOriginalExtension();
try {
$file->move($this->projectDir . '/' . self::UPLOADS_DIRECTORY, $filename);
$tmpPath = $this->fileSystemManager->moveUploadedFile(
$file->getPathname(),
$this->fileSystemManager->getUploadsDirectory(),
$file->getClientOriginalName()
);
$importFiles[] = [
'id' => uniqid(),
'path' => $this->projectDir . '/' . self::UPLOADS_DIRECTORY . '/' . $filename,
'path' => $tmpPath,
'original_name' => $originalFileName,
];
} catch (FileException $e) {
@@ -85,7 +87,7 @@ class ImportController extends AbstractController
* @throws Exception
*/
#[Route('/import/match', name: 'import_match')]
public function match(Request $request, SessionInterface $session): Response
public function match(SessionInterface $session): Response
{
$files = $session->get('import_files', []);
if (empty($files)) {
@@ -137,7 +139,8 @@ class ImportController extends AbstractController
]);
}
private function formatBytes($bytes, $precision = 2) {
private function formatBytes($bytes, $precision = 2)
{
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
@@ -174,7 +177,7 @@ class ImportController extends AbstractController
throw new \Exception('Manga non trouvé.');
}
if(!is_null($chapter)){
if (!is_null($chapter)) {
$chapter = $manga->getChapterByNumber($chapter);
if (!$chapter) {
throw new \Exception('Chapitre non trouvé.');
@@ -182,17 +185,15 @@ class ImportController extends AbstractController
}
$importedFiles[] = $file['original_name'];
$this->mangaImportService->importFile($manga, $volume, $chapter,$file['path']);
$this->mangaImportService->importFile($manga, $volume, $chapter, $file['path']);
} catch (\Exception $e) {
$errors[] = "Erreur lors de l'import de {$file['original_name']} : " . $e->getMessage();
}
}
// Nettoyer les fichiers temporaires non importés
foreach ($files as $fileId => $file) {
if (!in_array($fileId, (array)$selectedFiles) && file_exists($file['path'])) {
unlink($file['path']);
}
foreach ($files as $file) {
$this->fileSystemManager->deleteFile($file['path']);
}
// Nettoyer la session

View File

@@ -5,6 +5,7 @@ namespace App\Controller;
use App\Entity\Chapter;
use App\Entity\Manga;
use App\Form\MangaEditType;
use App\Manager\FileSystemManager;
use App\Manager\Toolbar\Factory\ToolbarFactory;
use App\Message\DownloadChapter;
use App\Message\RefreshMetadata;
@@ -38,7 +39,7 @@ class MangaController extends AbstractController
private ImageManager $imageManager;
public function __construct(
private readonly string $projectDir,
private readonly FileSystemManager $fileSystemManager,
private readonly MangaRepository $mangaRepository,
private readonly ChapterRepository $chapterRepository,
private readonly MessageBusInterface $bus,
@@ -47,7 +48,6 @@ class MangaController extends AbstractController
private readonly MangadexProvider $mangadexProvider,
private readonly EntityManagerInterface $entityManager,
private readonly NotificationService $notificationService,
private readonly SluggerInterface $slugger,
private readonly ContentSourceRepository $contentSourceRepository
)
{
@@ -213,39 +213,6 @@ class MangaController extends AbstractController
return new JsonResponse(['success' => 'Chapter hidden.'], 200);
}
// #[Route('/manga/read/{mangaSlug}/{chapterNumber}/{pageNumber}', name: 'app_manga_read')]
// public function readChapterPage(string $mangaSlug, float $chapterNumber, int $pageNumber = 1): Response
// {
// $manga = $this->mangaRepository->findOneBy(['slug' => $mangaSlug]);
// if (!$manga) {
// throw $this->createNotFoundException("Le manga demandé n'existe pas.");
// }
//
// $chapter = $manga->getChapterByNumber($chapterNumber);
// if (!$chapter) {
// throw $this->createNotFoundException("Le chapitre demandé n'existe pas.");
// }
//
// if (is_null($chapter->getCbzPath())) {
// throw $this->createNotFoundException("Le chapitre demandé n'a pas été scrapé.");
// }
//
// $pageContent = $this->cbzService->getPageContent($chapter->getCbzPath(), $pageNumber);
// if (!$pageContent) {
// throw $this->createNotFoundException("La page demandée n'existe pas.");
// }
//
// $totalPages = $this->cbzService->getPageCount($chapter->getCbzPath());
//
// return $this->render('manga/manga_reader.html.twig', [
// 'manga' => $manga,
// 'chapter' => $chapter,
// 'currentPage' => $pageNumber,
// 'totalPages' => $totalPages,
// 'pageContent' => base64_encode($pageContent),
// ]);
// }
#[Route('/manga/search/{query}', name: 'app_manga_search')]
public function search(string $query = ''): Response
{
@@ -273,8 +240,7 @@ class MangaController extends AbstractController
->setPublicationYear($request->request->get('publicationYear'))
->setRating($request->request->get('rating'))
->setExternalId($request->request->get('externalId'))
->setMonitored(false)
;
->setMonitored(false);
// Traitement de l'image
$imageUrl = $request->request->get('imageUrl');
@@ -322,25 +288,24 @@ class MangaController extends AbstractController
// Générer un nom de fichier unique
$originalFilename = pathinfo($imageUrl, PATHINFO_FILENAME);
$safeFilename = $this->slugger->slug($originalFilename);
$newFilename = $safeFilename . '-' . uniqid() . '.' . 'jpg';
$newFilename = $this->fileSystemManager->generateUniqueImageFilename($imageUrl);
try {
// Créer et sauvegarder la miniature
$thumbnail = $this->imageManager->read($tempImagePath);
$thumbnail->cover(300, 440);
$thumbnail->save($this->projectDir . '/public/images/thumbnails/' . $newFilename, quality: 85);
$thumbnail->save($this->fileSystemManager->getImagePath('thumbnails') . '/' . $newFilename, quality: 85);
// Sauvegarder l'image en taille réelle
$fullImage = $this->imageManager->read($tempImagePath);
$fullImage->save($this->projectDir . '/public/images/full/' . $newFilename, quality: 90);
$fullImage->save($this->fileSystemManager->getImagePath('full') . '/' . $newFilename, quality: 90);
// Fermer et supprimer le fichier temporaire
fclose($tempImage);
return [
'full' => '/images/full/' . $newFilename,
'thumbnail' => '/images/thumbnails/' . $newFilename
'full' => $this->fileSystemManager->getImagePath('full') . '/' . $newFilename,
'thumbnail' => $this->fileSystemManager->getImagePath('thumbnails') . '/' . $newFilename
];
} catch (FileException $e) {