Skip to content

Commit

Permalink
Fix if not rar arch
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioMendolia committed Sep 20, 2023
1 parent 7b12f9d commit 1927ce5
Showing 1 changed file with 92 additions and 34 deletions.
126 changes: 92 additions & 34 deletions src/Service/BookFileSystemManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Service;

use App\Entity\Book;
use Archive7z\Archive7z;
use Kiwilan\Ebook\Ebook;
use Psr\Log\LoggerInterface;
use Symfony\Component\Filesystem\Filesystem;
Expand Down Expand Up @@ -355,42 +356,10 @@ public function extractCover(Book $book): Book
$return = shell_exec('unrar lb "'.$bookFile->getRealPath().'"');

if (!is_string($return)) {
$book = $this->extractFromGeneralArchive($bookFile, $book);
break;
}
$entries = explode(PHP_EOL, $return);

sort($entries);

$entries = array_values(array_filter($entries, static function ($entry) {
return str_ends_with($entry, '.jpg') || str_ends_with($entry, '.jpeg') || str_ends_with($entry, '.png');
}));
if (count($entries) === 0) {
break;
}
$cover = current($entries);

$expl = explode('.', $cover);
$coverFile = explode('/', $cover);
$coverFile = end($coverFile);
$ext = end($expl);
$book->setImageExtension($ext);

shell_exec('unrar e -ep "'.$bookFile->getRealPath().'" "'.$cover.'" -op"/tmp/" -y');

$filesystem->mkdir($this->getCalculatedImagePath($book, true));
try {
$checksum = $this->getFileChecksum(new \SplFileInfo('/tmp/'.$coverFile));
} catch (\Exception $e) {
$this->logger->error('Could not calculate checksum', ['book' => $bookFile->getRealPath(), 'exception' => $e->getMessage()]);
$checksum = md5(''.time());
}
$filesystem->rename(
'/tmp/'.$coverFile,
$this->getCalculatedImagePath($book, true).$this->getCalculatedImageName($book, $checksum),
true);

$book->setImagePath($this->getCalculatedImagePath($book, false));
$book->setImageFilename($this->getCalculatedImageName($book, $checksum));
$book = $this->extractFromRarArchive($bookFile, $book);

break;
case 'pdf':
Expand Down Expand Up @@ -421,4 +390,93 @@ public function extractCover(Book $book): Book

return $book;
}

private function extractFromRarArchive(\SplFileInfo $bookFile, Book $book): Book
{
$return = shell_exec('unrar lb "'.$bookFile->getRealPath().'"');

if (!is_string($return)) {
$this->logger->error('not a string', ['book' => $bookFile->getRealPath(), 'return' => $return]);

return $book;
}
$entries = explode(PHP_EOL, $return);

sort($entries);

$entries = array_values(array_filter($entries, static function ($entry) {
return str_ends_with($entry, '.jpg') || str_ends_with($entry, '.jpeg') || str_ends_with($entry, '.png');
}));
if (count($entries) === 0) {
$this->logger->error('no errors');

return $book;
}
$cover = current($entries);

$expl = explode('.', $cover);
$coverFile = explode('/', $cover);
$coverFile = end($coverFile);
$ext = end($expl);
$book->setImageExtension($ext);

shell_exec('unrar e -ep "'.$bookFile->getRealPath().'" "'.$cover.'" -op"/tmp/" -y');

$filesystem = new Filesystem();
$filesystem->mkdir($this->getCalculatedImagePath($book, true));
try {
$checksum = $this->getFileChecksum(new \SplFileInfo('/tmp/'.$coverFile));
} catch (\Exception $e) {
$this->logger->error('Could not calculate checksum', ['book' => $bookFile->getRealPath(), 'exception' => $e->getMessage()]);
$checksum = md5(''.time());
}
$filesystem->rename(
'/tmp/'.$coverFile,
$this->getCalculatedImagePath($book, true).$this->getCalculatedImageName($book, $checksum),
true);

$book->setImagePath($this->getCalculatedImagePath($book, false));
$book->setImageFilename($this->getCalculatedImageName($book, $checksum));

return $book;
}

private function extractFromGeneralArchive(\SplFileInfo $bookFile, Book $book): Book
{
$archive = new Archive7z($bookFile->getRealPath());

if (!$archive->isValid()) {
return $book;
}

$entries = [];
foreach ($archive->getEntries() as $entry) {
if (str_contains($entry->getPath(), '.jpg') || str_contains($entry->getPath(), '.jpeg')) {
$entries[] = $entry->getPath();
}
}
ksort($entries);

sort($entries);

if (count($entries) === 0) {
return $book;
}

$archive->setOutputDirectory('/tmp')->extractEntry($entries[0]); // extract the archive

$filesystem = new Filesystem();
$filesystem->mkdir($this->getCalculatedImagePath($book, true));
$checksum = $this->getFileChecksum(new \SplFileInfo('/tmp/'.$entries[0]));
$filesystem->rename(
'/tmp/'.$entries[0],
$this->getCalculatedImagePath($book, true).$this->getCalculatedImageName($book, $checksum),
true);

$book->setImagePath($this->getCalculatedImagePath($book, false));
$book->setImageFilename($this->getCalculatedImageName($book, $checksum));
$book->setImageExtension('jpg');

return $book;
}
}

0 comments on commit 1927ce5

Please sign in to comment.