pub / rememori

Simple file-based bookmarking and notes application
git clone https://src.jayvii.de/pub/rememori.git
Home | Log | Files | Exports | Refs | README | RSS

commit cbe614dcedc90379bd7638939eeeedeba51455cd
parent 0e75d7f68714df972e761171cb3529ef25123132
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date:   Sun, 27 Oct 2024 13:40:21 +0100

fix: first rename, then update content

the previous order would not update file content
if both name/category and content was changed

Diffstat:
Mlib/edit.php | 30+++++++++++++++++-------------
1 file changed, 17 insertions(+), 13 deletions(-)

diff --git a/lib/edit.php b/lib/edit.php @@ -8,7 +8,23 @@ function edit_note( string $content ) { - /* Write note as if it was completely new */ + /* construct new filepath */ + $filepath_t0 = $category . "/" . $filename; + + /* if old and new filepaths differ, the file was likely renamed. + * delete the old one and return immediately */ + if ($filepath_t1 != $filepath_t0) { + $renamed = rename( + $GLOBALS["data_dir"] . "/" . $user . "/" . $filepath_t1, + $GLOBALS["data_dir"] . "/" . $user . "/" . $filepath_t0 + ); + /* if renaming failed, return "false" and exit immediately */ + if ($renamed === false) { + return false; + } + } + + /* Update content of note if necessary^ */ $written = write_note( $user, $category, @@ -21,18 +37,6 @@ function edit_note( return false; } - /* construct new filepath */ - $filepath_t0 = $category . "/" . $filename; - - /* if old and new filepaths differ, the file was likely renamed. - * delete the old one and return immediately */ - if ($filepath_t1 != $filepath_t0) { - return rename( - $GLOBALS["data_dir"] . "/" . $user . "/" . $filepath_t1, - $GLOBALS["data_dir"] . "/" . $user . "/" . $filepath_t0 - ); - } - /* if all went fine, return "true" */ return true;