diff --git a/src/main.rs b/src/main.rs index 54b1b65..ae2d349 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use std::fs; -use serde::Deserialize; +use std::path::Path; +use serde::{Deserialize, Serialize}; use reqwest::blocking::Client; use std::error::Error; @@ -32,11 +33,13 @@ fn get_jpeg_files(directory: &str) -> Vec { images } -fn format_post_text(template: &str, batch_num: usize, total_batches: usize) -> String { - template.replace("@batch@", &format!("Batch {} out of {}", batch_num, total_batches)) +fn format_post_text(template: &str, batch_num: usize, total_batches: usize, title: &str) -> String { + template + .replace("@batch@", &format!("Batch {} out of {}", batch_num, total_batches)) + .replace("@title@", title) } -fn upload_images_batch(client: &Client, config: &Config, images: &[String], batch_num: usize, total_batches: usize) -> Result<(), Box> { +fn upload_images_batch(client: &Client, config: &Config, images: &[String], batch_num: usize, total_batches: usize, title: &str) -> Result<(), Box> { let url = format!("{}/api/v1/media", config.pixelfed_url); let mut media_ids = Vec::new(); @@ -57,7 +60,7 @@ fn upload_images_batch(client: &Client, config: &Config, images: &[String], batc if !media_ids.is_empty() { let post_url = format!("{}/api/v1/statuses", config.pixelfed_url); - let post_text = format_post_text(&config.default_text, batch_num, total_batches); + let post_text = format_post_text(&config.default_text, batch_num, total_batches, title); let body = serde_json::json!({ "status": post_text, "media_ids": media_ids, @@ -76,17 +79,24 @@ fn upload_images_batch(client: &Client, config: &Config, images: &[String], batc fn main() -> Result<(), Box> { let args: Vec = std::env::args().collect(); if args.len() < 2 { - eprintln!("Usage: {} ", args[0]); + eprintln!("Usage: {} [--title ]", args[0]); std::process::exit(1); } + let mut title = "".to_string(); + if let Some(index) = args.iter().position(|x| x == "--title") { + if index + 1 < args.len() { + title = args[index + 1].clone(); + } + } + let config = load_config()?; let images = get_jpeg_files(&args[1]); let client = Client::new(); let total_batches = (images.len() + 19) / 20; for (i, chunk) in images.chunks(20).enumerate() { - upload_images_batch(&client, &config, chunk, i + 1, total_batches)?; + upload_images_batch(&client, &config, chunk, i + 1, total_batches, &title)?; } println!("All images uploaded successfully.");