mirror of
https://github.com/grocy/grocy.git
synced 2025-04-29 01:32:38 +00:00
Also delete downscaled image files when deleting an image (closes #1499)
This commit is contained in:
parent
34ffb96ae3
commit
90b8ea15ff
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user