1extern crate reqwest;
2
3use reqwest::header::USER_AGENT;
4use reqwest::Client;
5use reqwest::Error;
6use reqwest::Response;
7use std::fs::File;
8use std::io::Read;
9use std::path::Path;
10
11pub fn upload(file_path: &str) -> Result<String, Box<dyn std::error::Error>> {
12 let client = Client::new();
13 let path = Path::new(file_path);
14 let basename = match path.file_name().unwrap().to_str() {
15 Some(s) => s,
16 None => {
17 panic!("Failed to determine path");
18 }
19 };
20 let url = &format!("https://transfer.sh/{}", basename);
21 let file = File::open(file_path)?;
22 let res = make_request(client, url, file);
23 return match res {
24 Ok(mut r) => {
25 let mut body = String::new();
26 r.read_to_string(&mut body)?;
27 Ok(body)
28 },
29 Err(e) => Err(e.into()),
30 }
31}
32
33fn make_request(client: Client, url: &str, file: File) -> Result<Response, Error> {
34 client
35 .put(url)
36 .body(file)
37 .header(USER_AGENT, "curl/7.58.0") .send()
39}