Add controls on input file and subdirectory management

master
Julien Rosset 1 year ago
parent f4c9014c79
commit 5f635b3020

@ -9,6 +9,7 @@
"minimum-stability": "stable", "minimum-stability": "stable",
"require": { "require": {
"php": "^8.3", "php": "^8.3",
"jrosset/lasterrorexception": "^1.1",
"symfony/console": "^7.0" "symfony/console": "^7.0"
}, },
"autoload": { "autoload": {

@ -2,6 +2,7 @@
namespace jrosset; namespace jrosset;
use jrosset\LastErrorException\LastErrorException;
use RecursiveDirectoryIterator; use RecursiveDirectoryIterator;
use RecursiveIteratorIterator; use RecursiveIteratorIterator;
use RegexIterator; use RegexIterator;
@ -103,21 +104,23 @@ EOF
*/ */
private function processDirectory(SplFileInfo $directory, OutputInterface $output): void private function processDirectory(SplFileInfo $directory, OutputInterface $output): void
{ {
//region Check directory is readable
$output->writeln('<info>Processing directory: ' . $directory->getPathname() . '</info>'); $output->writeln('<info>Processing directory: ' . $directory->getPathname() . '</info>');
if (!$directory->isReadable()) { if (!$directory->isReadable()) {
$output->writeln('<error>The directory is not readable</error>'); $output->writeln('<error>The directory is not readable</error>');
return; return;
} }
//endregion
//region Treat each EPUB files (check recursively)
$directoryIterator = new RecursiveDirectoryIterator($directory->getPathname()); $directoryIterator = new RecursiveDirectoryIterator($directory->getPathname());
$directoryIterator = new RecursiveIteratorIterator($directoryIterator); $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 */ /** @var SplFileInfo $file */
foreach ($directoryIterator as $file) { foreach ($directoryIterator as $file) {
$this->processFile($file, $output); $this->processFile($file, $output);
} }
//endregion
} }
/** /**
@ -132,6 +135,43 @@ EOF
*/ */
private function processFile(SplFileInfo $file, OutputInterface $output): void 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
} }
} }
Loading…
Cancel
Save