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
use dotenv::dotenv;
use reqwest::{blocking::multipart, blocking::Client, blocking::Response, Result};
use std::env;

pub fn upload(filepath: &str) -> Result<Response> {
    dotenv().ok();
    let username = env::var("STREAMABLE_USERNAME").unwrap();
    let password = env::var("STREAMABLE_PASSWORD").unwrap();

    let endpoint = "https://api.streamable.com/upload";

    let form = multipart::Form::new()
        .file("file", std::path::Path::new(filepath))
        .unwrap_or_else(|e| panic!("Error: {}", e));

    let client = Client::new();
    let resp = client
        .post(endpoint)
        .basic_auth(username, Some(password))
        .multipart(form)
        .send()
        .unwrap_or_else(|e| panic!("Error: {}", e));

    // dbg!(&resp);

    Ok(resp)
}

#[cfg(test)]
mod tests {
    use crate::upload;

    #[test]
    fn streamable_upload() {
        let result = upload("./media/sample.mp4");
        dbg!(&result);

        match result {
            Ok(v) => println!("{:?}", v),
            Err(e) => panic!("Error: {}", e),
        }
    }
}