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;
10use std::io::copy;
11
12pub fn upload(file_path: &str) -> Result<String, Box<dyn std::error::Error>> {
13 let client = Client::new();
14 let path = Path::new(file_path);
15 let basename = match path.file_name().unwrap().to_str() {
16 Some(s) => s,
17 None => {
18 panic!("Failed to determine path");
19 }
20 };
21 let url = &format!("https://filepush.co/upload/{}", basename);
22 let file = File::open(file_path)?;
23 let res = make_request(client, url, file);
24 return match res {
25 Ok(mut r) => {
26 let mut body = String::new();
27 r.read_to_string(&mut body)?;
28 Ok(body)
29 },
30 Err(e) => Err(e.into()),
31 }
32}
33
34fn make_request(client: Client, url: &str, file: File) -> Result<Response, Error> {
35 client
36 .put(url)
37 .body(file)
38 .header(USER_AGENT, "curl/7.58.0") .send()
40}
41
42pub fn download(url: &str) -> Result<String, Box<dyn std::error::Error>>{
43 let target = url;
44 let mut dest_path = String::new();
45 let mut response = reqwest::get(target)?;
46 let mut dest = {
47 let fname = response
48 .url()
49 .path_segments()
50 .and_then(|segments| segments.last())
51 .and_then(|name| if name.is_empty() { None } else { Some(name) })
52 .unwrap_or("tmp.bin");
53
54 dest_path.push_str(fname);
55 File::create(fname)?
56 };
57 copy(&mut response, &mut dest)?;
58 Ok(dest_path)
59}