ruwebframe 0.1.8

a simple webframe for rust actix-web, based on rudi and rbatis.
Documentation
use crate::rubase::rudto::filedto::FileDto;
use crate::rulog::ru_log;
use std::fs::File;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::{env, fs, io};
use walkdir::WalkDir;

pub fn if_windows() -> bool {
    cfg!(target_os = "windows")
}
pub fn if_linux() -> bool {
    cfg!(target_os = "linux")
}
pub fn if_macos() -> bool {
    cfg!(target_os = "macos")
}

pub fn find_dir(basedir: &str, dir: &str) -> Vec<std::path::PathBuf> {
    WalkDir::new(basedir)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| {
            e.file_type().is_dir()
                && e.path()
                    .file_name()
                    .unwrap()
                    .to_string_lossy()
                    .ends_with(dir)
        })
        .map(|e| e.path().to_path_buf())
        .collect()
}
#[allow(dead_code)]
pub fn find_dirs(basedir: &str, dir: &str) -> Vec<std::path::PathBuf> {
    println!("find_dirs: {}", basedir);
    let dirs = WalkDir::new(basedir)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.file_type().is_dir())
        .map(|e| e.path().to_path_buf())
        .collect();
    dirs
}

pub fn list_all_files(dir: &str) -> Vec<std::path::PathBuf> {
    WalkDir::new(dir)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.file_type().is_file())
        .map(|e| e.path().to_path_buf())
        .collect()
}
pub fn list_rs_files(dir: &str) -> Vec<String> {
    let allfiles = list_all_files(dir);
    let mut files = Vec::new();
    for file in allfiles {
        ru_log::info(file.to_str().unwrap());
        if file.extension().and_then(|s| s.to_str()) == Some("rs") {
            files.push(file.display().to_string());
        }
    }
    files
}
pub fn list_all_files_suffix(dir: &str, suffix: &str) -> Vec<String> {
    let allfiles = list_all_files(dir);
    let mut files = Vec::new();
    for file in allfiles {
        ru_log::info(file.to_str().unwrap());
        if file.extension().and_then(|s| s.to_str()) == Some(suffix) {
            files.push(file.display().to_string());
        }
    }
    files
}

pub fn is_rs(f: std::path::PathBuf) -> bool {
    return f.extension().and_then(|s| s.to_str()) == Some("rs");
}
pub fn check_file_exist(pathf: &String) -> bool {
    // 例如: ./src/rupageui/page/pageresult_init.rs
    Path::new(&pathf).exists() // 检查文件是否存在
}
pub fn read_file(file_name: &str) -> Result<String, io::Error> {
    let file = File::open(file_name); // ? 运算符:出错时直接返回 Err
    let mut contents = String::new();
    let ret = file?.read_to_string(&mut contents); // 同上
    if ret.is_err() {
        return Err(ret.unwrap_err());
    }
    Ok(contents)
}

pub fn write_file(file_name: &str, content: &str) -> Result<String, io::Error> {
    let file = File::create(file_name); // ? 运算符:出错时直接返回 Err
    let contents = String::new();
    let ret = file?.write(content.as_bytes()); // 同上
    if ret.is_err() {
        return Err(ret.unwrap_err());
    }
    Ok(contents)
}

pub fn list_files(dto: &FileDto) -> Vec<String> {
    let allfiles = list_all_files(dto.home_dir.as_str());
    let mut files = Vec::new();
    for file in allfiles {
        ru_log::info(file.to_str().unwrap());
        if file.extension().and_then(|s| s.to_str()) == Some(dto.suffix.as_str()) {
            let fs = file_size_m(&file);
            if fs > dto.size {
                files.push(file.display().to_string());
            }
        }
    }
    files
}
pub fn file_size_m(file: &std::path::PathBuf) -> u64 {
    let metadata = fs::metadata(file.as_path());
    let size = metadata.unwrap().len(); // 返回 u64 类型,单位:字节
    size / 1024 / 1024
}
#[allow(dead_code)]
fn file_size_g(file: &std::path::PathBuf) -> u64 {
    let metadata = fs::metadata(file.as_path());
    let size = metadata.unwrap().len(); // 返回 u64 类型,单位:字节
    size / 1024 / 1024 / 1024
}

pub fn current_dir() -> Result<PathBuf, io::Error> {
    // 获取当前工作目录
    env::current_dir()
}
pub fn list_dir(dto: &FileDto) -> Vec<String> {
    let allfiles = list_all_files(dto.home_dir.as_str());
    let mut files = Vec::new();
    for file in allfiles {
        ru_log::info(file.to_str().unwrap());
        if file.extension().and_then(|s| s.to_str()) == Some(dto.suffix.as_str()) {
            let fs = crate::rubase::rutils::file_utils::file_size_m(&file);
            if fs > dto.size {
                files.push(file.display().to_string());
            }
        }
    }
    files
}