feat(tauri): 添加文件修改时间查询命令

- 新增 get_file_mtime 命令
- 支持检测文件外部修改
This commit is contained in:
yhh
2025-12-16 11:51:58 +08:00
parent 0bcb675c3b
commit 9d3eeb1980
3 changed files with 36 additions and 3 deletions

View File

@@ -254,6 +254,25 @@ pub fn read_file_as_base64(file_path: String) -> Result<String, String> {
Ok(general_purpose::STANDARD.encode(&file_content))
}
/// Get file modification time (milliseconds since UNIX epoch)
/// 获取文件修改时间Unix 纪元以来的毫秒数)
#[tauri::command]
pub fn get_file_mtime(path: String) -> Result<u64, String> {
let metadata = fs::metadata(&path)
.map_err(|e| format!("Failed to get metadata for {}: {}", path, e))?;
let modified = metadata
.modified()
.map_err(|e| format!("Failed to get modified time for {}: {}", path, e))?;
let millis = modified
.duration_since(std::time::UNIX_EPOCH)
.map_err(|e| format!("Time error: {}", e))?
.as_millis() as u64;
Ok(millis)
}
/// Copy file from source to destination
#[tauri::command]
pub fn copy_file(src: String, dst: String) -> Result<(), String> {

View File

@@ -65,6 +65,7 @@ fn main() {
commands::scan_directory,
commands::read_file_as_base64,
commands::copy_file,
commands::get_file_mtime,
// Dialog operations
commands::open_folder_dialog,
commands::open_file_dialog,
@@ -183,18 +184,27 @@ fn handle_project_protocol(
}
/// Get MIME type based on file extension
/// 根据文件扩展名获取 MIME 类型
fn get_mime_type(file_path: &str) -> &'static str {
if file_path.ends_with(".ts") || file_path.ends_with(".tsx") {
"application/javascript"
} else if file_path.ends_with(".js") {
} else if file_path.ends_with(".js") || file_path.ends_with(".mjs") {
"application/javascript"
} else if file_path.ends_with(".json") {
"application/json"
} else if file_path.ends_with(".wasm") {
"application/wasm"
} else if file_path.ends_with(".css") {
"text/css"
} else if file_path.ends_with(".html") {
"text/html"
} else if file_path.ends_with(".png") {
"image/png"
} else if file_path.ends_with(".jpg") || file_path.ends_with(".jpeg") {
"image/jpeg"
} else if file_path.ends_with(".svg") {
"image/svg+xml"
} else {
"text/plain"
"application/octet-stream"
}
}

View File

@@ -81,7 +81,8 @@
{
"identifier": "main",
"windows": [
"main"
"main",
"frame-debugger"
],
"permissions": [
"core:default",
@@ -91,6 +92,9 @@
"core:window:allow-toggle-maximize",
"core:window:allow-close",
"core:window:allow-is-maximized",
"core:window:allow-create",
"core:webview:allow-create-webview",
"core:webview:allow-create-webview-window",
"shell:default",
"dialog:default",
"updater:default",