Skip to content

Commit

Permalink
Improving getFilePaths performance and exclude unnecessary directorie…
Browse files Browse the repository at this point in the history
…s from recursive reading
  • Loading branch information
Maksold committed Jul 26, 2024
1 parent 2441a1a commit 50fb547
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions Command/CatalogAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,28 @@ protected function getCacheDirectoryPath()
.DIRECTORY_SEPARATOR.'catalog'.DIRECTORY_SEPARATOR.'product'.DIRECTORY_SEPARATOR.'cache';
}

/**
* Gets the product placeholder directory path
* @return string
* @throws \Magento\Framework\Exception\FileSystemException
*/
protected function getPlaceholderDirectoryPath(): string
{
return $this->directoryList->getPath(DirectoryList::MEDIA)
.DIRECTORY_SEPARATOR.'catalog'.DIRECTORY_SEPARATOR.'product'.DIRECTORY_SEPARATOR.'placeholder';
}

/**
* Gets the Mirasvit SEO-friendly product image directory path
* @return string
* @throws \Magento\Framework\Exception\FileSystemException
*/
protected function getMirasvitSEODirectoryPath(): string
{
return $this->directoryList->getPath(DirectoryList::MEDIA)
.DIRECTORY_SEPARATOR.'catalog'.DIRECTORY_SEPARATOR.'product'.DIRECTORY_SEPARATOR.'image';
}

/**
* Get the file paths for all media files exclusing cache files
* @param $useCache boolean
Expand All @@ -172,11 +194,21 @@ protected function getFilePaths($useCache = true)
if (!$this->driverFile->isExists($mediaDirectoryPath)) {
return [];
}
$cacheFiles = $this->getCacheFilePaths();
$this->filePaths = [];
foreach ($this->driverFile->readDirectoryRecursively($mediaDirectoryPath) as $filePath) {
if (!in_array($filePath, $cacheFiles, true) && !$this->driverFile->isDirectory($filePath)) {
$this->filePaths[] = $filePath;
$excludedPaths = [
$this->getCacheDirectoryPath(),
$this->getMirasvitSEODirectoryPath(),
$this->getPlaceholderDirectoryPath()
];
foreach ($this->driverFile->readDirectory($mediaDirectoryPath) as $dirsPath) {
if (!$this->driverFile->isDirectory($dirsPath) || in_array($dirsPath, $excludedPaths, true)) {
continue; // Skip files and excluded dirs
}

foreach ($this->driverFile->readDirectoryRecursively($dirsPath) as $filePath) {
if (!$this->driverFile->isDirectory($filePath)) {
$this->filePaths[] = $filePath;
}
}
}
return $this->filePaths;
Expand Down

0 comments on commit 50fb547

Please sign in to comment.