[Attachment] Allow to delete the associated file

This commit is contained in:
Diogo Peralta Cordeiro 2021-08-12 00:25:25 +01:00 committed by Hugo Sales
parent 508f1f8796
commit 78c5c4b084
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0

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
*/ */