From 480d672296902400ad9a5299c4b083161f6d2784 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Fri, 14 Aug 2026 22:08:46 +0200 Subject: [PATCH] generate a note title in add_file_note (backend) --- application/backend/src/backend.rs | 7 ++++--- application/backend/src/note.rs | 13 +++++++++++++ application/note/src/file.rs | 19 ++++--------------- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/application/backend/src/backend.rs b/application/backend/src/backend.rs index 4ccf080..87feda5 100644 --- a/application/backend/src/backend.rs +++ b/application/backend/src/backend.rs @@ -49,7 +49,7 @@ use entity::note::{Column, Entity as Note, NoteType}; use entity::notecontent::{Entity as NoteContent}; use migration::prelude::Utc; use crate::lockmanager::LockManager; -use crate::note::{create_nodename, nodename_to_contenttype, randomize_nodename}; +use crate::note::{create_nodename, nodename_to_contenttype, nodename_to_title, randomize_nodename}; use crate::notify::{fs_notify, FSWatch}; use crate::storage::{*}; @@ -820,7 +820,7 @@ impl BackendHandle { let result: Result = async { // prepare db insert let nodename = generate_nodename_in_collection(&bhandle.backend.db, collection_id, name.as_str()).await?; - let title = nodename.clone(); // TODO + let title = nodename_to_title(&nodename).to_string(); let contenttype = nodename_to_contenttype(nodename.as_str()).to_string(); let insert = note::ActiveModel { collection_id: Set(collection_id), @@ -971,7 +971,8 @@ impl BackendHandle { if let Some(old_path) = old_path && let Some(new_path) = new_path { let result = tokio::fs::rename(new_path, old_path).await; if let Err(e) = result { - msg.push_str("; moving file back from trash failed"); + msg.push_str("; moving file back from trash failed: "); + msg.push_str(e.to_string().as_str()); } } Err(DbErr::Custom(msg)) diff --git a/application/backend/src/note.rs b/application/backend/src/note.rs index f1db72b..c2e2b56 100644 --- a/application/backend/src/note.rs +++ b/application/backend/src/note.rs @@ -61,6 +61,19 @@ pub fn randomize_nodename(nodename: &str) -> String { } } +pub fn nodename_to_title(s: &str) -> &str { + if !s.trim().is_empty() { + let name = Path::new(s) + .file_stem() + .and_then(|s| s.to_str()) + .unwrap_or(s); + return name; + } + // TODO: the default file name needs to be configured somewhere + "file" +} + + pub fn nodename_to_contenttype(name: &str) -> &'static str { let ext = Path::new(name) .extension() diff --git a/application/note/src/file.rs b/application/note/src/file.rs index f7b3ddb..f933fd0 100644 --- a/application/note/src/file.rs +++ b/application/note/src/file.rs @@ -31,6 +31,7 @@ use std::path::{Path, PathBuf}; use std::rc::Rc; use url::Url; use backend::backend::{BackendHandle, BroadcastMessage, NoteId, NoteTitleUpdate, OpenExternResult, RenameNoteRet}; +use backend::note::nodename_to_title; use ui_rs::{action, doc_cast, ui_actions, UiModel}; use ui_rs::ui::*; use crate::AppStates; @@ -188,7 +189,7 @@ impl FileNote { } pub fn update_title(&mut self, s: &str, notify: bool) { - let title = generate_title(s).unwrap_or("file"); + let title = nodename_to_title(s); if notify { let update = NoteTitleUpdate { @@ -234,9 +235,8 @@ impl NoteViewModel for FileNote { // nothing has changed return; } - - // TODO: "file" is also used in update_title() and needs to be configured somewhere - let new_title = generate_title(&nodename).unwrap_or("file").to_string(); + + let new_title = nodename_to_title(&nodename).to_string(); mem::swap(&mut self.nodename, &mut nodename); @@ -268,14 +268,3 @@ impl NoteViewModel for FileNote { } } -fn generate_title(s: &str) -> Option<&str> { - if !s.trim().is_empty() { - let name = Path::new(s) - .file_stem() - .and_then(|s| s.to_str()) - .unwrap_or(s); - return Some(name); - } - - None -} -- 2.52.0