2020-12-05 12:08:24 +01:00
|
|
|
use anyhow::{bail, Result};
|
|
|
|
use reqwest::{
|
|
|
|
multipart::{Form, Part},
|
|
|
|
Body,
|
|
|
|
Client,
|
|
|
|
};
|
|
|
|
use tokio::{fs::File, io::reader_stream};
|
|
|
|
|
2020-12-05 12:32:01 +01:00
|
|
|
use crate::{
|
|
|
|
api::UpResp,
|
|
|
|
util::{self, open_link},
|
|
|
|
};
|
2020-12-05 12:08:24 +01:00
|
|
|
|
|
|
|
pub async fn run(
|
|
|
|
http: &Client,
|
|
|
|
token: String,
|
|
|
|
path: String,
|
|
|
|
name: String,
|
|
|
|
category: String,
|
|
|
|
open: bool,
|
|
|
|
) -> Result<()> {
|
2020-12-05 12:32:01 +01:00
|
|
|
if !util::cats(http).await?.contains(&category) {
|
2020-12-05 12:08:24 +01:00
|
|
|
bail!(r#"category is invalid. use "cats" to list categories"#);
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = http
|
|
|
|
.post("https://data.tilera.xyz/api/jensmemes/upload")
|
|
|
|
.multipart(
|
|
|
|
Form::new()
|
|
|
|
.text("category", category)
|
|
|
|
.text("token", token)
|
|
|
|
.part(
|
|
|
|
"file",
|
|
|
|
Part::stream(Body::wrap_stream(reader_stream(File::open(path).await?)))
|
|
|
|
.file_name(name),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let status = res.status();
|
|
|
|
let res = serde_json::from_slice::<UpResp>(&res.bytes().await?)?;
|
|
|
|
|
|
|
|
println!("Server responded with code {}", status);
|
2020-12-05 12:32:01 +01:00
|
|
|
|
2020-12-05 12:08:24 +01:00
|
|
|
if !open {
|
|
|
|
println!();
|
|
|
|
}
|
|
|
|
|
|
|
|
for f in res.files {
|
|
|
|
if open {
|
|
|
|
open_link(&f).await?;
|
|
|
|
} else {
|
|
|
|
println!("{}", f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|