get file name in desktop

pull/2421/head
VulnX 5 months ago
parent 6b4c391738
commit 7cca7c841b
No known key found for this signature in database
GPG Key ID: CC61EA8583C4CD8E

@ -22,7 +22,7 @@ use std::{
time::{SystemTime, UNIX_EPOCH}, time::{SystemTime, UNIX_EPOCH},
}; };
use crate::{scope::Entry, Error, SafeFilePath}; use crate::{scope::Entry, Error, FsExt, SafeFilePath};
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub enum CommandError { pub enum CommandError {
@ -1100,6 +1100,14 @@ fn is_forbidden<P: AsRef<Path>>(
} }
} }
#[tauri::command]
pub async fn file_name<R: Runtime>(
webview: Webview<R>,
filepath: SafeFilePath,
) -> Result<String, String> {
webview.fs().file_name(filepath)
}
struct StdFileResource(Mutex<File>); struct StdFileResource(Mutex<File>);
impl StdFileResource { impl StdFileResource {

@ -6,7 +6,7 @@ use std::path::PathBuf;
use tauri::{AppHandle, Runtime}; use tauri::{AppHandle, Runtime};
use crate::{FilePath, OpenOptions}; use crate::{FilePath, OpenOptions, SafeFilePath};
pub struct Fs<R: Runtime>(pub(crate) AppHandle<R>); pub struct Fs<R: Runtime>(pub(crate) AppHandle<R>);
@ -32,4 +32,19 @@ impl<R: Runtime> Fs<R> {
let path = path_or_err(path)?; let path = path_or_err(path)?;
std::fs::OpenOptions::from(opts).open(path) std::fs::OpenOptions::from(opts).open(path)
} }
pub fn file_name(&self, filepath: SafeFilePath) -> Result<String, String> {
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
}
}
} }

@ -418,6 +418,7 @@ pub fn init<R: Runtime>() -> TauriPlugin<R, Option<config::Config>> {
commands::write_text_file, commands::write_text_file,
commands::exists, commands::exists,
commands::size, commands::size,
commands::file_name,
#[cfg(feature = "watch")] #[cfg(feature = "watch")]
watcher::watch, watcher::watch,
#[cfg(feature = "watch")] #[cfg(feature = "watch")]

Loading…
Cancel
Save