From: Olaf Wintermann Date: Tue, 4 Aug 2026 18:51:50 +0000 (+0200) Subject: use content_type to select the file note preview type X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;ds=sidebyside;p=note.git use content_type to select the file note preview type --- diff --git a/application/note/src/file.rs b/application/note/src/file.rs index e64a3f0..5dbcd4d 100644 --- a/application/note/src/file.rs +++ b/application/note/src/file.rs @@ -25,7 +25,7 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ -use std::ffi::{OsStr}; + use std::mem; use std::path::{Path, PathBuf}; use std::rc::Rc; @@ -35,7 +35,7 @@ use ui_rs::{action, doc_cast, ui_actions, UiModel}; use ui_rs::ui::*; use crate::AppStates; use crate::note::NoteViewModel; -use crate::window::NoteTypeTabView; +use crate::window::{FileNotePreviewTabView, NoteTypeTabView}; #[derive(UiModel)] pub struct FileNote { @@ -46,6 +46,7 @@ pub struct FileNote { pub id: NoteId, pub nodename: String, + pub content_type: String, pub local_path: Option, pub is_tmp_file: bool, @@ -78,6 +79,7 @@ impl FileNote { id, nodename: String::default(), + content_type: String::default(), local_path: None, is_tmp_file: false, is_opening: false, @@ -94,6 +96,7 @@ impl FileNote { let doc: UiDoc = UiDoc::new_from_box2(Box::new(self), |n,d| { n.doc = d.doc_ref(); n.nodename = model.nodename.clone(); + n.content_type = model.content_type.clone(); n.note_type.set(NoteTypeTabView::File as i64); n.note_nodename.set(model.nodename.as_str()); @@ -103,6 +106,31 @@ impl FileNote { doc_cast!(doc, dyn NoteViewModel) } + fn set_preview(&mut self, path: &Path) { + const WEBVIEW_TYPES: &[&str] = &[ + "text/html", + "application/pdf", + ]; + + if self.content_type.starts_with("image/") { + let result = self.image.load_image_file(path); + if result.is_ok() { + self.preview_tab.set(FileNotePreviewTabView::ImageViewer as i64); + return + } + self.preview_tab.set(crate::window::FileNotePreviewTabView::ImageViewer as i64); + } else if WEBVIEW_TYPES.contains(&self.content_type.as_str()) { + let url = Url::from_file_path(&path); + if let Ok(url) = url { + self.webview.load_url(url.as_str()); + self.preview_tab.set(crate::window::FileNotePreviewTabView::WebView as i64); + return + } + } + + self.preview_tab.set(crate::window::FileNotePreviewTabView::Empty as i64); + } + fn query_local_path(&mut self) { let Some(doc) = self.doc.get_doc() else { return; @@ -119,27 +147,7 @@ impl FileNote { note.is_opening = false; match result { OpenExternResult::Ok((path, istmp)) => { - // TODO: implement better file type detection - // idealy save content type in the note - match path.extension().and_then(OsStr::to_str) { - Some("png" | "jpeg" | "jpg" | "webp") => { - let result = note.image.load_image_file(path.as_path()); - if result.is_ok() { - // TODO: replace 1 with enum value - note.preview_tab.set(1); - } - }, - Some("html" | "htm" | "pdf") => { - let url = Url::from_file_path(&path); - if let Ok(url) = url { - note.webview.load_url(url.as_str()); - // TODO: replace 2 with enum value - note.preview_tab.set(2); - } - } - _ => {} - } - + note.set_preview(&path); note.local_path = Some(path); note.is_tmp_file = istmp; } diff --git a/application/note/src/note.rs b/application/note/src/note.rs index c0ed8b2..c8af18c 100644 --- a/application/note/src/note.rs +++ b/application/note/src/note.rs @@ -29,7 +29,7 @@ use std::path::PathBuf; use std::rc::Rc; use std::sync::atomic::{AtomicU64, Ordering}; use std::time::SystemTime; -use sea_orm::{Iden, NotSet, Set}; +use sea_orm::{NotSet, Set}; use sea_orm::sea_query::prelude::Utc; use backend::backend::{BackendHandle, BroadcastMessage, NoteContentRet, NoteId, NoteTitleUpdate, SaveNoteResult}; use backend::lockmanager::NoteLock; diff --git a/application/note/src/window.rs b/application/note/src/window.rs index 2ca11a5..44c824c 100644 --- a/application/note/src/window.rs +++ b/application/note/src/window.rs @@ -329,6 +329,13 @@ pub enum NoteTypeTabView { File = 2, } +#[repr(i64)] +pub enum FileNotePreviewTabView { + Empty = 0, + ImageViewer = 1, + WebView = 2 +} + #[derive(Default)] pub struct Navigation { history: Vec,