cap340 / CleanMedia

Magento 2 CLI Command : remove images of deleted product in the /media/catalog/product folder
29 stars 16 forks source link

SplFileInfo::getSize(): stat failed for /some/path/some_image.jpg #16

Closed vctsvz closed 4 years ago

vctsvz commented 4 years ago

Counting the file size $size += $file->getSize(); occurs after the file is deleted $this->driverFile->deleteFile($file);, which leads to an error: SplFileInfo::getSize(): stat failed for /some/path/some_image.jpg

vendor/cap/cleanmedia/Console/Command/CleanMedia.php:118-137

/** @var SplFileInfo $file */
foreach ($iterator as $file) {
    if ($isNoCache) {
        if (strpos($file, "/cache") !== false) {
            continue;
        }
    }
    $filename = $file->getFilename();
    if (!in_array($filename, $inDb)) {
        $fileRelativePath = str_replace($mediaPath, '', $file->getPathname());
        if (!$isDryRun) {
            $output->writeln('<comment>REMOVING: </comment>' . $fileRelativePath);
            $this->driverFile->deleteFile($file);
        } else {
            $output->writeln('<comment>DRY-RUN: </comment>' . $fileRelativePath);
        }
        $count++;
        $size += $file->getSize();
    }
}