feat: better error handling

errors during the upload are now shown in a separate user-friendly alert
dialog, and you can also choose to upload the file to no folder
This commit is contained in:
uku 2025-05-11 23:21:28 +02:00
parent 601ee85b5f
commit 94562b995a
Signed by: uku
SSH key fingerprint: SHA256:4P0aN6M8ajKukNi6aPOaX0LacanGYtlfjmN+m/sHY/o
4 changed files with 174 additions and 36 deletions

View file

@ -1,6 +1,8 @@
use std::{path::Path, sync::LazyLock};
use color_eyre::eyre::{Result, bail};
use reqwest::{
StatusCode,
blocking::{Client, multipart::Form},
header::AUTHORIZATION,
};
@ -44,54 +46,76 @@ impl ZiplineFileInfo {
}
}
pub fn get_folders(config: &Config) -> Result<Vec<ZiplineFolder>, reqwest::Error> {
pub fn get_folders(config: &Config) -> Result<Vec<ZiplineFolder>> {
let url = format!("{}api/user/folders?noincl=true", config.fixed_url());
CLIENT
let res = CLIENT
.get(url)
.header(AUTHORIZATION, &config.zipline_token)
.send()?
.json()
.send()?;
if res.status() != StatusCode::OK {
bail!("an error occurred ({}): {}", res.status(), res.text()?);
} else {
res.json().map_err(Into::into)
}
}
pub fn upload_file(
config: &Config,
folder: &ZiplineFolder,
folder: Option<&ZiplineFolder>,
file_path: &Path,
) -> Result<ZiplineUploadResponse, reqwest::Error> {
) -> Result<ZiplineUploadResponse> {
let url = format!("{}api/upload", config.fixed_url());
let form = Form::new().file("file", file_path).unwrap(); // FIXME
let form = Form::new().file("file", file_path)?;
CLIENT
let mut req = CLIENT
.post(url)
.header(AUTHORIZATION, &config.zipline_token)
.header("x-zipline-folder", &folder.id)
.header("x-zipline-format", "name")
.multipart(form)
.send()?
.json()
.multipart(form);
if let Some(folder) = folder {
req = req.header("x-zipline-folder", &folder.id);
}
let res = req.send()?;
if res.status() != StatusCode::OK {
bail!("an error occurred ({}): {}", res.status(), res.text()?);
} else {
res.json().map_err(Into::into)
}
}
pub fn recalc_thumbnails(config: &Config) -> Result<(), reqwest::Error> {
pub fn recalc_thumbnails(config: &Config) -> Result<()> {
let url = format!("{}api/server/thumbnails", config.fixed_url());
CLIENT
let res = CLIENT
.post(url)
.header(AUTHORIZATION, &config.zipline_token)
.json(&[("rerun", false)])
.send()?
.error_for_status()?;
.send()?;
Ok(())
if res.status() != StatusCode::OK {
bail!("an error occurred ({}): {}", res.status(), res.text()?);
} else {
Ok(())
}
}
pub fn get_file_details(config: &Config, id: &str) -> Result<ZiplineFileInfo, reqwest::Error> {
pub fn get_file_details(config: &Config, id: &str) -> Result<ZiplineFileInfo> {
let url = format!("{}api/user/files/{id}", config.fixed_url());
CLIENT
let res = CLIENT
.get(url)
.header(AUTHORIZATION, &config.zipline_token)
.send()?
.json()
.send()?;
if res.status() != StatusCode::OK {
bail!("an error occurred ({}): {}", res.status(), res.text()?);
} else {
res.json().map_err(Into::into)
}
}