mtag_cli/utils/
fs.rs

1use std::{
2    fs,
3    path::{Path, PathBuf},
4};
5
6pub fn list_files_in_folder(folder_path: &Path) -> Result<Vec<PathBuf>, std::io::Error> {
7    let mut file_paths = Vec::new();
8    let entries = fs::read_dir(folder_path)?;
9
10    for entry in entries {
11        let entry = entry?;
12        let path = entry.path();
13
14        if path.is_file() {
15            file_paths.push(path.clone());
16        } else if path.is_dir() {
17            let subfolder_files = list_files_in_folder(&path)?;
18            file_paths.extend(subfolder_files);
19        }
20    }
21
22    Ok(file_paths)
23}
24
25pub fn copy_file_with_parents(
26    source_path: &Path,
27    target_path: &Path,
28) -> Result<(), Box<dyn std::error::Error>> {
29    // 创建目标目录的父文件夹(如果不存在)
30    if let Some(parent_dir) = target_path.parent() {
31        fs::create_dir_all(parent_dir)?;
32    }
33
34    // 复制文件(覆盖同名文件)
35    fs::copy(source_path, target_path)?;
36
37    Ok(())
38}
39
40pub fn is_audio_file(file_path: &Path) -> bool {
41    let kind = infer::get_from_path(file_path)
42        .expect("file read successfully")
43        .expect("file type is known");
44    let file_type: &str = kind.mime_type();
45    file_type.split('/').collect::<Vec<&str>>()[0] == "audio"
46}