mysql_async/local_infile_handler/
builtin.rs
1use futures_util::FutureExt;
10use tokio::fs::File;
11use tokio_util::io::ReaderStream;
12
13use std::{
14 collections::HashSet,
15 path::{Path, PathBuf},
16};
17
18use crate::{
19 error::LocalInfileError,
20 local_infile_handler::{BoxFuture, GlobalHandler},
21};
22
23#[derive(Clone, Debug)]
37pub struct WhiteListFsHandler {
38 white_list: HashSet<PathBuf>,
39}
40
41impl WhiteListFsHandler {
42 pub fn new<A, B>(white_list: B) -> WhiteListFsHandler
43 where
44 A: Into<PathBuf>,
45 B: IntoIterator<Item = A>,
46 {
47 let mut white_list_set = HashSet::new();
48 for path in white_list.into_iter() {
49 white_list_set.insert(Into::<PathBuf>::into(path));
50 }
51 WhiteListFsHandler {
52 white_list: white_list_set,
53 }
54 }
55}
56
57impl GlobalHandler for WhiteListFsHandler {
58 fn handle(&self, file_name: &[u8]) -> BoxFuture<'static, super::InfileData> {
59 let path = String::from_utf8_lossy(file_name);
60 let path = self
61 .white_list
62 .get(Path::new(&*path))
63 .cloned()
64 .ok_or_else(|| LocalInfileError::PathIsNotInTheWhiteList(path.into_owned()));
65 async move {
66 let file = File::open(path?).await?;
67 Ok(Box::pin(ReaderStream::new(file)) as super::InfileData)
68 }
69 .boxed()
70 }
71}