mirror of
https://github.com/grocy/grocy.git
synced 2025-08-17 11:06:36 +00:00
Also delete downscaled image files when deleting an image (closes #1499)
This commit is contained in:
@@ -25,12 +25,7 @@ class FilesApiController extends BaseApiController
|
|||||||
throw new \Exception('Invalid filename');
|
throw new \Exception('Invalid filename');
|
||||||
}
|
}
|
||||||
|
|
||||||
$filePath = $this->getFilesService()->GetFilePath($args['group'], $fileName);
|
$this->getFilesService()->DeleteFile($args['group'], $fileName);
|
||||||
|
|
||||||
if (file_exists($filePath))
|
|
||||||
{
|
|
||||||
unlink($filePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->EmptyApiResponse($response);
|
return $this->EmptyApiResponse($response);
|
||||||
}
|
}
|
||||||
|
@@ -19,11 +19,6 @@ class FilesService extends BaseService
|
|||||||
$fileNameDownscaled = $fileNameWithoutExtension . '__downscaledto' . ($bestFitHeight ? $bestFitHeight : 'auto') . 'x' . ($bestFitWidth ? $bestFitWidth : 'auto') . '.' . $fileExtension;
|
$fileNameDownscaled = $fileNameWithoutExtension . '__downscaledto' . ($bestFitHeight ? $bestFitHeight : 'auto') . 'x' . ($bestFitWidth ? $bestFitWidth : 'auto') . '.' . $fileExtension;
|
||||||
$filePathDownscaled = $this->GetFilePath($group, $fileNameDownscaled);
|
$filePathDownscaled = $this->GetFilePath($group, $fileNameDownscaled);
|
||||||
|
|
||||||
if (!extension_loaded('gd'))
|
|
||||||
{
|
|
||||||
return $filePath;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (!file_exists($filePathDownscaled))
|
if (!file_exists($filePathDownscaled))
|
||||||
@@ -54,6 +49,33 @@ class FilesService extends BaseService
|
|||||||
return $filePathDownscaled;
|
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)
|
public function GetFilePath($group, $fileName)
|
||||||
{
|
{
|
||||||
$groupFolderPath = $this->StoragePath . '/' . $group;
|
$groupFolderPath = $this->StoragePath . '/' . $group;
|
||||||
|
Reference in New Issue
Block a user