diff options
author | Julien Veyssier <eneiluj@posteo.net> | 2021-11-25 18:38:13 +0300 |
---|---|---|
committer | Julien Veyssier <eneiluj@posteo.net> | 2022-01-03 12:27:37 +0300 |
commit | 551729e08d7c044ae34d8972129caa448725f7a9 (patch) | |
tree | fa810c00048ae6de3a92ffa965aac2cd57aba51c /lib/Listeners | |
parent | fc56680d9621a7fb8cc9fd75d0ad0230de3c9884 (diff) |
implement auto attachment management for file deletion/move/copy
Signed-off-by: Julien Veyssier <eneiluj@posteo.net>
Diffstat (limited to 'lib/Listeners')
-rw-r--r-- | lib/Listeners/BeforeNodeDeletedListener.php | 50 | ||||
-rw-r--r-- | lib/Listeners/BeforeNodeRenamedListener.php | 53 | ||||
-rw-r--r-- | lib/Listeners/NodeCopiedListener.php | 55 |
3 files changed, 158 insertions, 0 deletions
diff --git a/lib/Listeners/BeforeNodeDeletedListener.php b/lib/Listeners/BeforeNodeDeletedListener.php new file mode 100644 index 000000000..eac7b28d6 --- /dev/null +++ b/lib/Listeners/BeforeNodeDeletedListener.php @@ -0,0 +1,50 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2021 Julien Veyssier <eneiluj@posteo.net> + * + * @author Julien Veyssier <eneiluj@posteo.net> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Text\Listeners; + +use OCA\Text\Service\ImageService; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Files\Events\Node\BeforeNodeDeletedEvent; +use OCP\Files\File; + +class BeforeNodeDeletedListener implements IEventListener { + + private ImageService $imageService; + + public function __construct(ImageService $imageService) { + $this->imageService = $imageService; + } + + public function handle(Event $event): void { + if (!$event instanceof BeforeNodeDeletedEvent) { + return; + } + if ($event->getNode() instanceof File && $event->getNode()->getMimeType() === 'text/markdown') { + $this->imageService->deleteAttachments($event->getNode()); + } + } +} diff --git a/lib/Listeners/BeforeNodeRenamedListener.php b/lib/Listeners/BeforeNodeRenamedListener.php new file mode 100644 index 000000000..483c2f9a6 --- /dev/null +++ b/lib/Listeners/BeforeNodeRenamedListener.php @@ -0,0 +1,53 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2021 Julien Veyssier <eneiluj@posteo.net> + * + * @author Julien Veyssier <eneiluj@posteo.net> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Text\Listeners; + +use OCA\Text\Service\ImageService; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Files\Events\Node\BeforeNodeRenamedEvent; +use OCP\Files\File; + +class BeforeNodeRenamedListener implements IEventListener { + + private ImageService $imageService; + + public function __construct(ImageService $imageService) { + $this->imageService = $imageService; + } + + public function handle(Event $event): void { + if (!$event instanceof BeforeNodeRenamedEvent) { + return; + } + if ($event->getSource() instanceof File + && $event->getSource()->getMimeType() === 'text/markdown' + && $event->getTarget() instanceof File + ) { + $this->imageService->moveAttachments($event->getSource(), $event->getTarget()); + } + } +} diff --git a/lib/Listeners/NodeCopiedListener.php b/lib/Listeners/NodeCopiedListener.php new file mode 100644 index 000000000..64b66c166 --- /dev/null +++ b/lib/Listeners/NodeCopiedListener.php @@ -0,0 +1,55 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2021 Julien Veyssier <eneiluj@posteo.net> + * + * @author Julien Veyssier <eneiluj@posteo.net> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Text\Listeners; + +use OCA\Text\Service\ImageService; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Files\Events\Node\NodeCopiedEvent; +use OCP\Files\File; + +class NodeCopiedListener implements IEventListener { + + private ImageService $imageService; + + public function __construct(ImageService $imageService) { + $this->imageService = $imageService; + } + + public function handle(Event $event): void { + if (!$event instanceof NodeCopiedEvent) { + return; + } + error_log('COPIED source ' . $event->getSource()->getId() . ' target '. $event->getTarget()->getId()); + if ($event->getSource() instanceof File + && $event->getSource()->getMimeType() === 'text/markdown' + && $event->getTarget() instanceof File + && $event->getTarget()->getMimeType() === 'text/markdown' + ) { + $this->imageService->copyAttachments($event->getSource(), $event->getTarget()); + } + } +} |