diff options
author | Phie <phie@phie.ovh> | 2019-04-02 02:13:01 +0300 |
---|---|---|
committer | Phie <phie@phie.ovh> | 2019-04-02 02:13:01 +0300 |
commit | aaba3699caf1cb8dfb395dfc13720ee029e454de (patch) | |
tree | 6f2a514f21444b1cb150faca744090c1f4d86c86 /lib | |
parent | bf4ed375f3ea6e5efd6668e4c1d3a041dcd9be2f (diff) |
cache is back in a secured way + delete from cache when a note is deleted
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/Controller/NoteController.php | 9 | ||||
-rw-r--r-- | lib/Hooks/FSHooks.php | 61 | ||||
-rw-r--r-- | lib/Misc/CacheManager.php | 25 |
3 files changed, 64 insertions, 31 deletions
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(); |