1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use crate::{db::models::ScanEvent, settings::target::TargetProcess};
use reqwest::header;
use serde::Deserialize;
use tracing::error;

#[derive(Deserialize, Clone)]
pub struct FileFlows {
    /// URL to the FileFlows server
    pub url: String,
}

impl FileFlows {
    fn get_client(&self) -> anyhow::Result<reqwest::Client> {
        let headers = header::HeaderMap::new();

        reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(10))
            .default_headers(headers)
            .build()
            .map_err(Into::into)
    }

    async fn scan(&self, ev: &ScanEvent) -> anyhow::Result<()> {
        let client = self.get_client()?;

        let mut url = url::Url::parse(&self.url)?.join("/api/library-file/process-file")?;

        url.query_pairs_mut().append_pair("filename", &ev.file_path);

        let res = client.post(url.to_string()).send().await?;

        if res.status().is_success() {
            Ok(())
        } else {
            let body = res.text().await?;
            Err(anyhow::anyhow!("unable to send scan: {}", body))
        }
    }
}

impl TargetProcess for FileFlows {
    async fn process<'a>(&self, evs: &[&'a ScanEvent]) -> anyhow::Result<Vec<String>> {
        let mut succeeded = Vec::new();

        for ev in evs {
            let res = self.scan(ev).await;

            match res {
                Ok(_) => {
                    succeeded.push(ev.file_path.clone());
                }
                Err(e) => {
                    error!("failed to process '{}': {:?}", ev.file_path, e);
                }
            }
        }

        Ok(succeeded)
    }
}