use replicate_client::{Client, FileInput, FileOutput};
use tempfile::tempdir;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("šļø File Handling Demo");
let _client = match Client::from_env() {
Ok(client) => client,
Err(_) => {
println!("ā Please set the REPLICATE_API_TOKEN environment variable");
return Ok(());
}
};
println!("\nš Using file input from URL...");
let file_url = "https://replicate.delivery/pbxt/IJjJHBfbGfNT4gmhLkmHvV6XDKQDV3LJXhXH4C0WMPBozWqTwg1/view.jpeg";
let file_input = FileInput::from_url(file_url);
println!("ā
Created file input from URL: {}", file_url);
println!(" Type: URL = {}", file_input.is_url());
println!("\nš Using file input from local path...");
let temp_dir = tempdir()?;
let temp_file_path = temp_dir.path().join("test_image.txt");
tokio::fs::write(&temp_file_path, b"This is a test file").await?;
let file_input = FileInput::from_path(&temp_file_path);
println!("ā
Created file input from path: {:?}", temp_file_path);
println!(" Type: Path = {}", file_input.is_path());
println!("\nš¾ Using file input from bytes...");
let file_data = b"Binary file data here";
let file_input = FileInput::from_bytes_with_metadata(
file_data.as_slice(),
Some("test.bin".to_string()),
Some("application/octet-stream".to_string()),
);
println!("ā
Created file input from bytes");
println!(" Type: Bytes = {}", file_input.is_bytes());
println!("\nš¤ Working with file outputs...");
let file_output = FileOutput::new("https://example.com/output.png")
.with_filename("generated_image.png")
.with_content_type("image/png")
.with_size(1024 * 1024);
println!("ā
Created file output");
println!(" URL: {}", file_output.url);
println!(" Filename: {:?}", file_output.filename);
println!(" Content Type: {:?}", file_output.content_type);
println!(" Size: {:?} bytes", file_output.size);
println!("\nā¬ļø File download capabilities...");
println!(" File output can be downloaded with:");
println!(" - output.download().await // Gets bytes");
println!(" - output.save_to_path(path).await // Saves to file");
temp_dir.close()?;
println!("\nš File handling demo completed!");
Ok(())
}