[Avatar] Delete attachment only if safe

This commit is contained in:
Diogo Peralta Cordeiro 2021-08-05 13:49:59 +01:00 committed by Hugo Sales
parent 21362d1e4d
commit a77f51dd06
No known key found for this signature in database
GPG Key ID: 7D0C7EAFC9D835A0

View File

@ -23,7 +23,6 @@ namespace Component\Avatar\Entity;
use App\Core\DB\DB;
use App\Core\Entity;
use App\Core\Log;
use App\Core\Router\Router;
use App\Entity\Attachment;
use App\Util\Common;
@ -123,25 +122,21 @@ class Avatar extends Entity
}
/**
* Delete this avatar and the corresponding file and thumbnails, which this owns
* Delete this avatar and, if safe, the corresponding file and thumbnails, which this owns
*
* Inefficient implementation, but there are plenty of edge cases and this is supposed to be a rare operation
* @param bool $cascade
* @param bool $flush
*/
public function delete(bool $cascade = true, bool $flush = true): void
{
if ($cascade) {
// Avatar doesn't own the file, but it's stored in a different place than Attachment
// would think, so we need to handle it ourselves. Since the attachment could be shared,
// can only delete if cascading
$filepath = $this->getPath();
if (file_exists($filepath)) {
if (@unlink($filepath) === false) {
Log::warning("Failed deleting attachment for avatar with id={$this->attachment_id} at {$filepath}");
}
}
$this->attachment->delete(cascade: true, flush: false);
}
DB::remove($this);
if ($cascade) {
$attachment = $this->getAttachment();
// We can't use $attachment->isSafeDelete() because underlying findBy doesn't respect remove persistence
if ($attachment->countDependencies() - 1 === 0) {
$attachment->delete(cascade: true, flush: false);
}
}
if ($flush) {
DB::flush();
}