Skip to content

Commit

Permalink
Fix Kepubification
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioMendolia committed Dec 2, 2024
1 parent e64f946 commit 5d5b2d4
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/Kobo/Kepubify/KepubifyMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class KepubifyMessageHandler

public function __construct(
#[Autowire(param: 'kernel.cache_dir')]
private readonly string $cacheDir,
private readonly LoggerInterface $koboKepubify,
private readonly KepubifyEnabler $kepubifyEnabler,
private readonly string $cacheDir,
private readonly LoggerInterface $koboKepubifyLogger,
private readonly KepubifyEnabler $kepubifyEnabler,
private readonly CacheItemPoolInterface $kepubifyCachePool,
) {
}
Expand All @@ -29,13 +29,14 @@ public function __invoke(KepubifyMessage $message): void
{
// Disable kepubify if the path is not set
if (false === $this->kepubifyEnabler->isEnabled()) {
$this->koboKepubifyLogger->debug('Kepubify is disabled');
return;
}

// Create a temporary file
$temporaryFile = $this->getTemporaryFilename();
if ($temporaryFile === false) {
$this->koboKepubify->error('Error while creating temporary file');
$this->koboKepubifyLogger->error('Error while creating temporary file');

return;
}
Expand All @@ -44,7 +45,7 @@ public function __invoke(KepubifyMessage $message): void
try {
$item = $this->kepubifyCachePool->getItem('kepubify_object_'.md5($message->source));
} catch (InvalidArgumentException $e) {
$this->koboKepubify->error('Error while caching kepubify: {error}', [
$this->koboKepubifyLogger->error('Error while caching kepubify: {error}', [
'error' => $e->getMessage(),
'exception' => $e,
]);
Expand Down Expand Up @@ -79,7 +80,7 @@ public function __invoke(KepubifyMessage $message): void
try {
$this->kepubifyCachePool->deleteItem($item->getKey());
} catch (InvalidArgumentException $e) {
$this->koboKepubify->error('Error while deleting cached kepubify data: {error}', [
$this->koboKepubifyLogger->error('Error while deleting cached kepubify data: {error}', [
'error' => $e->getMessage(),
'exception' => $e,
]);
Expand All @@ -90,7 +91,7 @@ public function __invoke(KepubifyMessage $message): void

$result = file_put_contents($temporaryFile, $data->getContent());
if ($result === false) {
$this->koboKepubify->error('Error while restoring cached kepubify data');
$this->koboKepubifyLogger->error('Error while restoring cached kepubify data');
$temporaryFile = null;
}
$message->destination = $temporaryFile;
Expand All @@ -116,20 +117,28 @@ private function getTemporaryFilename(): string|false

private function convert(KepubifyMessage $message, string $temporaryFile): ?string
{
$filename = basename($message->source);

$temporaryFolder = dirname($temporaryFile);

$convertedFilename = str_replace('.epub', '.kepub.epub', $filename);

// Run the conversion
$process = new Process([$this->kepubifyEnabler->getKepubifyBinary(), '--output', $temporaryFile, $message->source]);
$this->koboKepubify->debug('Run kepubify command: {command}', ['command' => $process->getCommandLine()]);
$process = new Process([$this->kepubifyEnabler->getKepubifyBinary(),'--inplace', '--output', $temporaryFolder, $message->source]);
$this->koboKepubifyLogger->debug('Run kepubify command: {command}', ['command' => $process->getCommandLine()]);
$process->run();

if (!$process->isSuccessful()) {
$this->koboKepubify->error('Error while running kepubify: {output}: {error}', [
$this->koboKepubifyLogger->error('Error while running kepubify: {output}: {error}', [
'output' => $process->getOutput(),
'error' => $process->getErrorOutput(),
]);
@unlink($temporaryFile);
$temporaryFile = null;
}

unlink($temporaryFile);

rename($temporaryFolder.'/'.$convertedFilename, $temporaryFile);

return $temporaryFile;
}
}

0 comments on commit 5d5b2d4

Please sign in to comment.