[File] Fix file deletion violating foreign keys

This commit is contained in:
Alexei Sorokin 2020-08-13 23:56:31 +03:00
parent 33caf31237
commit b65c200922
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 // Clear out related things in the database and filesystem, such as thumbnails
if (Event::handle('FileDeleteRelated', array($this))) { $related = [
$thumbs = new File_thumbnail(); 'File_redirection',
$thumbs->file_id = $this->id; 'File_thumbnail',
if ($thumbs->find()) { 'File_to_post',
while ($thumbs->fetch()) { ];
$thumbs->delete(); Event::handle('FileDeleteRelated', [$this, &$related]);
}
}
$f2p = new File_to_post(); foreach ($related as $cls) {
$f2p->file_id = $this->id; $inst = new $cls();
if ($f2p->find()) { $inst->file_id = $this->id;
while ($f2p->fetch()) { if ($inst->find()) {
$f2p->delete(); while ($inst->fetch()) {
$inst->delete();
} }
} }
} }

View File

@ -440,6 +440,12 @@ class EmbedPlugin extends Plugin
return !file_exists($imgPath); 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 * @return bool false on no check made, provider name on success
* @throws ServerException if check is made but fails * @throws ServerException if check is made but fails