2021-04-01 19:12:23 +02:00
|
|
|
use crate::util::open_link;
|
2021-01-05 15:13:19 +01:00
|
|
|
use anyhow::Result;
|
2021-04-01 19:12:23 +02:00
|
|
|
use jm_client_core::{api::UpResp, util};
|
2020-12-06 14:57:14 +01:00
|
|
|
use log::info;
|
2020-12-05 12:08:24 +01:00
|
|
|
use reqwest::{
|
|
|
|
multipart::{Form, Part},
|
|
|
|
Body,
|
|
|
|
Client,
|
|
|
|
};
|
|
|
|
use tokio::{fs::File, io::reader_stream};
|
|
|
|
|
|
|
|
pub async fn run(
|
|
|
|
http: &Client,
|
|
|
|
token: String,
|
|
|
|
path: String,
|
|
|
|
name: String,
|
|
|
|
category: String,
|
|
|
|
open: bool,
|
|
|
|
) -> Result<()> {
|
2021-01-05 15:13:19 +01:00
|
|
|
util::assert_category_exists(http, &category).await?;
|
2020-12-05 12:08:24 +01:00
|
|
|
|
|
|
|
let res = http
|
|
|
|
.post("https://data.tilera.xyz/api/jensmemes/upload")
|
|
|
|
.multipart(
|
|
|
|
Form::new()
|
|
|
|
.text("category", category)
|
|
|
|
.text("token", token)
|
|
|
|
.part(
|
|
|
|
"file",
|
2020-12-06 14:57:14 +01:00
|
|
|
Part::stream(Body::wrap_stream(reader_stream({
|
|
|
|
info!("Opening file {}", &path);
|
|
|
|
File::open(path).await?
|
|
|
|
})))
|
|
|
|
.file_name(name),
|
2020-12-05 12:08:24 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let status = res.status();
|
2021-01-05 20:05:13 +01:00
|
|
|
let res = util::api::try_deserialize_api_reponse::<UpResp>(&res.bytes().await?)?;
|
2020-12-05 12:08:24 +01:00
|
|
|
|
|
|
|
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(())
|
|
|
|
}
|