11
This commit is contained in:
@@ -7,6 +7,7 @@ use serde_json::json;
|
||||
use tauri::{AppHandle, Manager, State};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::commands::fs_util;
|
||||
use crate::material_db::MaterialDb;
|
||||
use crate::material_store::MaterialStore;
|
||||
|
||||
@@ -30,15 +31,6 @@ fn materials_dir(app: &AppHandle) -> Result<PathBuf, String> {
|
||||
Ok(dir)
|
||||
}
|
||||
|
||||
fn is_under_materials_dir(root: &Path, target: &Path) -> bool {
|
||||
if let (Ok(root), Ok(canonical)) = (root.canonicalize(), target.canonicalize()) {
|
||||
return canonical.starts_with(&root);
|
||||
}
|
||||
let root_s = root.to_string_lossy().to_lowercase();
|
||||
let target_s = target.to_string_lossy().to_lowercase();
|
||||
target_s.contains("video-material") && target_s.starts_with(&root_s)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn list_materials(store: State<'_, MaterialStore>) -> Result<Vec<crate::material_db::MaterialRecord>, String> {
|
||||
store.list().await
|
||||
@@ -54,14 +46,14 @@ pub async fn update_material_name(
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn delete_material(
|
||||
app: AppHandle,
|
||||
store: State<'_, MaterialStore>,
|
||||
id: i64,
|
||||
) -> Result<(), String> {
|
||||
let file_path = store.delete(id).await?;
|
||||
if let Some(path) = file_path {
|
||||
delete_material_file_if_allowed(&app, &path).await?;
|
||||
pub async fn delete_material(store: State<'_, MaterialStore>, id: i64) -> Result<(), String> {
|
||||
let record = store
|
||||
.get(id)
|
||||
.await?
|
||||
.ok_or_else(|| "素材不存在".to_string())?;
|
||||
fs_util::remove_allowed_local_media_file(&record.path)?;
|
||||
if !store.delete(id).await? {
|
||||
return Err("素材不存在".into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -128,16 +120,3 @@ async fn copy_to_materials_dir(
|
||||
Ok(dest.to_string_lossy().to_string())
|
||||
}
|
||||
|
||||
async fn delete_material_file_if_allowed(app: &AppHandle, path: &str) -> Result<(), String> {
|
||||
let target = normalize_path(path);
|
||||
let dir = materials_dir(app)?;
|
||||
if !is_under_materials_dir(&dir, &target) {
|
||||
return Err("不允许删除该路径下的文件".into());
|
||||
}
|
||||
if target.is_file() {
|
||||
tokio::fs::remove_file(&target)
|
||||
.await
|
||||
.map_err(|e| format!("删除文件失败: {e}"))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user