diff --git a/composer.json b/composer.json
index 6880b6b..507bbc3 100644
--- a/composer.json
+++ b/composer.json
@@ -9,6 +9,7 @@
"minimum-stability": "stable",
"require": {
"php": "^8.3",
+ "jrosset/lasterrorexception": "^1.1",
"symfony/console": "^7.0"
},
"autoload": {
diff --git a/src/Main.php b/src/Main.php
index ad26497..20f9da6 100644
--- a/src/Main.php
+++ b/src/Main.php
@@ -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('Processing directory: ' . $directory->getPathname() . '');
-
if (!$directory->isReadable()) {
$output->writeln('The directory is not readable');
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('Processing file: ' . $file->getPathname() . '');
+
+ if (mb_strtolower($file->getExtension()) !== 'epub') {
+ $output->writeln('Invalid file extension, "epub" expected');
+ return;
+ }
+ if (!$file->isReadable()) {
+ $output->writeln('The file is not readable');
+ return;
+ }
+ $fileParentDirectory = new SplFileInfo(dirname($file->getRealPath()));
+ if (!$fileParentDirectory->isReadable()) {
+ $output->writeln('The file parent directory is not readable');
+ return;
+ }
+ if (!$fileParentDirectory->isWritable()) {
+ $output->writeln('The file parent directory is not writable');
+ 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('Failed to create subdirectory "' . $fileParentDirectory->getPathname() . '": ' . (new LastErrorException())->getMessage() . '');
+ return;
+ }
+
+ $oldFile = $file;
+ $file = new SplFileInfo($fileParentDirectory . DIRECTORY_SEPARATOR . $file->getFilename());
+ if (rename($oldFile->getPathname(), $file->getPathname())) {
+ $output->writeln('Failed to move file to subdirectory "' . $oldFile->getPathname() . '" => "' . $file->getPathname() . '": ' . (new LastErrorException())->getMessage() . '');
+ return;
+ }
+ }
+ //endregion
}
}
\ No newline at end of file