bambu-3mf-cli 0.1.0

Ultra-fast Bambu Studio & Orca Slicer 3MF inspection, API server, and asset extractor powered by Ferrox Framework
use crate::parser::B3mfParser;
use axum::{
    extract::Multipart,
    routing::{get, post},
    Json, Router,
};
use ferrox_app::FerroxApp;
use ferrox_transports::http::HttpTransport;
use serde_json::{json, Value};
use std::io::Write;
use tempfile::NamedTempFile;

pub async fn start_ferrox_server(port: u16) -> Result<(), Box<dyn std::error::Error>> {
    let router = Router::new()
        .route("/api/v1/health", get(health_check))
        .route("/api/v1/analyze", post(analyze_uploaded_3mf));

    let transport = HttpTransport::new(router, port);

    println!("⚡ Launching Ferrox Framework HTTP Transport for Bambu 3MF Server on port {}...", port);

    FerroxApp::new()
        .add_transport(transport)
        .start()
        .await?;

    Ok(())
}

async fn health_check() -> Json<Value> {
    Json(json!({
        "status": "ok",
        "service": "bambu-3mf-server",
        "framework": "Ferrox Framework v0.1.0",
        "author": "AI-Autistic-Intelligence"
    }))
}

async fn analyze_uploaded_3mf(mut multipart: Multipart) -> Json<Value> {
    while let Ok(Some(field)) = multipart.next_field().await {
        let name = field.name().unwrap_or_default().to_string();
        if name == "file" || field.file_name().unwrap_or_default().ends_with(".3mf") {
            if let Ok(bytes) = field.bytes().await {
                // Write bytes to temporary file for ZipArchive parsing
                if let Ok(mut temp_file) = NamedTempFile::new() {
                    if temp_file.write_all(&bytes).is_ok() {
                        if let Ok(analysis) = B3mfParser::parse(temp_file.path()) {
                            return Json(json!({
                                "success": true,
                                "analysis": analysis
                            }));
                        }
                    }
                }
            }
        }
    }

    Json(json!({
        "success": false,
        "error": "No valid .3mf file provided in 'file' field of multipart request"
    }))
}