use crate::constants::TEMP_EPOCHS;
use crate::error::Result;
use crate::processing::validate_file_exists;
use crate::walrus::{upload_to_walrus_sync, WalrusOptions};
use std::path::Path;
pub fn upload_image_to_walrus(
input_path: &Path,
aggregator_url: Option<String>,
publisher_url: Option<String>,
epochs: Option<u64>,
temp: bool,
) -> Result<()> {
println!("📤 Uploading to Walrus: {:?}", input_path);
validate_file_exists(input_path)?;
let final_epochs = if temp {
Some(TEMP_EPOCHS) } else {
epochs
};
let options = WalrusOptions::new(aggregator_url, publisher_url, final_epochs);
println!("🔗 Aggregator URL: {}", options.aggregator_url);
println!("🔗 Publisher URL: {}", options.publisher_url);
println!("⏰ Epochs: {:?}", options.epochs);
let blob_id = upload_to_walrus_sync(input_path, &options)?;
println!("✅ Upload successful!");
println!("🆔 Blob ID: {}", blob_id);
let access_url = build_walrus_access_url(&options.aggregator_url, &blob_id);
println!("🌐 Access URL: {}", access_url);
if temp {
println!("⏰ Temporary file: Will expire after 1 epoch (~24 hours)");
println!("🔄 Use without -t flag for longer storage");
}
if let Ok(metadata) = std::fs::metadata(input_path) {
println!("📊 File size: {} bytes", metadata.len());
}
println!("💡 You can use the blob ID to retrieve the file later");
Ok(())
}
fn build_walrus_access_url(aggregator_url: &str, blob_id: &str) -> String {
if aggregator_url.ends_with('/') {
format!("{}v1/blobs/{}", aggregator_url, blob_id)
} else {
format!("{}/v1/blobs/{}", aggregator_url, blob_id)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_build_walrus_access_url_with_trailing_slash() {
let aggregator = "https://example.com/";
let blob_id = "test123";
let result = build_walrus_access_url(aggregator, blob_id);
assert_eq!(result, "https://example.com/v1/blobs/test123");
}
#[test]
fn test_build_walrus_access_url_without_trailing_slash() {
let aggregator = "https://example.com";
let blob_id = "test123";
let result = build_walrus_access_url(aggregator, blob_id);
assert_eq!(result, "https://example.com/v1/blobs/test123");
}
}