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