@@ -46,178 +46,178 @@ |
||
46 | 46 | |
47 | 47 | class File implements IEntity, IDisplayText, IUrl { |
48 | 48 | |
49 | - private const EVENT_NAMESPACE = '\OCP\Files::'; |
|
50 | - |
|
51 | - /** @var IL10N */ |
|
52 | - protected $l10n; |
|
53 | - /** @var IURLGenerator */ |
|
54 | - protected $urlGenerator; |
|
55 | - /** @var IRootFolder */ |
|
56 | - protected $root; |
|
57 | - /** @var ILogger */ |
|
58 | - protected $logger; |
|
59 | - /** @var string */ |
|
60 | - protected $eventName; |
|
61 | - /** @var Event */ |
|
62 | - protected $event; |
|
63 | - /** @var ShareManager */ |
|
64 | - private $shareManager; |
|
65 | - /** @var IUserSession */ |
|
66 | - private $userSession; |
|
67 | - /** @var ISystemTagManager */ |
|
68 | - private $tagManager; |
|
69 | - |
|
70 | - |
|
71 | - public function __construct( |
|
72 | - IL10N $l10n, |
|
73 | - IURLGenerator $urlGenerator, |
|
74 | - IRootFolder $root, |
|
75 | - ILogger $logger, |
|
76 | - ShareManager $shareManager, |
|
77 | - IUserSession $userSession, |
|
78 | - ISystemTagManager $tagManager |
|
79 | - ) { |
|
80 | - $this->l10n = $l10n; |
|
81 | - $this->urlGenerator = $urlGenerator; |
|
82 | - $this->root = $root; |
|
83 | - $this->logger = $logger; |
|
84 | - $this->shareManager = $shareManager; |
|
85 | - $this->userSession = $userSession; |
|
86 | - $this->tagManager = $tagManager; |
|
87 | - } |
|
88 | - |
|
89 | - public function getName(): string { |
|
90 | - return $this->l10n->t('File'); |
|
91 | - } |
|
92 | - |
|
93 | - public function getIcon(): string { |
|
94 | - return $this->urlGenerator->imagePath('core', 'categories/files.svg'); |
|
95 | - } |
|
96 | - |
|
97 | - public function getEvents(): array { |
|
98 | - return [ |
|
99 | - new GenericEntityEvent($this->l10n->t('File created'), self::EVENT_NAMESPACE . 'postCreate'), |
|
100 | - new GenericEntityEvent($this->l10n->t('File updated'), self::EVENT_NAMESPACE . 'postWrite'), |
|
101 | - new GenericEntityEvent($this->l10n->t('File renamed'), self::EVENT_NAMESPACE . 'postRename'), |
|
102 | - new GenericEntityEvent($this->l10n->t('File deleted'), self::EVENT_NAMESPACE . 'postDelete'), |
|
103 | - new GenericEntityEvent($this->l10n->t('File accessed'), self::EVENT_NAMESPACE . 'postTouch'), |
|
104 | - new GenericEntityEvent($this->l10n->t('File copied'), self::EVENT_NAMESPACE . 'postCopy'), |
|
105 | - new GenericEntityEvent($this->l10n->t('Tag assigned'), MapperEvent::EVENT_ASSIGN), |
|
106 | - ]; |
|
107 | - } |
|
108 | - |
|
109 | - public function prepareRuleMatcher(IRuleMatcher $ruleMatcher, string $eventName, Event $event): void { |
|
110 | - if (!$event instanceof GenericEvent && !$event instanceof MapperEvent) { |
|
111 | - return; |
|
112 | - } |
|
113 | - $this->eventName = $eventName; |
|
114 | - $this->event = $event; |
|
115 | - try { |
|
116 | - $node = $this->getNode(); |
|
117 | - $ruleMatcher->setEntitySubject($this, $node); |
|
118 | - $ruleMatcher->setFileInfo($node->getStorage(), $node->getInternalPath()); |
|
119 | - } catch (NotFoundException $e) { |
|
120 | - // pass |
|
121 | - } |
|
122 | - } |
|
123 | - |
|
124 | - public function isLegitimatedForUserId(string $uid): bool { |
|
125 | - try { |
|
126 | - $node = $this->getNode(); |
|
127 | - if($node->getOwner()->getUID() === $uid) { |
|
128 | - return true; |
|
129 | - } |
|
130 | - $acl = $this->shareManager->getAccessList($node, true, true); |
|
131 | - return array_key_exists($uid, $acl['users']); |
|
132 | - } catch (NotFoundException $e) { |
|
133 | - return false; |
|
134 | - } |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @throws NotFoundException |
|
139 | - */ |
|
140 | - protected function getNode(): Node { |
|
141 | - if (!$this->event instanceof GenericEvent && !$this->event instanceof MapperEvent) { |
|
142 | - throw new NotFoundException(); |
|
143 | - } |
|
144 | - switch ($this->eventName) { |
|
145 | - case self::EVENT_NAMESPACE . 'postCreate': |
|
146 | - case self::EVENT_NAMESPACE . 'postWrite': |
|
147 | - case self::EVENT_NAMESPACE . 'postDelete': |
|
148 | - case self::EVENT_NAMESPACE . 'postTouch': |
|
149 | - return $this->event->getSubject(); |
|
150 | - case self::EVENT_NAMESPACE . 'postRename': |
|
151 | - case self::EVENT_NAMESPACE . 'postCopy': |
|
152 | - return $this->event->getSubject()[1]; |
|
153 | - case MapperEvent::EVENT_ASSIGN: |
|
154 | - if (!$this->event instanceof MapperEvent || $this->event->getObjectType() !== 'files') { |
|
155 | - throw new NotFoundException(); |
|
156 | - } |
|
157 | - $nodes = $this->root->getById((int)$this->event->getObjectId()); |
|
158 | - if (is_array($nodes) && !empty($nodes)) { |
|
159 | - return array_shift($nodes); |
|
160 | - } |
|
161 | - break; |
|
162 | - } |
|
163 | - throw new NotFoundException(); |
|
164 | - } |
|
165 | - |
|
166 | - public function getDisplayText(int $verbosity = 0): string { |
|
167 | - $user = $this->userSession->getUser(); |
|
168 | - try { |
|
169 | - $node = $this->getNode(); |
|
170 | - } catch (NotFoundException $e) { |
|
171 | - return ''; |
|
172 | - } |
|
173 | - |
|
174 | - $options = [ |
|
175 | - $user ? $user->getDisplayName() : $this->l10n->t('Someone'), |
|
176 | - $node->getName() |
|
177 | - ]; |
|
178 | - |
|
179 | - switch ($this->eventName) { |
|
180 | - case self::EVENT_NAMESPACE . 'postCreate': |
|
181 | - return $this->l10n->t('%s created %s', $options); |
|
182 | - case self::EVENT_NAMESPACE . 'postWrite': |
|
183 | - return $this->l10n->t('%s modified %s', $options); |
|
184 | - case self::EVENT_NAMESPACE . 'postDelete': |
|
185 | - return $this->l10n->t('%s deleted %s', $options); |
|
186 | - case self::EVENT_NAMESPACE . 'postTouch': |
|
187 | - return $this->l10n->t('%s accessed %s', $options); |
|
188 | - case self::EVENT_NAMESPACE . 'postRename': |
|
189 | - return $this->l10n->t('%s renamed %s', $options); |
|
190 | - case self::EVENT_NAMESPACE . 'postCopy': |
|
191 | - return $this->l10n->t('%s copied %s', $options); |
|
192 | - case MapperEvent::EVENT_ASSIGN: |
|
193 | - $tagNames = []; |
|
194 | - if($this->event instanceof MapperEvent) { |
|
195 | - $tagIDs = $this->event->getTags(); |
|
196 | - $tagObjects = $this->tagManager->getTagsByIds($tagIDs); |
|
197 | - foreach ($tagObjects as $systemTag) { |
|
198 | - /** @var ISystemTag $systemTag */ |
|
199 | - if($systemTag->isUserVisible()) { |
|
200 | - $tagNames[] = $systemTag->getName(); |
|
201 | - } |
|
202 | - } |
|
203 | - } |
|
204 | - $filename = array_pop($options); |
|
205 | - $tagString = implode(', ', $tagNames); |
|
206 | - if($tagString === '') { |
|
207 | - return ''; |
|
208 | - } |
|
209 | - array_push($options, $tagString, $filename); |
|
210 | - return $this->l10n->t('%s assigned %s to %s', $options); |
|
211 | - } |
|
212 | - } |
|
213 | - |
|
214 | - public function getUrl(): string { |
|
215 | - try { |
|
216 | - return $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $this->getNode()->getId()]); |
|
217 | - } catch (InvalidPathException $e) { |
|
218 | - return ''; |
|
219 | - } catch (NotFoundException $e) { |
|
220 | - return ''; |
|
221 | - } |
|
222 | - } |
|
49 | + private const EVENT_NAMESPACE = '\OCP\Files::'; |
|
50 | + |
|
51 | + /** @var IL10N */ |
|
52 | + protected $l10n; |
|
53 | + /** @var IURLGenerator */ |
|
54 | + protected $urlGenerator; |
|
55 | + /** @var IRootFolder */ |
|
56 | + protected $root; |
|
57 | + /** @var ILogger */ |
|
58 | + protected $logger; |
|
59 | + /** @var string */ |
|
60 | + protected $eventName; |
|
61 | + /** @var Event */ |
|
62 | + protected $event; |
|
63 | + /** @var ShareManager */ |
|
64 | + private $shareManager; |
|
65 | + /** @var IUserSession */ |
|
66 | + private $userSession; |
|
67 | + /** @var ISystemTagManager */ |
|
68 | + private $tagManager; |
|
69 | + |
|
70 | + |
|
71 | + public function __construct( |
|
72 | + IL10N $l10n, |
|
73 | + IURLGenerator $urlGenerator, |
|
74 | + IRootFolder $root, |
|
75 | + ILogger $logger, |
|
76 | + ShareManager $shareManager, |
|
77 | + IUserSession $userSession, |
|
78 | + ISystemTagManager $tagManager |
|
79 | + ) { |
|
80 | + $this->l10n = $l10n; |
|
81 | + $this->urlGenerator = $urlGenerator; |
|
82 | + $this->root = $root; |
|
83 | + $this->logger = $logger; |
|
84 | + $this->shareManager = $shareManager; |
|
85 | + $this->userSession = $userSession; |
|
86 | + $this->tagManager = $tagManager; |
|
87 | + } |
|
88 | + |
|
89 | + public function getName(): string { |
|
90 | + return $this->l10n->t('File'); |
|
91 | + } |
|
92 | + |
|
93 | + public function getIcon(): string { |
|
94 | + return $this->urlGenerator->imagePath('core', 'categories/files.svg'); |
|
95 | + } |
|
96 | + |
|
97 | + public function getEvents(): array { |
|
98 | + return [ |
|
99 | + new GenericEntityEvent($this->l10n->t('File created'), self::EVENT_NAMESPACE . 'postCreate'), |
|
100 | + new GenericEntityEvent($this->l10n->t('File updated'), self::EVENT_NAMESPACE . 'postWrite'), |
|
101 | + new GenericEntityEvent($this->l10n->t('File renamed'), self::EVENT_NAMESPACE . 'postRename'), |
|
102 | + new GenericEntityEvent($this->l10n->t('File deleted'), self::EVENT_NAMESPACE . 'postDelete'), |
|
103 | + new GenericEntityEvent($this->l10n->t('File accessed'), self::EVENT_NAMESPACE . 'postTouch'), |
|
104 | + new GenericEntityEvent($this->l10n->t('File copied'), self::EVENT_NAMESPACE . 'postCopy'), |
|
105 | + new GenericEntityEvent($this->l10n->t('Tag assigned'), MapperEvent::EVENT_ASSIGN), |
|
106 | + ]; |
|
107 | + } |
|
108 | + |
|
109 | + public function prepareRuleMatcher(IRuleMatcher $ruleMatcher, string $eventName, Event $event): void { |
|
110 | + if (!$event instanceof GenericEvent && !$event instanceof MapperEvent) { |
|
111 | + return; |
|
112 | + } |
|
113 | + $this->eventName = $eventName; |
|
114 | + $this->event = $event; |
|
115 | + try { |
|
116 | + $node = $this->getNode(); |
|
117 | + $ruleMatcher->setEntitySubject($this, $node); |
|
118 | + $ruleMatcher->setFileInfo($node->getStorage(), $node->getInternalPath()); |
|
119 | + } catch (NotFoundException $e) { |
|
120 | + // pass |
|
121 | + } |
|
122 | + } |
|
123 | + |
|
124 | + public function isLegitimatedForUserId(string $uid): bool { |
|
125 | + try { |
|
126 | + $node = $this->getNode(); |
|
127 | + if($node->getOwner()->getUID() === $uid) { |
|
128 | + return true; |
|
129 | + } |
|
130 | + $acl = $this->shareManager->getAccessList($node, true, true); |
|
131 | + return array_key_exists($uid, $acl['users']); |
|
132 | + } catch (NotFoundException $e) { |
|
133 | + return false; |
|
134 | + } |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @throws NotFoundException |
|
139 | + */ |
|
140 | + protected function getNode(): Node { |
|
141 | + if (!$this->event instanceof GenericEvent && !$this->event instanceof MapperEvent) { |
|
142 | + throw new NotFoundException(); |
|
143 | + } |
|
144 | + switch ($this->eventName) { |
|
145 | + case self::EVENT_NAMESPACE . 'postCreate': |
|
146 | + case self::EVENT_NAMESPACE . 'postWrite': |
|
147 | + case self::EVENT_NAMESPACE . 'postDelete': |
|
148 | + case self::EVENT_NAMESPACE . 'postTouch': |
|
149 | + return $this->event->getSubject(); |
|
150 | + case self::EVENT_NAMESPACE . 'postRename': |
|
151 | + case self::EVENT_NAMESPACE . 'postCopy': |
|
152 | + return $this->event->getSubject()[1]; |
|
153 | + case MapperEvent::EVENT_ASSIGN: |
|
154 | + if (!$this->event instanceof MapperEvent || $this->event->getObjectType() !== 'files') { |
|
155 | + throw new NotFoundException(); |
|
156 | + } |
|
157 | + $nodes = $this->root->getById((int)$this->event->getObjectId()); |
|
158 | + if (is_array($nodes) && !empty($nodes)) { |
|
159 | + return array_shift($nodes); |
|
160 | + } |
|
161 | + break; |
|
162 | + } |
|
163 | + throw new NotFoundException(); |
|
164 | + } |
|
165 | + |
|
166 | + public function getDisplayText(int $verbosity = 0): string { |
|
167 | + $user = $this->userSession->getUser(); |
|
168 | + try { |
|
169 | + $node = $this->getNode(); |
|
170 | + } catch (NotFoundException $e) { |
|
171 | + return ''; |
|
172 | + } |
|
173 | + |
|
174 | + $options = [ |
|
175 | + $user ? $user->getDisplayName() : $this->l10n->t('Someone'), |
|
176 | + $node->getName() |
|
177 | + ]; |
|
178 | + |
|
179 | + switch ($this->eventName) { |
|
180 | + case self::EVENT_NAMESPACE . 'postCreate': |
|
181 | + return $this->l10n->t('%s created %s', $options); |
|
182 | + case self::EVENT_NAMESPACE . 'postWrite': |
|
183 | + return $this->l10n->t('%s modified %s', $options); |
|
184 | + case self::EVENT_NAMESPACE . 'postDelete': |
|
185 | + return $this->l10n->t('%s deleted %s', $options); |
|
186 | + case self::EVENT_NAMESPACE . 'postTouch': |
|
187 | + return $this->l10n->t('%s accessed %s', $options); |
|
188 | + case self::EVENT_NAMESPACE . 'postRename': |
|
189 | + return $this->l10n->t('%s renamed %s', $options); |
|
190 | + case self::EVENT_NAMESPACE . 'postCopy': |
|
191 | + return $this->l10n->t('%s copied %s', $options); |
|
192 | + case MapperEvent::EVENT_ASSIGN: |
|
193 | + $tagNames = []; |
|
194 | + if($this->event instanceof MapperEvent) { |
|
195 | + $tagIDs = $this->event->getTags(); |
|
196 | + $tagObjects = $this->tagManager->getTagsByIds($tagIDs); |
|
197 | + foreach ($tagObjects as $systemTag) { |
|
198 | + /** @var ISystemTag $systemTag */ |
|
199 | + if($systemTag->isUserVisible()) { |
|
200 | + $tagNames[] = $systemTag->getName(); |
|
201 | + } |
|
202 | + } |
|
203 | + } |
|
204 | + $filename = array_pop($options); |
|
205 | + $tagString = implode(', ', $tagNames); |
|
206 | + if($tagString === '') { |
|
207 | + return ''; |
|
208 | + } |
|
209 | + array_push($options, $tagString, $filename); |
|
210 | + return $this->l10n->t('%s assigned %s to %s', $options); |
|
211 | + } |
|
212 | + } |
|
213 | + |
|
214 | + public function getUrl(): string { |
|
215 | + try { |
|
216 | + return $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $this->getNode()->getId()]); |
|
217 | + } catch (InvalidPathException $e) { |
|
218 | + return ''; |
|
219 | + } catch (NotFoundException $e) { |
|
220 | + return ''; |
|
221 | + } |
|
222 | + } |
|
223 | 223 | } |