editer
This commit is contained in:
1 parent
ad987111a3
commit
c9e61f38c5
89 files changed
+11775
-3155
No files matched your search
@@ -0,0 +1,147 @@
|
||||
//! 编辑器「本地目录」命令层:直接操作用户真实文件系统(不进索引、无沙箱)。
|
||||
//!
|
||||
//! # 为什么复用 `terminal::local_fs`
|
||||
//!
|
||||
//! 列举 / 新建目录 / 重命名 / **回收站删除** 在终端模块的「本地栏」已经实现过,
|
||||
//! 且把「目录/文件分流」「不弹系统错误对话框」「拒绝删除盘符根」这些细节处理好了。
|
||||
//! 这里只补它没有的两件事:
|
||||
//! 1. **新建文件**(空文本,终端本地栏只建目录);
|
||||
//! 2. **重名一律报错**——目录树是用户的真实文件系统,静默改名会让人找不到刚建的文件。
|
||||
//!
|
||||
//! # 安全边界(与终端同一姿态)
|
||||
//!
|
||||
//! 路径来自用户自己的浏览/输入,不做「限制在本目录根下」的约束(那是本地文件管理器,
|
||||
//! 限制根只会挡住用户自己)。唯一硬守卫是 **拒绝删除盘符根**(已在
|
||||
//! [`terminal::local_fs::delete`] 内实现),且删除一律走回收站。
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
use serde::Serialize;
|
||||
use specta::Type;
|
||||
|
||||
use super::library::validate_name;
|
||||
use crate::terminal::local_fs;
|
||||
|
||||
/// 批量删除结果(回传前端展示「成功 N 项、失败明细」)。
|
||||
#[derive(Debug, Clone, Serialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DeleteReport {
|
||||
/// 成功移入回收站的项数
|
||||
pub removed: u32,
|
||||
/// 失败明细(路径 + 原因)
|
||||
pub failed: Vec<String>,
|
||||
}
|
||||
|
||||
/// 列举本地目录(`path` 为空 = 用户主目录)。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn editor_read_dir(path: String) -> Result<local_fs::LocalDir, String> {
|
||||
local_fs::list_dir(&path)
|
||||
}
|
||||
|
||||
/// 在 `dir` 下新建一个空文本文件。重名报错(不自动加序号)。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn editor_create_file(dir: String, name: String) -> Result<String, String> {
|
||||
let target = resolve_child(&dir, &name)?;
|
||||
if target.exists() {
|
||||
return Err(format!("「{name}」已存在"));
|
||||
}
|
||||
std::fs::write(&target, b"").map_err(|e| format!("新建文件失败: {e}"))?;
|
||||
Ok(local_fs::display_path(&target))
|
||||
}
|
||||
|
||||
/// 在 `dir` 下新建一个文件夹。重名报错(不自动加序号)。
|
||||
///
|
||||
/// 命名用 `editor_create_dir` 而不是 `editor_create_folder`:后者已被**库内**建文件夹
|
||||
/// 的命令占用(`editor_create_folder(dir_rel, name)`,走库根沙箱)—— 两者操作对象不同
|
||||
/// (真实文件系统 vs 库内),必须能同时在命令表里存在。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn editor_create_dir(dir: String, name: String) -> Result<String, String> {
|
||||
let target = resolve_child(&dir, &name)?;
|
||||
if target.exists() {
|
||||
return Err(format!("「{name}」已存在"));
|
||||
}
|
||||
local_fs::create_dir(&target.to_string_lossy())?;
|
||||
Ok(local_fs::display_path(&target))
|
||||
}
|
||||
|
||||
/// 同目录内重命名。目标存在则报错。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn editor_rename_path(path: String, new_name: String) -> Result<String, String> {
|
||||
let src = Path::new(path.trim());
|
||||
if !src.exists() {
|
||||
return Err("路径不存在".to_string());
|
||||
}
|
||||
let parent = src.parent().ok_or_else(|| "无法解析父目录".to_string())?;
|
||||
let name = validate_name(&new_name)?;
|
||||
let target = parent.join(&name);
|
||||
if target == src {
|
||||
return Ok(local_fs::display_path(src));
|
||||
}
|
||||
if target.exists() {
|
||||
return Err(format!("「{name}」已存在"));
|
||||
}
|
||||
local_fs::rename(&path, &target.to_string_lossy())?;
|
||||
Ok(local_fs::display_path(&target))
|
||||
}
|
||||
|
||||
/// 删除若干本地项(**走系统回收站**,不是永久删除)。
|
||||
///
|
||||
/// 单个失败不中断其余项:用户选中的 10 个文件里有一个正被占用,另外 9 个仍应删掉。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn editor_delete_path(paths: Vec<String>) -> Result<DeleteReport, String> {
|
||||
tauri::async_runtime::spawn_blocking(move || {
|
||||
let (removed, failed) = local_fs::delete(&paths);
|
||||
DeleteReport {
|
||||
removed: removed as u32,
|
||||
failed,
|
||||
}
|
||||
})
|
||||
.await
|
||||
.map_err(|e| format!("删除任务失败: {e}"))
|
||||
}
|
||||
|
||||
/// 把一个文件 / 文件夹移动到目标目录下(同盘移动;重名报错)。
|
||||
///
|
||||
/// 不允许把文件夹移动到自身或其子目录下 —— 那会把目录搬进自己里面,属数据事故。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn editor_move_path(path: String, dest_dir: String) -> Result<String, String> {
|
||||
let src = Path::new(path.trim());
|
||||
if !src.exists() {
|
||||
return Err("源路径不存在".to_string());
|
||||
}
|
||||
let dest = Path::new(dest_dir.trim());
|
||||
if !dest.is_dir() {
|
||||
return Err("目标目录不存在".to_string());
|
||||
}
|
||||
let name = src
|
||||
.file_name()
|
||||
.ok_or_else(|| "无法解析文件名".to_string())?;
|
||||
let target = dest.join(name);
|
||||
if target == src {
|
||||
return Ok(local_fs::display_path(src));
|
||||
}
|
||||
if target.exists() {
|
||||
return Err(format!("目标目录中已存在「{}」", name.to_string_lossy()));
|
||||
}
|
||||
if src.is_dir() && dest.starts_with(src) {
|
||||
return Err("不能把文件夹移动到自身或子目录中".to_string());
|
||||
}
|
||||
local_fs::rename(&path, &target.to_string_lossy())?;
|
||||
Ok(local_fs::display_path(&target))
|
||||
}
|
||||
|
||||
/// 校验名字并把「目录 + 名字」拼成绝对路径。
|
||||
fn resolve_child(dir: &str, name: &str) -> Result<std::path::PathBuf, String> {
|
||||
let name = validate_name(name)?;
|
||||
let dir = Path::new(dir.trim());
|
||||
if !dir.is_dir() {
|
||||
return Err("目标目录不存在".to_string());
|
||||
}
|
||||
Ok(dir.join(name))
|
||||
}
|
||||
Reference in new issue
Block a user