|
|
|
@ -2,6 +2,7 @@
|
|
|
|
|
|
|
|
|
|
namespace jrosset;
|
|
|
|
|
|
|
|
|
|
use jrosset\LastErrorException\LastErrorException;
|
|
|
|
|
use RecursiveDirectoryIterator;
|
|
|
|
|
use RecursiveIteratorIterator;
|
|
|
|
|
use RegexIterator;
|
|
|
|
@ -103,21 +104,23 @@ EOF
|
|
|
|
|
*/
|
|
|
|
|
private function processDirectory(SplFileInfo $directory, OutputInterface $output): void
|
|
|
|
|
{
|
|
|
|
|
//region Check directory is readable
|
|
|
|
|
$output->writeln('<info>Processing directory: ' . $directory->getPathname() . '</info>');
|
|
|
|
|
|
|
|
|
|
if (!$directory->isReadable()) {
|
|
|
|
|
$output->writeln('<error>The directory is not readable</error>');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//endregion
|
|
|
|
|
//region Treat each EPUB files (check recursively)
|
|
|
|
|
$directoryIterator = new RecursiveDirectoryIterator($directory->getPathname());
|
|
|
|
|
$directoryIterator = new RecursiveIteratorIterator($directoryIterator);
|
|
|
|
|
$directoryIterator = new RegexIterator($directoryIterator, /** @lang PhpRegExp */ '/\.epub$/', RegexIterator::MATCH);
|
|
|
|
|
$directoryIterator = new RegexIterator($directoryIterator, /** @lang PhpRegExp */ '/\.epub$/i', RegexIterator::MATCH);
|
|
|
|
|
|
|
|
|
|
/** @var SplFileInfo $file */
|
|
|
|
|
foreach ($directoryIterator as $file) {
|
|
|
|
|
$this->processFile($file, $output);
|
|
|
|
|
}
|
|
|
|
|
//endregion
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -132,6 +135,43 @@ EOF
|
|
|
|
|
*/
|
|
|
|
|
private function processFile(SplFileInfo $file, OutputInterface $output): void
|
|
|
|
|
{
|
|
|
|
|
//region Check file is OK for processing
|
|
|
|
|
$output->writeln('<info>Processing file: ' . $file->getPathname() . '</info>');
|
|
|
|
|
|
|
|
|
|
if (mb_strtolower($file->getExtension()) !== 'epub') {
|
|
|
|
|
$output->writeln('<error>Invalid file extension, "epub" expected</error>');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!$file->isReadable()) {
|
|
|
|
|
$output->writeln('<error>The file is not readable</error>');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$fileParentDirectory = new SplFileInfo(dirname($file->getRealPath()));
|
|
|
|
|
if (!$fileParentDirectory->isReadable()) {
|
|
|
|
|
$output->writeln('<error>The file parent directory is not readable</error>');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!$fileParentDirectory->isWritable()) {
|
|
|
|
|
$output->writeln('<error>The file parent directory is not writable</error>');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//endregion
|
|
|
|
|
//region Create subdirectory for the file (if necessary)
|
|
|
|
|
if (count(scandir($fileParentDirectory->getPathname())) > 1) {
|
|
|
|
|
$fileParentDirectory = new SplFileInfo($fileParentDirectory->getPathname() . DIRECTORY_SEPARATOR . $file->getBasename('.' . $file->getExtension()));
|
|
|
|
|
if (!mkdir($fileParentDirectory->getPathname())) {
|
|
|
|
|
$output->writeln('<error>Failed to create subdirectory "' . $fileParentDirectory->getPathname() . '": ' . (new LastErrorException())->getMessage() . '</error>');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$oldFile = $file;
|
|
|
|
|
$file = new SplFileInfo($fileParentDirectory . DIRECTORY_SEPARATOR . $file->getFilename());
|
|
|
|
|
if (rename($oldFile->getPathname(), $file->getPathname())) {
|
|
|
|
|
$output->writeln('<error>Failed to move file to subdirectory "' . $oldFile->getPathname() . '" => "' . $file->getPathname() . '": ' . (new LastErrorException())->getMessage() . '</error>');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//endregion
|
|
|
|
|
}
|
|
|
|
|
}
|