71 lines
1.6 KiB
Rust
71 lines
1.6 KiB
Rust
use crate::util;
|
|
use anyhow::{Result, bail};
|
|
use jm_client_core::{api::UpResp, JMClient};
|
|
use log::info;
|
|
use reqwest::{
|
|
multipart::{Form, Part},
|
|
Body,
|
|
};
|
|
use tokio::{fs::File, io::reader_stream};
|
|
|
|
pub async fn run(
|
|
client: &JMClient,
|
|
token: String,
|
|
path: String,
|
|
name: String,
|
|
category: String,
|
|
open: bool,
|
|
) -> Result<()> {
|
|
util::assert_category_exists(client, &category).await?;
|
|
|
|
let res = client
|
|
.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();
|
|
// TODO move into JMClient
|
|
let bytes = &res.bytes().await?;
|
|
|
|
let res = if let Ok(res) = serde_json::from_slice::<UpResp>(bytes) {
|
|
res
|
|
} else {
|
|
if let Ok(s) = std::str::from_utf8(bytes) {
|
|
bail!("Server responded with unexpected response: {}", s);
|
|
} else {
|
|
bail!("Server responded with invalid utf8 bytes: {:?}", bytes);
|
|
}
|
|
};
|
|
|
|
|
|
println!("Server responded with code {}", status);
|
|
|
|
if !open {
|
|
println!();
|
|
}
|
|
|
|
for f in res.files {
|
|
if open {
|
|
util::open_link(&f).await?;
|
|
} else {
|
|
println!("{}", f);
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|