Skip to content

Commit 3be98c3

Browse files
authored
- Fix broken filehhooks for adding new geophotos and tracks to maps: (#1420)
Fix and rewrite isUserNode to accept files from other storages than IHomeStorage e.g. include photos and tracks from external_files or shares. - Fix possible assert in addByFile(Node $file) if getAccessList($file) returns empty result. Signed-off-by: umgfoin <[email protected]>
1 parent d648548 commit 3be98c3

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

lib/Hooks/FileHooks.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
use OCA\Maps\Service\PhotofilesService;
1717
use OCA\Maps\Service\TracksService;
1818
use OCP\Files\FileInfo;
19-
use OCP\Files\IHomeStorage;
2019
use OCP\Files\IRootFolder;
2120
use OCP\Lock\ILockingProvider;
2221
use OCP\Share;
2322
use OCP\Util;
23+
use function OCP\Log\logger;
2424

2525
/**
2626
* Handles files events
@@ -44,7 +44,8 @@ public function __construct(IRootFolder $root, PhotofilesService $photofilesServ
4444

4545
public function register() {
4646
$fileWriteCallback = function (\OCP\Files\Node $node) {
47-
if ($this->isUserNode($node) && $node->getSize() > 0) {
47+
//logger('maps')->debug("Hook postWrite");
48+
if ($node->getType() === FileInfo::TYPE_FILE && $this->isUserNode($node) && $node->getSize()) {
4849
$path = $node->getPath();
4950
if (!$this->lockingProvider->isLocked($path, ILockingProvider::LOCK_SHARED)
5051
and !$this->lockingProvider->isLocked($path, ILockingProvider::LOCK_EXCLUSIVE)
@@ -59,6 +60,7 @@ public function register() {
5960
$this->root->listen('\OC\Files', 'postWrite', $fileWriteCallback);
6061

6162
$fileDeletionCallback = function (\OCP\Files\Node $node) {
63+
//logger('maps')->debug("Hook preDelete");
6264
if ($this->isUserNode($node)) {
6365
if ($node->getType() === FileInfo::TYPE_FOLDER) {
6466
$this->photofilesService->deleteByFolder($node);
@@ -111,6 +113,7 @@ public function register() {
111113
}
112114

113115
public function postShare($params) {
116+
//logger('maps')->debug("Hook postShare");
114117
if ($params['itemType'] === 'file') {
115118
//$targetFilePath = $params['itemTarget'];
116119
//$sourceUserId = $params['uidOwner'];
@@ -135,6 +138,7 @@ public function postShare($params) {
135138
}
136139

137140
public function postUnShare($params) {
141+
//logger('maps')->debug("Hook postUnShare");
138142
if ($params['shareType'] === Share::SHARE_TYPE_USER) {
139143
if ($params['itemType'] === 'file') {
140144
$targetUserId = $params['shareWith'];
@@ -146,6 +150,7 @@ public function postUnShare($params) {
146150
}
147151

148152
public function preUnShare($params) {
153+
//logger('maps')->debug("Hook preUnShare");
149154
if ($params['shareType'] === Share::SHARE_TYPE_USER) {
150155
if ($params['itemType'] === 'folder') {
151156
$targetUserId = $params['shareWith'];
@@ -176,7 +181,12 @@ private function getNodeForPath($path) {
176181
}
177182

178183
private function isUserNode(\OCP\Files\Node $node): bool {
179-
return $node->getStorage()->instanceOfStorage(IHomeStorage::class);
184+
//return $node->getStorage()->instanceOfStorage("\OCP\Files\IHomeStorage")
185+
$owner = $node->getStorage()->getOwner('');
186+
if (! $owner) {
187+
return false;
188+
}
189+
return str_starts_with($node->getPath(), '/' . $owner . '/');
180190
}
181191

182192
}

lib/Service/PhotofilesService.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,16 @@ public function rescan($userId, $inBackground = true, $pathToScan = null) {
9696
// add the file for its owner and users that have access
9797
// check if it's already in DB before adding
9898
public function addByFile(Node $file) {
99-
$ownerId = $file->getOwner()->getUID();
10099
if ($this->isPhoto($file)) {
100+
$ownerId = $file->getOwner()->getUID();
101101
$this->addPhoto($file, $ownerId);
102102
// is the file accessible to other users ?
103103
$accesses = $this->shareManager->getAccessList($file);
104-
foreach ($accesses['users'] as $uid) {
105-
if ($uid !== $ownerId) {
106-
$this->addPhoto($file, $uid);
104+
if (array_key_exists('users', $accesses)) {
105+
foreach ($accesses['users'] as $uid) {
106+
if ($uid !== $ownerId) {
107+
$this->addPhoto($file, $uid);
108+
}
107109
}
108110
}
109111
return true;

0 commit comments

Comments
 (0)