[Attachment] Allow to delete the associated file

This commit is contained in:
Diogo Peralta Cordeiro 2021-08-12 00:25:25 +01:00
parent 6453593b0d
commit b20a4c89fb
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
1 changed files with 22 additions and 1 deletions

View File

@ -71,7 +71,6 @@ class Attachment extends Entity
/** /**
* @return int * @return int
*/ */
public function getLives(): int public function getLives(): int
{ {
return $this->lives; return $this->lives;
@ -205,6 +204,28 @@ class Attachment extends Entity
return true; return true;
} }
/**
* Attachment delete always removes dependencies, cleanups and flushes
*/
public function deleteStorage(): bool
{
if (!is_null($filepath = $this->getPath())) {
if (file_exists($filepath)) {
if (@unlink($filepath) === false) {
Log::error("Failed deleting file for attachment with id={$this->getId()} at {$filepath}.");
return false;
} else {
$this->setFilename(null);
DB::persist($this);
DB::flush();
}
} else {
Log::warning("File for attachment with id={$this->getId()} at {$filepath} was already deleted when I was going to handle it.");
}
}
return true;
}
/** /**
* Attachment delete always removes dependencies, cleanups and flushes * Attachment delete always removes dependencies, cleanups and flushes
*/ */