11
This commit is contained in:
@@ -169,16 +169,11 @@ impl AvatarDb {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 删除记录并返回文件路径(供调用方删除磁盘文件)
|
||||
pub fn delete(&self, id: i64) -> Result<Option<String>, AvatarDbError> {
|
||||
let row = self.get(id)?;
|
||||
let Some(record) = row else {
|
||||
return Ok(None);
|
||||
};
|
||||
pub fn delete(&self, id: i64) -> Result<bool, AvatarDbError> {
|
||||
let conn = self.conn.lock().map_err(|_| {
|
||||
AvatarDbError::Message("形象数据库锁异常".into())
|
||||
})?;
|
||||
conn.execute("DELETE FROM avatars WHERE id = ?1", params![id])?;
|
||||
Ok(Some(record.file_path))
|
||||
let n = conn.execute("DELETE FROM avatars WHERE id = ?1", params![id])?;
|
||||
Ok(n > 0)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,7 +68,12 @@ impl AvatarStore {
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete(&self, id: i64) -> Result<Option<String>, String> {
|
||||
pub async fn get(&self, id: i64) -> Result<Option<AvatarRecord>, String> {
|
||||
self.with_db(move |db| db.get(id).map_err(|e| e.to_string()))
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete(&self, id: i64) -> Result<bool, String> {
|
||||
self.with_db(move |db| db.delete(id).map_err(|e| e.to_string()))
|
||||
.await
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ use uuid::Uuid;
|
||||
|
||||
use crate::avatar_db::AvatarRecord;
|
||||
use crate::avatar_store::AvatarStore;
|
||||
use crate::commands::fs_util;
|
||||
|
||||
fn normalize_path(path: &str) -> PathBuf {
|
||||
Path::new(path)
|
||||
@@ -24,15 +25,6 @@ fn avatars_dir(app: &AppHandle) -> Result<PathBuf, String> {
|
||||
Ok(dir)
|
||||
}
|
||||
|
||||
fn is_under_avatars_dir(avatars_root: &Path, target: &Path) -> bool {
|
||||
if let (Ok(root), Ok(canonical)) = (avatars_root.canonicalize(), target.canonicalize()) {
|
||||
return canonical.starts_with(&root);
|
||||
}
|
||||
let root_s = avatars_root.to_string_lossy().to_lowercase();
|
||||
let target_s = target.to_string_lossy().to_lowercase();
|
||||
target_s.contains("avatars") && target_s.starts_with(&root_s)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn list_avatars(store: State<'_, AvatarStore>) -> Result<Vec<AvatarRecord>, String> {
|
||||
store.list().await
|
||||
@@ -65,16 +57,16 @@ pub async fn avatar_name_exists(
|
||||
store.name_exists(&name, exclude_id).await
|
||||
}
|
||||
|
||||
/// 删除数据库记录并删除应用目录内的视频文件
|
||||
/// 删除应用目录内的视频文件及数据库记录
|
||||
#[tauri::command]
|
||||
pub async fn delete_avatar(
|
||||
app: AppHandle,
|
||||
store: State<'_, AvatarStore>,
|
||||
id: i64,
|
||||
) -> Result<(), String> {
|
||||
let file_path = store.delete(id).await?;
|
||||
if let Some(path) = file_path {
|
||||
delete_avatar_file_if_allowed(&app, &path).await?;
|
||||
pub async fn delete_avatar(store: State<'_, AvatarStore>, id: i64) -> Result<(), String> {
|
||||
let record = store
|
||||
.get(id)
|
||||
.await?
|
||||
.ok_or_else(|| "形象不存在".to_string())?;
|
||||
fs_util::remove_allowed_local_media_file(&record.file_path)?;
|
||||
if !store.delete(id).await? {
|
||||
return Err("形象不存在".into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -115,16 +107,3 @@ pub async fn import_avatar_video(
|
||||
Ok(dest.to_string_lossy().to_string())
|
||||
}
|
||||
|
||||
async fn delete_avatar_file_if_allowed(app: &AppHandle, path: &str) -> Result<(), String> {
|
||||
let target = normalize_path(path);
|
||||
let dir = avatars_dir(app)?;
|
||||
if !is_under_avatars_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