firecloud-cli 0.2.0

Command-line interface for FireCloud P2P messaging and file sharing
//! Chunk command - Test chunking a file

use anyhow::Result;
use firecloud_storage::{compress, CompressionLevel, FileChunker};
use std::fs::File;
use std::path::PathBuf;
use tracing::info;

pub async fn run(file_path: PathBuf) -> Result<()> {
    info!("Chunking file: {}", file_path.display());

    let file = File::open(&file_path)?;
    let file_size = file.metadata()?.len();

    info!("File size: {} bytes", file_size);

    let chunker = FileChunker::new();
    let chunks = chunker.chunk_reader(file)?;

    info!("Split into {} chunks:", chunks.len());

    let mut total_compressed = 0u64;

    for (i, chunk) in chunks.iter().enumerate() {
        let compressed = compress(&chunk.data, CompressionLevel::Balanced)?;
        let ratio = if chunk.data.len() > 0 {
            (compressed.len() as f64 / chunk.data.len() as f64) * 100.0
        } else {
            100.0
        };

        info!(
            "  Chunk {}: {} bytes (hash: {}), compressed: {} bytes ({:.1}%)",
            i + 1,
            chunk.data.len(),
            chunk.hash(),
            compressed.len(),
            ratio
        );

        total_compressed += compressed.len() as u64;
    }

    let overall_ratio = if file_size > 0 {
        (total_compressed as f64 / file_size as f64) * 100.0
    } else {
        100.0
    };

    info!("---");
    info!(
        "Total: {} bytes → {} bytes ({:.1}%)",
        file_size, total_compressed, overall_ratio
    );

    Ok(())
}