Skip to content

Commit

Permalink
Merge pull request #583 from ColinFrick/sanity-check
Browse files Browse the repository at this point in the history
fix(delete): sanity check filename when deleting file
  • Loading branch information
rosell-dk authored Apr 15, 2023
2 parents 35d0a6f + 25af3be commit 0c50bb7
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions lib/classes/HandleDeleteFileHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace WebPExpress;
use \WebPExpress\Convert;
use \WebPExpress\Mime;
use \WebPExpress\SanityCheck;

class HandleDeleteFileHook
{
Expand All @@ -12,22 +13,28 @@ class HandleDeleteFileHook
*/
public static function deleteAssociatedWebP($filename)
{
$mimeTypes = [
'image/jpeg',
'image/png',
];
if (!Mime::isOneOfTheseImageMimeTypes($filename, $mimeTypes)) {
return $filename;
}
try {
$filename = SanityCheck::absPathExistsAndIsFileInDocRoot($filename);

$mimeTypes = [
'image/jpeg',
'image/png',
];
if (!Mime::isOneOfTheseImageMimeTypes($filename, $mimeTypes)) {
return $filename;
}

$config = Config::loadConfigAndFix();
$destination = Convert::getDestination($filename, $config);
if (@file_exists($destination)) {
if (@unlink($destination)) {
Convert::updateBiggerThanOriginalMark($filename, $destination, $config);
} else {
error_log('WebP Express failed deleting webp:' . $destination);
$config = Config::loadConfigAndFix();
$destination = Convert::getDestination($filename, $config);
if (@file_exists($destination)) {
if (@unlink($destination)) {
Convert::updateBiggerThanOriginalMark($filename, $destination, $config);
} else {
error_log('WebP Express failed deleting webp:' . $destination);
}
}
} catch (SanityException $e) {
// fail silently. (maybe we should write to debug log instead?)
}

return $filename;
Expand Down

0 comments on commit 0c50bb7

Please sign in to comment.