Also delete downscaled image files when deleting an image (closes #1499)

This commit is contained in:
Bernd Bestel 2021-07-02 20:50:52 +02:00
parent 34ffb96ae3
commit 90b8ea15ff
No known key found for this signature in database
GPG Key ID: 71BD34C0D4891300
2 changed files with 28 additions and 11 deletions

View File

@ -25,12 +25,7 @@ class FilesApiController extends BaseApiController
throw new \Exception('Invalid filename');
}
$filePath = $this->getFilesService()->GetFilePath($args['group'], $fileName);
if (file_exists($filePath))
{
unlink($filePath);
}
$this->getFilesService()->DeleteFile($args['group'], $fileName);
return $this->EmptyApiResponse($response);
}

View File

@ -19,11 +19,6 @@ class FilesService extends BaseService
$fileNameDownscaled = $fileNameWithoutExtension . '__downscaledto' . ($bestFitHeight ? $bestFitHeight : 'auto') . 'x' . ($bestFitWidth ? $bestFitWidth : 'auto') . '.' . $fileExtension;
$filePathDownscaled = $this->GetFilePath($group, $fileNameDownscaled);
if (!extension_loaded('gd'))
{
return $filePath;
}
try
{
if (!file_exists($filePathDownscaled))
@ -54,6 +49,33 @@ class FilesService extends BaseService
return $filePathDownscaled;
}
public function DeleteFile($group, $fileName)
{
$filePath = $this->GetFilePath($group, $fileName);
if (file_exists($filePath))
{
$fileNameWithoutExtension = pathinfo($filePath, PATHINFO_FILENAME);
$fileExtension = pathinfo($filePath, PATHINFO_EXTENSION);
if (getimagesize($filePath) !== false) // Then the file is an image
{
// Also delete all corresponding "__downscaledto" files when deleting an image
$groupFolderPath = $this->StoragePath . '/' . $group;
$files = scandir($groupFolderPath);
foreach($files as $file)
{
if (string_starts_with($file, $fileNameWithoutExtension . '__downscaledto'))
{
unlink($this->GetFilePath($group, $file));
}
}
}
unlink($filePath);
}
}
public function GetFilePath($group, $fileName)
{
$groupFolderPath = $this->StoragePath . '/' . $group;