jensmemesclient/cli/src/commands/up.rs

59 lines
1.3 KiB
Rust

use crate::util::open_link;
use anyhow::Result;
use jm_client_core::{api::UpResp, util};
use log::info;
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<()> {
util::assert_category_exists(http, &category).await?;
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({
info!("Opening file {}", &path);
File::open(path).await?
})))
.file_name(name),
),
)
.send()
.await?;
let status = res.status();
let res = util::api::try_deserialize_api_reponse::<UpResp>(&res.bytes().await?)?;
println!("Server responded with code {}", status);
if !open {
println!();
}
for f in res.files {
if open {
open_link(&f).await?;
} else {
println!("{}", f);
}
}
Ok(())
}