]> uap-core.de Git - note.git/commitdiff
add backend function for checking if a nodename is available
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 1 Aug 2026 09:39:03 +0000 (11:39 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 1 Aug 2026 09:39:03 +0000 (11:39 +0200)
application/backend/src/backend.rs
application/backend/src/storage.rs
application/note/src/file.rs

index 2e46450332ddad99c92b9fb320c3325d203e8761..f1344ea1fc683c525e40fed7abcb880e2d37a09e 100644 (file)
@@ -717,6 +717,17 @@ impl BackendHandle {
         let _ = self.tx.send(cmd);
     }
 
+    pub fn check_nodename<F>(&self, collection_id: i32, note_id: i32, nodename: &str, callback: F)
+    where F: FnOnce(Result<bool, DbErr>) + Send + 'static {
+        let bhandle = self.clone();
+        let name = nodename.to_string();
+        let cmd = Box::pin(async move {
+            let result = check_free_nodename(&bhandle.backend.db, collection_id, note_id, name.as_str()).await;
+            callback(result);
+        });
+        let _ = self.tx.send(cmd);
+    }
+
     pub fn get_file_metadata<F>(&self, path: &Path, callback: F)
     where F: FnOnce(std::io::Result<Metadata>) + Send + 'static {
         let p = PathBuf::from(path);
index a91ab75f7b505b58ee8c97ac712baa3afa393335..7ce260a83154a5a2ead580ea12455b0a022efaae 100644 (file)
@@ -136,3 +136,26 @@ pub async fn write_tmp_file(name: &str, data: Vec<u8>) -> std::io::Result<std::p
 
     Ok(path)
 }
+
+pub async fn check_free_nodename(db: &DatabaseConnection, collection_id: i32, note_id: i32, nodename: &str) -> Result<bool, DbErr> {
+    let exists = note::Entity::find()
+            .filter(note::Column::CollectionId.eq(collection_id))
+            .filter(note::Column::Nodename.eq(nodename))
+            .select_only()
+            .column(note::Column::NoteId)
+            .into_tuple::<i32>()
+            .one(db).await?;
+    if exists.is_some() {
+        return Ok(false)
+    }
+
+    let path = get_note_storage_path(db, note_id).await?;
+    if let Some(path) = path {
+        let m = fs::metadata(Path::new(&path)).await;
+        if m.is_ok() {
+            return Ok(false) // file exists
+        }
+    }
+
+    Ok(true)
+}
\ No newline at end of file
index bf357bbd17b0d5677023125acd4a411732266836..559efcefffa4c4d2ff8843524e83061cdc42396f 100644 (file)
@@ -133,7 +133,7 @@ impl FileNote {
                                     let url = Url::from_file_path(&path);
                                     if let Ok(url) = url {
                                         note.webview.load_url(url.as_str());
-                                        // TODO: replace 1 with enum value
+                                        // TODO: replace 2 with enum value
                                         note.preview_tab.set(2);
                                     }
                                 }