- 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 <umgfoin@users.noreply.github.com>
This commit is contained in:
umgfoin
2025-05-26 13:29:36 +02:00
committed by GitHub
parent d648548513
commit 3be98c3ecb
2 changed files with 19 additions and 7 deletions

View File

@ -16,11 +16,11 @@ use OC\Files\Filesystem;
use OCA\Maps\Service\PhotofilesService;
use OCA\Maps\Service\TracksService;
use OCP\Files\FileInfo;
use OCP\Files\IHomeStorage;
use OCP\Files\IRootFolder;
use OCP\Lock\ILockingProvider;
use OCP\Share;
use OCP\Util;
use function OCP\Log\logger;
/**
* Handles files events
@ -44,7 +44,8 @@ class FileHooks {
public function register() {
$fileWriteCallback = function (\OCP\Files\Node $node) {
if ($this->isUserNode($node) && $node->getSize() > 0) {
//logger('maps')->debug("Hook postWrite");
if ($node->getType() === FileInfo::TYPE_FILE && $this->isUserNode($node) && $node->getSize()) {
$path = $node->getPath();
if (!$this->lockingProvider->isLocked($path, ILockingProvider::LOCK_SHARED)
and !$this->lockingProvider->isLocked($path, ILockingProvider::LOCK_EXCLUSIVE)
@ -59,6 +60,7 @@ class FileHooks {
$this->root->listen('\OC\Files', 'postWrite', $fileWriteCallback);
$fileDeletionCallback = function (\OCP\Files\Node $node) {
//logger('maps')->debug("Hook preDelete");
if ($this->isUserNode($node)) {
if ($node->getType() === FileInfo::TYPE_FOLDER) {
$this->photofilesService->deleteByFolder($node);
@ -111,6 +113,7 @@ class FileHooks {
}
public function postShare($params) {
//logger('maps')->debug("Hook postShare");
if ($params['itemType'] === 'file') {
//$targetFilePath = $params['itemTarget'];
//$sourceUserId = $params['uidOwner'];
@ -135,6 +138,7 @@ class FileHooks {
}
public function postUnShare($params) {
//logger('maps')->debug("Hook postUnShare");
if ($params['shareType'] === Share::SHARE_TYPE_USER) {
if ($params['itemType'] === 'file') {
$targetUserId = $params['shareWith'];
@ -146,6 +150,7 @@ class FileHooks {
}
public function preUnShare($params) {
//logger('maps')->debug("Hook preUnShare");
if ($params['shareType'] === Share::SHARE_TYPE_USER) {
if ($params['itemType'] === 'folder') {
$targetUserId = $params['shareWith'];
@ -176,7 +181,12 @@ class FileHooks {
}
private function isUserNode(\OCP\Files\Node $node): bool {
return $node->getStorage()->instanceOfStorage(IHomeStorage::class);
//return $node->getStorage()->instanceOfStorage("\OCP\Files\IHomeStorage")
$owner = $node->getStorage()->getOwner('');
if (! $owner) {
return false;
}
return str_starts_with($node->getPath(), '/' . $owner . '/');
}
}

View File

@ -96,14 +96,16 @@ class PhotofilesService {
// add the file for its owner and users that have access
// check if it's already in DB before adding
public function addByFile(Node $file) {
$ownerId = $file->getOwner()->getUID();
if ($this->isPhoto($file)) {
$ownerId = $file->getOwner()->getUID();
$this->addPhoto($file, $ownerId);
// is the file accessible to other users ?
$accesses = $this->shareManager->getAccessList($file);
foreach ($accesses['users'] as $uid) {
if ($uid !== $ownerId) {
$this->addPhoto($file, $uid);
if (array_key_exists('users', $accesses)) {
foreach ($accesses['users'] as $uid) {
if ($uid !== $ownerId) {
$this->addPhoto($file, $uid);
}
}
}
return true;