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
61
62
63
use reqwest::blocking::{multipart::Form, multipart::Part};
use std::{fs::File, io::Read};

mod response;
use response::{Root, ScanRoot};

use crate::{
    utils::{http_get, http_multipart_post, http_post},
    VtClient, VtResult,
};

impl VtClient {
    pub fn file_info(&self, id: &str) -> VtResult<Root> {
        //! Retrieve file scan reports
        //! id: SHA-256, SHA-1 or MD5 identifying the file
        //!
        //! ## Example Usage
        //! ```rust
        //! use vt3::VtClient;
        //!
        //! let vt = VtClient::new("Your API Key");
        //! vt.file_info("44d88612fea8a8f36de82e1278abb02f");
        //! ```
        let url = format!("{}/files/{}", &self.endpoint, id);
        http_get(&self.api_key, &self.user_agent, &url)
    }

    pub fn file_scan(&self, file: &str) -> VtResult<ScanRoot> {
        //! Upload and scan a file
        //!
        //! ## Example Usage
        //! ```rust
        //! use vt3::VtClient;
        //!
        //! let vt = VtClient::new("Your API Key");
        //! println!("{:?}", vt.file_scan("data/eicar.com.txt"));
        //! ```
        let mut f = File::open(file)?;
        let mut buffer = Vec::new();
        {
            f.read_to_end(&mut buffer)?;
        }
        let form_data = Form::new().part("file", Part::bytes(buffer).file_name(file.to_owned()));
        let url = format!("{}/files", &self.endpoint);
        http_multipart_post(&self.api_key, &self.user_agent, &url, form_data)
    }

    pub fn file_rescan(&self, id: &str) -> VtResult<ScanRoot> {
        //! Re-submit/Re-scan already submitted files
        //! id: SHA-256, SHA-1 or MD5 identifying the file
        //!
        //! ## Example Usage
        //! ```rust
        //! use vt3::VtClient;
        //!
        //! let vt = VtClient::new("Your API Key");
        //! vt.file_rescan("44d88612fea8a8f36de82e1278abb02f");
        //! ```
        let url = format!("{}/files/{}/analyse", &self.endpoint, id);
        let form_data = &[("id", id)];
        http_post(&self.api_key, &self.user_agent, &url, form_data)
    }
}