From 878096d4a361fc24370d730b95d60f87c4cd3ee3 Mon Sep 17 00:00:00 2001 From: Phie Date: Wed, 31 Jul 2024 17:39:26 +0200 Subject: [PATCH] metadata save for folder --- appinfo/app.php | 2 +- lib/Controller/NoteController.php | 46 +++++++++++++++++++------------ lib/Misc/NoteUtils.php | 5 ++++ 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/appinfo/app.php b/appinfo/app.php index 6fcd89f..b2916a5 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -84,6 +84,6 @@ function () use ($container, $appName) { 'name' => 'Carnet' ]; } -); + ); ?> diff --git a/lib/Controller/NoteController.php b/lib/Controller/NoteController.php index 4431616..96dae51 100755 --- a/lib/Controller/NoteController.php +++ b/lib/Controller/NoteController.php @@ -554,29 +554,39 @@ public function createNote(){ * @NoCSRFRequired */ public function updateMetadata($path, $metadata){ - - $tmppath = tempnam(sys_get_temp_dir(), uniqid().".zip"); - $tmppath2 = tempnam(sys_get_temp_dir(), uniqid().".zip"); - if(!file_put_contents($tmppath, $this->CarnetFolder->get($path)->fopen("r"))) - return; - $zipFile = new \PhpZip\ZipFile(); - $zipFile->openFromStream(fopen($tmppath, "r")); //issue with encryption when open directly + unexpectedly faster to copy before Oo' - $zipFile->addFromString("metadata.json", $metadata, \PhpZip\ZipFile::METHOD_DEFLATED); - $zipFile->saveAsFile($tmppath2); - $tmph = fopen($tmppath2, "r"); - if($tmph){ + + if(NoteUtils::isFolderNote($path, $this->CarnetFolder)){ try{ - $file = $this->CarnetFolder->get($path); - $file->putContent($tmph); + $noteNode = $this->CarnetFolder->get($path); + $file = $noteNode->get("metadata.json"); + $file->putContent($metadata); + } catch(\OCP\Files\NotFoundException $e) { } - fclose($tmph); - } else - throw new Exception('Unable to create Zip'); - unlink($tmppath); - unlink($tmppath2); + } else { + $tmppath = tempnam(sys_get_temp_dir(), uniqid().".zip"); + $tmppath2 = tempnam(sys_get_temp_dir(), uniqid().".zip"); + if(!file_put_contents($tmppath, $this->CarnetFolder->get($path)->fopen("r"))) + return; + $zipFile = new \PhpZip\ZipFile(); + $zipFile->openFromStream(fopen($tmppath, "r")); //issue with encryption when open directly + unexpectedly faster to copy before Oo' + $zipFile->addFromString("metadata.json", $metadata, \PhpZip\ZipFile::METHOD_DEFLATED); + $zipFile->saveAsFile($tmppath2); + $tmph = fopen($tmppath2, "r"); + if($tmph){ + try{ + $file = $this->CarnetFolder->get($path); + $file->putContent($tmph); + } catch(\OCP\Files\NotFoundException $e) { + } + fclose($tmph); + } else + throw new Exception('Unable to create Zip'); + unlink($tmppath); + unlink($tmppath2); + } } diff --git a/lib/Misc/NoteUtils.php b/lib/Misc/NoteUtils.php index 1fd8e08..8aeffcd 100644 --- a/lib/Misc/NoteUtils.php +++ b/lib/Misc/NoteUtils.php @@ -33,6 +33,11 @@ public static function getShortText($text){ //not optimized at alllll.... return mb_substr($text, 0, 150); } + public static function isFolderNote($path, $carnetFolder){ + $node = $carnetFolder->get($path); + return $node->getType() === "dir" && self::isNote($path); + } + public static function isNote($path){ return self::endsWith($path, ".sqd"); }