Added:
- FileSystemManager.php refactoring of all write/read actions on filesystem Deleted: - old ToolbarManager.php
This commit is contained in:
@@ -4,23 +4,19 @@ namespace App\Service;
|
||||
|
||||
use App\Entity\Chapter;
|
||||
use App\Entity\Manga;
|
||||
use App\Manager\FileSystemManager;
|
||||
use App\Repository\ChapterRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Component\String\Slugger\SluggerInterface;
|
||||
|
||||
class MangaImportService
|
||||
readonly class MangaImportService
|
||||
{
|
||||
private const string CBZ_DIRECTORY = 'public/cbz';
|
||||
|
||||
public function __construct(
|
||||
private readonly string $projectDir,
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly ChapterRepository $chapterRepository,
|
||||
private readonly Filesystem $filesystem,
|
||||
private readonly SluggerInterface $slugger
|
||||
private FileSystemManager $fileSystemManager,
|
||||
private EntityManagerInterface $entityManager,
|
||||
private ChapterRepository $chapterRepository,
|
||||
private SluggerInterface $slugger
|
||||
)
|
||||
{
|
||||
}
|
||||
@@ -45,18 +41,17 @@ class MangaImportService
|
||||
private function importVolume(Manga $manga, int $volume, string $tempFilePath): void
|
||||
{
|
||||
$permanentFileName = $this->createPermanentFileName($manga, $volume);
|
||||
$mangaDirectory = $this->createMangaDirectory($manga);
|
||||
$permanentFilePath = $this->projectDir . '/' . $mangaDirectory .'/volume_' . sprintf('%02d', $volume) . '/' . $permanentFileName;
|
||||
$mangaDirectory = $this->fileSystemManager->createMangaDirectory($manga->getSlug(), $manga->getPublicationYear());
|
||||
$volumeDirectory = $this->fileSystemManager->createVolumeDirectory($mangaDirectory, $volume);
|
||||
$permanentFilePath = $volumeDirectory . '/' . $permanentFileName;
|
||||
|
||||
if ($this->filesystem->exists($permanentFilePath)) {
|
||||
if ($this->fileSystemManager->fileExists($permanentFilePath)) {
|
||||
throw new \RuntimeException("Un fichier pour ce volume existe déjà.");
|
||||
}
|
||||
|
||||
$this->filesystem->mkdir(dirname($permanentFilePath), 0755);
|
||||
$this->filesystem->rename($tempFilePath, $permanentFilePath, true);
|
||||
$this->fileSystemManager->moveFile($tempFilePath, $permanentFilePath);
|
||||
|
||||
$this->updateVolumeChapters($manga, $volume, $permanentFilePath);
|
||||
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
@@ -67,15 +62,15 @@ class MangaImportService
|
||||
{
|
||||
$volume = $chapter->getVolume();
|
||||
$permanentFileName = $this->createPermanentFileName($manga, $volume, $chapter->getNumber());
|
||||
$mangaDirectory = $this->createMangaDirectory($manga);
|
||||
$permanentFilePath = $this->projectDir . '/' . $mangaDirectory .'/volume_' . sprintf('%02d', $volume) . '/' . $permanentFileName;
|
||||
$mangaDirectory = $this->fileSystemManager->createMangaDirectory($manga->getSlug(), $manga->getPublicationYear());
|
||||
$volumeDirectory = $this->fileSystemManager->createVolumeDirectory($mangaDirectory, $chapter->getVolume());
|
||||
$permanentFilePath = $volumeDirectory . '/' . $permanentFileName;
|
||||
|
||||
if ($this->filesystem->exists($permanentFilePath)) {
|
||||
if ($this->fileSystemManager->fileExists($permanentFilePath)) {
|
||||
throw new \RuntimeException("Un fichier pour ce chapitre existe déjà.");
|
||||
}
|
||||
|
||||
$this->filesystem->mkdir(dirname($permanentFilePath), 0755);
|
||||
$this->filesystem->rename($tempFilePath, $permanentFilePath, true);
|
||||
$this->fileSystemManager->moveFile($tempFilePath, $permanentFilePath);
|
||||
|
||||
$chapter->setCbzPath($permanentFilePath);
|
||||
|
||||
@@ -91,15 +86,6 @@ class MangaImportService
|
||||
return $baseFileName . '.cbz';
|
||||
}
|
||||
|
||||
private function createMangaDirectory(Manga $manga): string
|
||||
{
|
||||
$mangaYear = $manga->getPublicationYear() ?? 'unknown';
|
||||
$directoryPath = self::CBZ_DIRECTORY . '/' . ucfirst($manga->getSlug()) . ' (' . $mangaYear . ')';
|
||||
|
||||
$this->filesystem->mkdir($directoryPath, 0755);
|
||||
return $directoryPath;
|
||||
}
|
||||
|
||||
private function updateVolumeChapters(Manga $manga, int $volume, string $cbzPath): void
|
||||
{
|
||||
$chapters = $this->chapterRepository->findBy([
|
||||
|
||||
@@ -6,6 +6,7 @@ use App\Entity\Chapter;
|
||||
use App\Entity\ContentSource;
|
||||
use App\Entity\Manga;
|
||||
use App\Event\PageScrappingProgressEvent;
|
||||
use App\Manager\FileSystemManager;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Exception;
|
||||
use GuzzleHttp\Client;
|
||||
@@ -15,11 +16,10 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
abstract class AbstractScraper implements ScraperInterface
|
||||
{
|
||||
const string PUBLIC_CBZ = '/public/cbz';
|
||||
protected Client $httpClient;
|
||||
|
||||
public function __construct(
|
||||
protected string $projectDir,
|
||||
protected FileSystemManager $fileSystemManager,
|
||||
protected EventDispatcherInterface $eventDispatcher,
|
||||
protected EntityManagerInterface $entityManager
|
||||
)
|
||||
@@ -53,7 +53,8 @@ abstract class AbstractScraper implements ScraperInterface
|
||||
|
||||
protected function generateCbzPath(Manga $manga, Chapter $chapter): string
|
||||
{
|
||||
$volumeDir = $this->createDirectories($manga, $chapter->getVolume());
|
||||
$mangaDir = $this->fileSystemManager->createMangaDirectory($manga->getSlug(), $manga->getPublicationYear());
|
||||
$volumeDir = $this->fileSystemManager->createVolumeDirectory($mangaDir, $chapter->getVolume());
|
||||
$fileName = sprintf('%s_vol%d_ch%s.cbz',
|
||||
$manga->getSlug(),
|
||||
$chapter->getVolume(),
|
||||
@@ -62,7 +63,7 @@ abstract class AbstractScraper implements ScraperInterface
|
||||
return $volumeDir . '/' . $fileName;
|
||||
}
|
||||
|
||||
protected function createCbzFile(string $tempDir, array $pageData, string $cbzFilePath): void
|
||||
protected function createCbzFile(array $pageData, string $cbzFilePath): void
|
||||
{
|
||||
$zip = new \ZipArchive();
|
||||
|
||||
@@ -76,26 +77,7 @@ abstract class AbstractScraper implements ScraperInterface
|
||||
|
||||
protected function cleanupTempFiles(string $directory): void
|
||||
{
|
||||
$files = glob($directory . '/*');
|
||||
foreach ($files as $file) {
|
||||
if (is_file($file)) {
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
rmdir($directory);
|
||||
}
|
||||
|
||||
protected function createDirectories(Manga $manga, int $volume): string
|
||||
{
|
||||
$mangaYear = $manga->getPublicationYear() ?? 'unknown';
|
||||
$mangaDir = sprintf('%s/%s (%s)', $this->projectDir . self::PUBLIC_CBZ, ucfirst($manga->getSlug()), $mangaYear);
|
||||
$volumeDir = sprintf('%s/volume_%d', $mangaDir, sprintf('%02d', $volume));
|
||||
|
||||
if (!is_dir($volumeDir)) {
|
||||
mkdir($volumeDir, 0755, true);
|
||||
}
|
||||
|
||||
return $volumeDir;
|
||||
$this->fileSystemManager->deleteDirectory($directory);
|
||||
}
|
||||
|
||||
protected function cleanImageUrl(string $url): string
|
||||
|
||||
@@ -13,17 +13,6 @@ use Symfony\Component\DomCrawler\Crawler;
|
||||
|
||||
class HtmlScraper extends AbstractScraper
|
||||
{
|
||||
private Client $client;
|
||||
|
||||
public function __construct(
|
||||
string $projectDir,
|
||||
EventDispatcherInterface $eventDispatcher,
|
||||
EntityManagerInterface $entityManager
|
||||
) {
|
||||
parent::__construct($projectDir, $eventDispatcher, $entityManager);
|
||||
$this->client = new Client();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
* @throws GuzzleException
|
||||
@@ -64,7 +53,7 @@ class HtmlScraper extends AbstractScraper
|
||||
}
|
||||
|
||||
$cbzFilePath = $this->generateCbzPath($manga, $chapter);
|
||||
$this->createCbzFile($tempDir, $pageData, $cbzFilePath);
|
||||
$this->createCbzFile($pageData, $cbzFilePath);
|
||||
|
||||
$chapter->setCbzPath($cbzFilePath);
|
||||
$this->entityManager->persist($chapter);
|
||||
@@ -144,7 +133,7 @@ class HtmlScraper extends AbstractScraper
|
||||
private function fetchHtml(string $url): string
|
||||
{
|
||||
try {
|
||||
$response = $this->client->get($url, [
|
||||
$response = $this->httpClient->get($url, [
|
||||
'http_errors' => true,
|
||||
'allow_redirects' => false
|
||||
]);
|
||||
|
||||
@@ -47,7 +47,7 @@ class JavascriptScraper extends AbstractScraper
|
||||
}
|
||||
|
||||
$cbzFilePath = $this->generateCbzPath($manga, $chapter);
|
||||
$this->createCbzFile($tempDir, $pageData, $cbzFilePath);
|
||||
$this->createCbzFile($pageData, $cbzFilePath);
|
||||
|
||||
$chapter->setCbzPath($cbzFilePath);
|
||||
$this->entityManager->persist($chapter);
|
||||
|
||||
@@ -10,17 +10,6 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
class MangadexScraper extends AbstractScraper
|
||||
{
|
||||
private Client $client;
|
||||
|
||||
public function __construct(
|
||||
string $projectDir,
|
||||
EventDispatcherInterface $eventDispatcher,
|
||||
EntityManagerInterface $entityManager
|
||||
) {
|
||||
parent::__construct($projectDir, $eventDispatcher, $entityManager);
|
||||
$this->client = new Client();
|
||||
}
|
||||
|
||||
public function scrapeChapter(Chapter $chapter, ContentSource $contentSource): array|bool
|
||||
{
|
||||
$chapterUrl = $contentSource->getBaseUrl() . sprintf($contentSource->getChapterUrlFormat(), $chapter->getExternalId());
|
||||
@@ -28,7 +17,7 @@ class MangadexScraper extends AbstractScraper
|
||||
$pageData = [];
|
||||
|
||||
try {
|
||||
$response = $this->client->get($chapterUrl);
|
||||
$response = $this->httpClient->get($chapterUrl);
|
||||
$results = json_decode($response->getBody()->getContents(), true);
|
||||
|
||||
if ($results['result'] !== 'ok' || count($results['chapter']['dataSaver']) === 0) {
|
||||
@@ -54,7 +43,7 @@ class MangadexScraper extends AbstractScraper
|
||||
}
|
||||
|
||||
$cbzFilePath = $this->generateCbzPath($manga, $chapter);
|
||||
$this->createCbzFile($tempDir, $pageData, $cbzFilePath);
|
||||
$this->createCbzFile($pageData, $cbzFilePath);
|
||||
|
||||
$chapter->setCbzPath($cbzFilePath);
|
||||
$this->entityManager->persist($chapter);
|
||||
|
||||
Reference in New Issue
Block a user