97 lines
2.3 KiB
Rust
97 lines
2.3 KiB
Rust
use std::{path::Path, sync::LazyLock};
|
|
|
|
use reqwest::{
|
|
blocking::{Client, multipart::Form},
|
|
header::AUTHORIZATION,
|
|
};
|
|
|
|
use crate::Config;
|
|
|
|
static CLIENT: LazyLock<Client> = LazyLock::new(Client::new);
|
|
|
|
#[derive(Debug, serde::Deserialize)]
|
|
pub struct ZiplineFolder {
|
|
pub id: String,
|
|
pub name: String,
|
|
}
|
|
|
|
#[derive(Debug, serde::Deserialize)]
|
|
pub struct ZiplineUploadResponse {
|
|
pub files: Vec<ZiplineFile>,
|
|
}
|
|
|
|
#[derive(Debug, serde::Deserialize)]
|
|
pub struct ZiplineFile {
|
|
pub id: String,
|
|
pub url: String,
|
|
}
|
|
|
|
#[derive(Debug, serde::Deserialize)]
|
|
pub struct ZiplineFileInfo {
|
|
pub thumbnail: Option<ZiplineThumbnail>,
|
|
}
|
|
|
|
#[derive(Debug, serde::Deserialize)]
|
|
pub struct ZiplineThumbnail {
|
|
pub path: String,
|
|
}
|
|
|
|
impl ZiplineFileInfo {
|
|
pub fn thumbnail_url(&self, config: &Config) -> Option<String> {
|
|
self.thumbnail
|
|
.as_ref()
|
|
.map(|t| format!("{}raw/{}", config.fixed_url(), t.path))
|
|
}
|
|
}
|
|
|
|
pub fn get_folders(config: &Config) -> Result<Vec<ZiplineFolder>, reqwest::Error> {
|
|
let url = format!("{}api/user/folders?noincl=true", config.fixed_url());
|
|
|
|
CLIENT
|
|
.get(url)
|
|
.header(AUTHORIZATION, &config.zipline_token)
|
|
.send()?
|
|
.json()
|
|
}
|
|
|
|
pub fn upload_file(
|
|
config: &Config,
|
|
folder: &ZiplineFolder,
|
|
file_path: &Path,
|
|
) -> Result<ZiplineUploadResponse, reqwest::Error> {
|
|
let url = format!("{}api/upload", config.fixed_url());
|
|
|
|
let form = Form::new().file("file", file_path).unwrap(); // FIXME
|
|
|
|
CLIENT
|
|
.post(url)
|
|
.header(AUTHORIZATION, &config.zipline_token)
|
|
.header("x-zipline-folder", &folder.id)
|
|
.header("x-zipline-format", "name")
|
|
.multipart(form)
|
|
.send()?
|
|
.json()
|
|
}
|
|
|
|
pub fn recalc_thumbnails(config: &Config) -> Result<(), reqwest::Error> {
|
|
let url = format!("{}api/server/thumbnails", config.fixed_url());
|
|
|
|
CLIENT
|
|
.post(url)
|
|
.header(AUTHORIZATION, &config.zipline_token)
|
|
.json(&[("rerun", false)])
|
|
.send()?
|
|
.error_for_status()?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn get_file_details(config: &Config, id: &str) -> Result<ZiplineFileInfo, reqwest::Error> {
|
|
let url = format!("{}api/user/files/{id}", config.fixed_url());
|
|
|
|
CLIENT
|
|
.get(url)
|
|
.header(AUTHORIZATION, &config.zipline_token)
|
|
.send()?
|
|
.json()
|
|
}
|