You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileEventListener.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright 2022 Carl Schwan <carl@carlschwan.eu>
  5. * @license AGPL-3.0-or-later
  6. *
  7. * This code is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License, version 3,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License, version 3,
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>
  18. *
  19. */
  20. namespace OC\Metadata;
  21. use OC\Files\Filesystem;
  22. use OCP\EventDispatcher\Event;
  23. use OCP\EventDispatcher\IEventListener;
  24. use OCP\Files\Events\Node\NodeDeletedEvent;
  25. use OCP\Files\Events\Node\NodeWrittenEvent;
  26. use OCP\Files\Events\NodeRemovedFromCache;
  27. use OCP\Files\File;
  28. use OCP\Files\Node;
  29. use OCP\Files\NotFoundException;
  30. use OCP\Files\FileInfo;
  31. class FileEventListener implements IEventListener {
  32. private IMetadataManager $manager;
  33. public function __construct(IMetadataManager $manager) {
  34. $this->manager = $manager;
  35. }
  36. private function shouldExtractMetadata(Node $node): bool {
  37. try {
  38. if ($node->getMimetype() === 'httpd/unix-directory') {
  39. return false;
  40. }
  41. } catch (NotFoundException $e) {
  42. return false;
  43. }
  44. if ($node->getSize(false) <= 0) {
  45. return false;
  46. }
  47. $path = $node->getPath();
  48. // TODO make this more dynamic, we have the same issue in other places
  49. return !str_starts_with($path, 'appdata_') && !str_starts_with($path, 'files_versions/') && !str_starts_with($path, 'files_trashbin/');
  50. }
  51. public function handle(Event $event): void {
  52. if ($event instanceof NodeRemovedFromCache) {
  53. $view = Filesystem::getView();
  54. $info = $view->getFileInfo($event->getPath());
  55. if ($info && $info->getType() === FileInfo::TYPE_FILE) {
  56. $this->manager->clearMetadata($info->getId());
  57. }
  58. }
  59. if ($event instanceof NodeDeletedEvent) {
  60. $node = $event->getNode();
  61. if ($this->shouldExtractMetadata($node)) {
  62. /** @var File $node */
  63. $this->manager->clearMetadata($event->getNode()->getId());
  64. }
  65. }
  66. if ($event instanceof NodeWrittenEvent) {
  67. $node = $event->getNode();
  68. if ($this->shouldExtractMetadata($node)) {
  69. /** @var File $node */
  70. $this->manager->generateMetadata($event->getNode(), false);
  71. }
  72. }
  73. }
  74. }