forked from biblioverse/biblioteca
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert ebooks using kepubify on the fly
- Loading branch information
Showing
6 changed files
with
127 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace App\Kobo\Messenger; | ||
|
||
/** | ||
* Convert an ebook with kepubify binary | ||
* The destination property will be set to the path of the converted file, | ||
*/ | ||
class KepubifyMessage | ||
{ | ||
/** | ||
* @var string|null Path of the destination file (which aim to be a temporary file). Null if the conversion failed. | ||
*/ | ||
public ?string $destination = null; | ||
|
||
public function __construct( | ||
/** Path of the source ebook to convert */ | ||
public string $source | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace App\Kobo\Messenger; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
use Symfony\Component\Messenger\Attribute\AsMessageHandler; | ||
use Symfony\Component\Process\Process; | ||
|
||
#[AsMessageHandler] | ||
class KepubifyMessageHandler | ||
{ | ||
public const CACHE_FOLDER = '/kepubify'; | ||
public const TEMP_NAME_SUFFIX = 'kepub-'; | ||
|
||
public function __construct( | ||
#[Autowire(param: 'kernel.cache_dir')] | ||
private readonly string $cacheDir, | ||
private readonly LoggerInterface $logger, | ||
#[Autowire(param: 'KEPUBIFY_BIN')] | ||
private string $kepubifyBinary | ||
) { | ||
} | ||
|
||
public function __invoke(KepubifyMessage $message): void | ||
{ | ||
// Disable kepubify if the path is not set | ||
if (trim($this->kepubifyBinary) === '') { | ||
return; | ||
} | ||
|
||
// Create a temporary file | ||
$temporaryFile = $this->getTemporaryFilename(); | ||
if ($temporaryFile === false) { | ||
$this->logger->error('Error while creating temporary file'); | ||
|
||
return; | ||
} | ||
|
||
// Run the conversion | ||
$process = new Process([$this->kepubifyBinary, '--output', $temporaryFile, $message->source]); | ||
$this->logger->debug('Run kepubify command: {command}', ['command' => $process->getCommandLine()]); | ||
$process->run(); | ||
|
||
if (!$process->isSuccessful()) { | ||
$this->logger->error('Error while running kepubify: {output}: {error}', [ | ||
'output' => $process->getOutput(), | ||
'error' => $process->getErrorOutput(), | ||
]); | ||
@unlink($temporaryFile); | ||
|
||
return; | ||
} | ||
|
||
$message->destination = $temporaryFile; | ||
} | ||
|
||
/** | ||
* Create a temporary file to handle the conversion result. | ||
* Note that the name must be unique to handle concurrent requests | ||
* because the file will be deleted once the request is served. | ||
* | ||
* @return string|false | ||
*/ | ||
private function getTemporaryFilename(): string|false | ||
{ | ||
$dir = $this->cacheDir.self::CACHE_FOLDER; | ||
if (!is_dir($dir) && !mkdir($dir, 0777, true) && !is_dir($dir)) { | ||
return false; | ||
} | ||
|
||
return tempnam($dir, self::TEMP_NAME_SUFFIX); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters