[File] Fix file deletion violating foreign keys

This commit is contained in:
Alexei Sorokin 2020-08-13 23:56:31 +03:00 committed by Diogo Peralta Cordeiro
parent 8fc807343b
commit e62e429302
2 changed files with 18 additions and 13 deletions

View File

@ -850,20 +850,19 @@ class File extends Managed_DataObject
}
// Clear out related things in the database and filesystem, such as thumbnails
if (Event::handle('FileDeleteRelated', array($this))) {
$thumbs = new File_thumbnail();
$thumbs->file_id = $this->id;
if ($thumbs->find()) {
while ($thumbs->fetch()) {
$thumbs->delete();
}
}
$related = [
'File_redirection',
'File_thumbnail',
'File_to_post',
];
Event::handle('FileDeleteRelated', [$this, &$related]);
$f2p = new File_to_post();
$f2p->file_id = $this->id;
if ($f2p->find()) {
while ($f2p->fetch()) {
$f2p->delete();
foreach ($related as $cls) {
$inst = new $cls();
$inst->file_id = $this->id;
if ($inst->find()) {
while ($inst->fetch()) {
$inst->delete();
}
}
}

View File

@ -440,6 +440,12 @@ class EmbedPlugin extends Plugin
return !file_exists($imgPath);
}
public function onFileDeleteRelated(File $file, array &$related): bool
{
$related[] = 'File_embed';
return true;
}
/**
* @return bool false on no check made, provider name on success
* @throws ServerException if check is made but fails