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 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. use Psr\Log\LoggerInterface;
  32. /**
  33. * @template-implements IEventListener<NodeRemovedFromCache>
  34. * @template-implements IEventListener<NodeDeletedEvent>
  35. * @template-implements IEventListener<NodeWrittenEvent>
  36. */
  37. class FileEventListener implements IEventListener {
  38. private IMetadataManager $manager;
  39. private LoggerInterface $logger;
  40. public function __construct(IMetadataManager $manager, LoggerInterface $logger) {
  41. $this->manager = $manager;
  42. $this->logger = $logger;
  43. }
  44. private function shouldExtractMetadata(Node $node): bool {
  45. try {
  46. if ($node->getMimetype() === 'httpd/unix-directory') {
  47. return false;
  48. }
  49. } catch (NotFoundException $e) {
  50. return false;
  51. }
  52. if ($node->getSize(false) <= 0) {
  53. return false;
  54. }
  55. $path = $node->getPath();
  56. return $this->isCorrectPath($path);
  57. }
  58. private function isCorrectPath(string $path): bool {
  59. // TODO make this more dynamic, we have the same issue in other places
  60. return !str_starts_with($path, 'appdata_') && !str_starts_with($path, 'files_versions/') && !str_starts_with($path, 'files_trashbin/');
  61. }
  62. public function handle(Event $event): void {
  63. if ($event instanceof NodeRemovedFromCache) {
  64. if (!$this->isCorrectPath($event->getPath())) {
  65. // Don't listen to paths for which we don't extract metadata
  66. return;
  67. }
  68. $view = Filesystem::getView();
  69. if (!$view) {
  70. // Should not happen since a scan in the user folder should setup
  71. // the file system.
  72. $e = new \Exception(); // don't trigger, just get backtrace
  73. $this->logger->error('Detecting deletion of a file with possible metadata but file system setup is not setup', [
  74. 'exception' => $e,
  75. 'app' => 'metadata'
  76. ]);
  77. return;
  78. }
  79. $info = $view->getFileInfo($event->getPath());
  80. if ($info && $info->getType() === FileInfo::TYPE_FILE) {
  81. $this->manager->clearMetadata($info->getId());
  82. }
  83. }
  84. if ($event instanceof NodeDeletedEvent) {
  85. $node = $event->getNode();
  86. if ($this->shouldExtractMetadata($node)) {
  87. /** @var File $node */
  88. $this->manager->clearMetadata($event->getNode()->getId());
  89. }
  90. }
  91. if ($event instanceof NodeWrittenEvent) {
  92. $node = $event->getNode();
  93. if ($this->shouldExtractMetadata($node)) {
  94. /** @var File $node */
  95. $this->manager->generateMetadata($event->getNode(), false);
  96. }
  97. }
  98. }
  99. }