libsubconverter/utils/
file_std.rs1use std::fs;
2use std::io::{self, Read};
3use std::path::Path;
4
5pub fn read_file(path: &str) -> Result<String, io::Error> {
7 let mut file = fs::File::open(path)?;
8 let mut contents = String::new();
9 file.read_to_string(&mut contents)?;
10 Ok(contents)
11}
12
13pub async fn read_file_async(path: &str) -> Result<String, io::Error> {
22 tokio::fs::read_to_string(path).await
23}
24
25pub async fn file_exists(path: &str) -> bool {
27 tokio::fs::metadata(path).await.is_ok()
28}
29
30pub fn file_get<P: AsRef<Path>>(path: P, base_path: Option<&str>) -> io::Result<String> {
40 if let Some(base_path) = base_path {
41 match path.as_ref().to_str() {
42 Some(path_str) => {
43 if !path_str.starts_with(base_path) {
44 return Err(io::Error::new(
45 io::ErrorKind::InvalidInput,
46 "File path is not within the base path",
47 ));
48 }
49 }
50 None => {
51 return Err(io::Error::new(
52 io::ErrorKind::InvalidInput,
53 "File path is not a valid UTF-8 string",
54 ));
55 }
56 }
57 }
58 fs::read_to_string(path)
59}
60
61pub async fn copy_file(src: &str, dst: &str) -> io::Result<()> {
63 if !Path::new(src).exists() {
65 return Err(io::Error::new(
66 io::ErrorKind::NotFound,
67 format!("Source file {} not found", src),
68 ));
69 }
70
71 if let Some(parent) = Path::new(dst).parent() {
73 fs::create_dir_all(parent)?;
74 }
75
76 fs::copy(src, dst)?;
78 Ok(())
79}
80
81pub async fn file_get_async<P: AsRef<Path>>(
91 path: P,
92 base_path: Option<&str>,
93) -> io::Result<String> {
94 if let Some(base_path) = base_path {
95 match path.as_ref().to_str() {
96 Some(path_str) => {
97 if !path_str.starts_with(base_path) {
98 return Err(io::Error::new(
99 io::ErrorKind::InvalidInput,
100 "File path is not within the base path",
101 ));
102 }
103 }
104 None => {
105 return Err(io::Error::new(
106 io::ErrorKind::InvalidInput,
107 "File path is not a valid UTF-8 string",
108 ));
109 }
110 }
111 }
112 tokio::fs::read_to_string(path).await
113}