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
extern crate reqwest;
use reqwest::header::USER_AGENT;
use reqwest::Client;
use reqwest::Error;
use reqwest::Response;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use std::io::copy;
pub fn upload(file_path: &str) -> Result<String, Box<dyn std::error::Error>> {
let client = Client::new();
let path = Path::new(file_path);
let basename = match path.file_name().unwrap().to_str() {
Some(s) => s,
None => {
panic!("Failed to determine path");
}
};
let url = &format!("https://filepush.co/upload/{}", basename);
let file = File::open(file_path)?;
let res = make_request(client, url, file);
return match res {
Ok(mut r) => {
let mut body = String::new();
r.read_to_string(&mut body)?;
Ok(body)
},
Err(e) => Err(e.into()),
}
}
fn make_request(client: Client, url: &str, file: File) -> Result<Response, Error> {
client
.put(url)
.body(file)
.header(USER_AGENT, "curl/7.58.0")
.send()
}
pub fn download(url: &str) -> Result<String, Box<dyn std::error::Error>>{
let target = url;
let mut dest_path = String::new();
let mut response = reqwest::get(target)?;
let mut dest = {
let fname = response
.url()
.path_segments()
.and_then(|segments| segments.last())
.and_then(|name| if name.is_empty() { None } else { Some(name) })
.unwrap_or("tmp.bin");
dest_path.push_str(fname);
File::create(fname)?
};
copy(&mut response, &mut dest)?;
Ok(dest_path)
}