- 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

@@ -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

View File

@@ -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
]);

View File

@@ -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);

View File

@@ -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);