diff options
-rw-r--r-- | appinfo/app.php | 16 | ||||
-rwxr-xr-x | lib/Controller/NoteController.php | 9 | ||||
-rw-r--r-- | lib/Hooks/FSHooks.php | 61 | ||||
-rw-r--r-- | lib/Misc/CacheManager.php | 25 |
4 files changed, 76 insertions, 35 deletions
diff --git a/appinfo/app.php b/appinfo/app.php index d1228b2..39b15ca 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -47,15 +47,23 @@ class Application extends App { private function connectWatcher(IAppContainer $container) { /** @var IRootFolder $root */ - /*$root = $container->query(IRootFolder::class); - $root->listen('\OC\Files', 'postWrite', function (Node $node) use ($container) { - $c = $container->query('ServerContainer'); + $root = $container->query(IRootFolder::class); + $root->listen('\OC\Files', 'postWrite', function (Node $node) use ($container) { + $c = $container->query('ServerContainer'); $user = $c->getUserSession()->getUser(); if($user != null){ $watcher = new FSHooks($c->getUserFolder(), $user->getUID(), $c->getConfig(), 'carnet',$container->query(IDBConnection::class)); $watcher->postWrite($node); } - });*/ + }); + $root->listen('\OC\Files', 'postDelete', function (Node $node) use ($container) { + $c = $container->query('ServerContainer'); + $user = $c->getUserSession()->getUser(); + if($user != null){ + $watcher = new FSHooks($c->getUserFolder(), $user->getUID(), $c->getConfig(), 'carnet',$container->query(IDBConnection::class)); + $watcher->postDelete($node); + } + }); } } $app = new Application(); diff --git a/lib/Controller/NoteController.php b/lib/Controller/NoteController.php index dcb8994..ecc24bf 100755 --- a/lib/Controller/NoteController.php +++ b/lib/Controller/NoteController.php @@ -456,19 +456,20 @@ public function getMetadata($paths){ $array = array(); $pathsAr = explode(",",$paths); - $cache = new CacheManager($this->db); + $cache = new CacheManager($this->db, $this->CarnetFolder); $metadataFromCache = $cache->getFromCache($pathsAr); - + foreach($pathsAr as $path){ if(empty($path)) continue; try{ - if(!array_key_exists($path, $metadataFromCache) OR True){ + + if(!array_key_exists($this->CarnetFolder->getFullPath($path), $metadataFromCache) OR True){ $utils = new NoteUtils(); try{ $meta = $utils->getMetadata($this->CarnetFolder, $path); $array[$path] = $meta; - //$cache->addToCache($path, $meta, $meta['lastmodfile']); + $cache->addToCache($path, $meta, $meta['lastmodfile']); } catch(\PhpZip\Exception\ZipException $e){ } diff --git a/lib/Hooks/FSHooks.php b/lib/Hooks/FSHooks.php index 7baa8a0..700c1fc 100644 --- a/lib/Hooks/FSHooks.php +++ b/lib/Hooks/FSHooks.php @@ -30,31 +30,52 @@ class FSHooks { } } - public function postWrite($node) { + public function postDelete($node){ if($this->carnetFolder == null) return; - try{ - if(substr($node->getName(), -3) === "sqd"){ // to avoid getting carnet's path each time a file is writen - //we check if is in our path - - if(substr($node->getPath(), 0, strlen($this->carnetFolder->getPath())) === $this->carnetFolder->getPath()){ - $relativePath = substr($node->getPath(), strlen($this->carnetFolder->getPath())); - if(substr($relativePath, 0, 1) === "/") - $relativePath = substr($relativePath, 1); - /*if(NoteController::$lastWrite === $node->getPath()){ - return; //was already handled in save - }*/ - $cacheManager = new CacheManager($this->db); - $utils = new NoteUtils(); - $metadata = $utils->getMetadata($this->carnetFolder, $relativePath); - $cacheManager->addToCache($relativePath, $metadata, $metadata['lastmodfile']); - } - } - } catch(\PhpZip\Exception\ZipException $e){ + if($this->isMine($node)){ + $cacheManager = new CacheManager($this->db, $this->carnetFolder); + $cacheManager->deleteFromCache($this->getRelativePath($node->getPath())); + } + } + private function isMine($node){ + if(substr($node->getName(), -3) === "sqd"){ // to avoid getting carnet's path each time a file is writen + //we check if is in our path + + if(substr($node->getPath(), 0, strlen($this->carnetFolder->getPath())) === $this->carnetFolder->getPath()){ + return true; + } } + return false; + } + + private function getRelativePath($fullPath){ + $relativePath = substr($fullPath, strlen($this->carnetFolder->getPath())); + if(substr($relativePath, 0, 1) === "/") + $relativePath = substr($relativePath, 1); + return $relativePath; + } + + public function postWrite($node) { + if($this->carnetFolder == null) + return; - + if($this->isMine($node)){ + try{ + + /*if(NoteController::$lastWrite === $node->getPath()){ + return; //was already handled in save + }*/ + $relativePath = $this->getRelativePath($node->getPath()); + $cacheManager = new CacheManager($this->db, $this->carnetFolder); + $utils = new NoteUtils(); + $metadata = $utils->getMetadata($this->carnetFolder, $relativePath); + $cacheManager->addToCache($relativePath, $metadata, $metadata['lastmodfile']); + } catch(\PhpZip\Exception\ZipException $e){ + + } + } } public function postWritePath($node) { diff --git a/lib/Misc/CacheManager.php b/lib/Misc/CacheManager.php index ce79d1b..398bbdb 100644 --- a/lib/Misc/CacheManager.php +++ b/lib/Misc/CacheManager.php @@ -4,15 +4,16 @@ use OCP\IDBConnection; class CacheManager{ private $db; - - public function __construct(IDBConnection $db) { + private $carnetFolder; + public function __construct(IDBConnection $db, $carnetFolder) { $this->db = $db; + $this->carnetFolder = $carnetFolder; } public function addToCache($relativePath, $metadata, $lastmodfile){ $sql = 'INSERT into `*PREFIX*carnet_metadata` VALUES(?, ?, ?)'; $stmt = $this->db->prepare($sql); - $stmt->bindParam(1, $relativePath, \PDO::PARAM_STR); + $stmt->bindParam(1, $this->carnetFolder->getFullPath($relativePath), \PDO::PARAM_STR); if(!is_string($metadata)) $metadata = json_encode($metadata); $stmt->bindParam(2, $metadata, \PDO::PARAM_STR); @@ -24,7 +25,7 @@ class CacheManager{ $sql = 'UPDATE `*PREFIX*carnet_metadata` SET `metadata` = ?, `last_modification_file` = ? WHERE `path` = ?'; $stmt = $this->db->prepare($sql); $stmt->bindParam(1, $metadata, \PDO::PARAM_STR); - $stmt->bindParam(3, $relativePath, \PDO::PARAM_STR); + $stmt->bindParam(3, $this->carnetFolder->getFullPath($relativePath), \PDO::PARAM_STR); $stmt->bindParam(2, $lastmodfile, \PDO::PARAM_INT); $stmt->execute(); @@ -32,13 +33,23 @@ class CacheManager{ } + public function deleteFromCache($relativePath){ + $sql = 'DELETE FROM `*PREFIX*carnet_metadata` WHERE `path` = ?'; + $stmt = $this->db->prepare($sql); + $stmt->bindParam(1, $this->carnetFolder->getFullPath($relativePath), \PDO::PARAM_STR); + $stmt->execute(); + + } + public function getFromCache($arrayRelativePath){ - $sql = 'SELECT * FROM `*PREFIX*carnet_metadata` ' . + $arrayFullPath = array(); + $sql = 'SELECT * FROM `*PREFIX*carnet_metadata` ' . 'WHERE '; for($i = 0; $i < sizeof($arrayRelativePath); $i++){ $sql .= "`path` = ? "; if($i < sizeof($arrayRelativePath)-1) $sql .= "OR "; + array_push($arrayFullPath, $this->carnetFolder->getFullPath($arrayRelativePath[$i])); } $stmt = $this->db->prepare($sql); @@ -47,11 +58,11 @@ class CacheManager{ $i++; }*/ - $stmt->execute($arrayRelativePath); + $stmt->execute($arrayFullPath); $array = array(); $fetched = $stmt->fetchAll(); foreach ($fetched as $row){ - $array[$row['path']] = json_decode($row['metadata']); + $array[substr($row['path'], strlen($this->carnetFolder->getPath())+1)] = json_decode($row['metadata']); } $stmt->closeCursor(); |