feat: add initial zipline uploading system
This commit is contained in:
parent
82a09ddf26
commit
ed69a54524
4 changed files with 2097 additions and 2 deletions
1896
Cargo.lock
generated
1896
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -4,3 +4,11 @@ version = "0.1.0"
|
|||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
color-eyre = "0.6.4"
|
||||
dirs = "6.0.0"
|
||||
reqwest = { version = "0.12.15", default-features = false, features = ["http2", "charset", "rustls-tls", "blocking", "json", "multipart"] }
|
||||
serde = { version = "1.0.219", features = ["derive"] }
|
||||
toml = "0.8.22"
|
||||
tracing = "0.1.41"
|
||||
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
|
||||
urlencoding = "2.1.3"
|
||||
|
|
98
src/main.rs
98
src/main.rs
|
@ -1,3 +1,97 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
mod zipline;
|
||||
|
||||
use std::{path::Path, time::Duration};
|
||||
|
||||
use color_eyre::eyre::{OptionExt, Result, bail};
|
||||
use tracing::Level;
|
||||
use tracing_subscriber::EnvFilter;
|
||||
use urlencoding::encode;
|
||||
|
||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
||||
struct Config {
|
||||
zipline_url: String,
|
||||
zipline_token: String,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
fn fixed_url(&self) -> String {
|
||||
let mut url = self.zipline_url.clone();
|
||||
if !url.ends_with("/") {
|
||||
url += "/";
|
||||
}
|
||||
if url.ends_with("api/") {
|
||||
url.truncate(url.len() - 3);
|
||||
}
|
||||
|
||||
url
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(EnvFilter::from_default_env().add_directive(Level::INFO.into()))
|
||||
.init();
|
||||
|
||||
color_eyre::install()?;
|
||||
|
||||
let Some(config_dir) = dirs::config_dir() else {
|
||||
bail!("could not get config dir!");
|
||||
};
|
||||
|
||||
std::fs::create_dir_all(&config_dir)?;
|
||||
|
||||
let config_file_path = config_dir.join("tyrolienne.toml");
|
||||
let config_contents = match std::fs::read_to_string(&config_file_path) {
|
||||
Ok(str) => str,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
|
||||
let default_cfg = toml::to_string(&Config::default())?;
|
||||
std::fs::write(&config_file_path, &default_cfg)?;
|
||||
bail!("config file was not found, wrote default one at {config_file_path:?}");
|
||||
}
|
||||
Err(e) => bail!(e),
|
||||
};
|
||||
|
||||
let config: Config = toml::from_str(&config_contents)?;
|
||||
if config.zipline_token.is_empty() {
|
||||
bail!("zipline token is empty! please set it at {config_file_path:?}");
|
||||
}
|
||||
if config.zipline_url.is_empty() {
|
||||
bail!("zipline url is empty! please set it at {config_file_path:?}");
|
||||
}
|
||||
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
let file = args.get(1).ok_or_eyre(
|
||||
"please specify the path to the file you want to upload as the first command line argument",
|
||||
)?;
|
||||
|
||||
let folders = zipline::get_folders(&config)?;
|
||||
let folder = &folders[0];
|
||||
|
||||
tracing::info!("uploading to folder '{}'...", folder.name);
|
||||
|
||||
let res = zipline::upload_file(&config, folder, Path::new(file))?;
|
||||
let zp_file = &res.files[0];
|
||||
|
||||
tracing::info!("recalculating thumbnails...");
|
||||
|
||||
zipline::recalc_thumbnails(&config)?;
|
||||
|
||||
std::thread::sleep(Duration::from_secs(5));
|
||||
|
||||
tracing::info!("fetching thumbnail url...");
|
||||
|
||||
let res = zipline::get_file_details(&config, &zp_file.id)?;
|
||||
let thumbnail_url = res
|
||||
.thumbnail_url(&config)
|
||||
.ok_or_eyre("could not get thumbnail url")?;
|
||||
|
||||
// TODO get w&h from video
|
||||
let autocomp_url = format!(
|
||||
"https://autocompressor.net/av1?v={}&i={}&w=1920&h=1080",
|
||||
encode(&zp_file.url),
|
||||
encode(&thumbnail_url)
|
||||
);
|
||||
tracing::info!("url: {autocomp_url}");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
97
src/zipline.rs
Normal file
97
src/zipline.rs
Normal file
|
@ -0,0 +1,97 @@
|
|||
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()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue