diff --git a/plugins/fs/src/commands.rs b/plugins/fs/src/commands.rs index 29d1104f..403dc602 100644 --- a/plugins/fs/src/commands.rs +++ b/plugins/fs/src/commands.rs @@ -22,7 +22,7 @@ use std::{ time::{SystemTime, UNIX_EPOCH}, }; -use crate::{scope::Entry, Error, SafeFilePath}; +use crate::{scope::Entry, Error, FsExt, SafeFilePath}; #[derive(Debug, thiserror::Error)] pub enum CommandError { @@ -1100,6 +1100,14 @@ fn is_forbidden>( } } +#[tauri::command] +pub async fn file_name( + webview: Webview, + filepath: SafeFilePath, +) -> Result { + webview.fs().file_name(filepath) +} + struct StdFileResource(Mutex); impl StdFileResource { diff --git a/plugins/fs/src/desktop.rs b/plugins/fs/src/desktop.rs index 477c0537..73a5765b 100644 --- a/plugins/fs/src/desktop.rs +++ b/plugins/fs/src/desktop.rs @@ -6,7 +6,7 @@ use std::path::PathBuf; use tauri::{AppHandle, Runtime}; -use crate::{FilePath, OpenOptions}; +use crate::{FilePath, OpenOptions, SafeFilePath}; pub struct Fs(pub(crate) AppHandle); @@ -32,4 +32,19 @@ impl Fs { let path = path_or_err(path)?; std::fs::OpenOptions::from(opts).open(path) } + + pub fn file_name(&self, filepath: SafeFilePath) -> Result { + match filepath { + SafeFilePath::Path(_) => { + let Some(filepath) = filepath.as_path() else { + return Err("failed to obtain filepath".into()); + }; + let Some(file_name) = filepath.file_name() else { + return Err("failed to get file_name from filepath".into()); + }; + Ok(file_name.to_string_lossy().to_string()) + } + SafeFilePath::Url(_) => unreachable!(), // We do not obtain a URL path in desktop + } + } } diff --git a/plugins/fs/src/lib.rs b/plugins/fs/src/lib.rs index 731c7040..4dc08c25 100644 --- a/plugins/fs/src/lib.rs +++ b/plugins/fs/src/lib.rs @@ -418,6 +418,7 @@ pub fn init() -> TauriPlugin> { commands::write_text_file, commands::exists, commands::size, + commands::file_name, #[cfg(feature = "watch")] watcher::watch, #[cfg(feature = "watch")]