diff options
author | Phie <phie@phie.ovh> | 2019-04-09 03:37:46 +0300 |
---|---|---|
committer | Phie <phie@phie.ovh> | 2019-04-09 03:37:46 +0300 |
commit | 967afaea02dae3f6f2dce15a745098ea91735eff (patch) | |
tree | 5dc6484c454b1c865e2b2786cc5e4638af4464e2 | |
parent | 8081be9f755d6f356fe74f15469db763f0edd56d (diff) |
faster save by writing cache immediately on save
-rwxr-xr-x | lib/Controller/NoteController.php | 27 | ||||
-rw-r--r-- | lib/Hooks/FSHooks.php | 5 | ||||
-rw-r--r-- | lib/Misc/NoteUtils.php | 5 |
3 files changed, 28 insertions, 9 deletions
diff --git a/lib/Controller/NoteController.php b/lib/Controller/NoteController.php index 615422b..e5feea7 100755 --- a/lib/Controller/NoteController.php +++ b/lib/Controller/NoteController.php @@ -713,8 +713,12 @@ $cache = $this->getCacheFolder(); $folder = $cache->get("currentnote".$id); $zipFile = new MyZipFile(); - $this->addFolderContentToArchive($folder,$zipFile,""); - //self::$lastWrite = $this->CarnetFolder->getPath()."/".$path; //to avoid FSHooks rewrite of metadata + $meta = array(); + $meta["previews"] = array(); + $previews = $this->addFolderContentToArchive($folder,$zipFile,""); + foreach($previews as $preview){ + array_push($meta['previews'], "./note/getmedia?note=".$path."&media=".$preview); + } $file = $this->CarnetFolder->newFile($path); //tried to do with a direct fopen on $file but lead to bad size on nextcloud $tmppath = tempnam(sys_get_temp_dir(), uniqid().".sqd"); @@ -725,28 +729,39 @@ $this->CarnetFolder->get($path)->delete(); } catch(\OCP\Files\NotFoundException $e) { } - //self::$lastWrite = $file->getPath(); - + $file->putContent($tmph); fclose($tmph); + $meta['metadata'] = $folder->get("metadata.json")->getContent(); + $meta['shorttext'] = NoteUtils::getShortTextFromHTML($folder->get("index.html")->getContent()); + $cache = new CacheManager($this->db, $this->CarnetFolder); + $cache->addToCache($path, $meta, $file->getFileInfo()->getMtime()); + } else throw new Exception('Unable to create Zip'); unlink($tmppath); } - + /* + returns previews + */ private function addFolderContentToArchive($folder, $archive, $relativePath){ + $previews = array(); foreach($folder->getDirectoryListing() as $in){ $inf = $in->getFileInfo(); $path = $relativePath.$inf->getName(); if($inf->getType() === "dir"){ $archive->addEmptyDir($path); - $this->addFolderContentToArchive($in, $archive, $path."/"); + $previews = array_merge($previews, $this->addFolderContentToArchive($in, $archive, $path."/")); }else { $archive->addFromStream($in->fopen("r"), $path, \PhpZip\ZipFile::METHOD_DEFLATED); + if(substr($path,0,strlen("data/preview_")) === "data/preview_"){ + array_push($previews, $path); + } } } + return $previews; } private function getCurrentnoteDir(){ diff --git a/lib/Hooks/FSHooks.php b/lib/Hooks/FSHooks.php index 700c1fc..a07f2ee 100644 --- a/lib/Hooks/FSHooks.php +++ b/lib/Hooks/FSHooks.php @@ -58,9 +58,10 @@ class FSHooks { } public function postWrite($node) { - if($this->carnetFolder == null) + if($this->carnetFolder == null || substr($_SERVER['REQUEST_URI'], -strlen('carnet/note/saveText')) === 'carnet/note/saveText') + { //cache is handled on save return; - + } if($this->isMine($node)){ try{ diff --git a/lib/Misc/NoteUtils.php b/lib/Misc/NoteUtils.php index 7f191bb..b3b0929 100644 --- a/lib/Misc/NoteUtils.php +++ b/lib/Misc/NoteUtils.php @@ -2,6 +2,9 @@ namespace OCA\Carnet\Misc; class NoteUtils{ public static $defaultCarnetNotePath = "Documents/QuickNote"; + public static function getShortTextFromHTML($html){ + return mb_substr(trim(preg_replace('#<[^>]+>#', ' ', $html)),0, 150); + } public function getMetadata($carnetFolder, $path){ $meta = array(); $tmppath = tempnam(sys_get_temp_dir(), uniqid().".zip"); @@ -18,7 +21,7 @@ class NoteUtils{ } try{ - $meta['shorttext'] = mb_substr(trim(preg_replace('#<[^>]+>#', ' ', $zipFile->getEntryContents("index.html"))),0, 150); + $meta['shorttext'] = self::getShortTextFromHTML($zipFile->getEntryContents("index.html")); $i=0; try{ foreach($zipFile->getListFiles() as $f){ |