feat: implement image tools (compress, resize, convert) and PDF tools (merge, split, images-to-pdf)
Image tools now actually process images: - Compress: re-encode JPEG with configurable quality - Resize: Lanczos3 resize with fit modes (inside/fill/crop) - Convert: cross-format conversion (JPEG/PNG/WebP/GIF/BMP) PDF tools: - Merge: combine multiple PDFs into one document - Split: extract page ranges via pagespec (1-3,5,7-9) - Images to PDF: embed images as JPEG in A4 pages with scaling - Compress: re-save PDF with optimized cross-reference table Also fix metrics format to emit default zero values. Co-Authored-By: Kilo <kilo@kilo.ai>
This commit is contained in:
@@ -1,15 +1,65 @@
|
||||
use std::path::Path;
|
||||
|
||||
use crate::config::WorkerConfig;
|
||||
use tools_common::types::Job;
|
||||
use crate::nats::progress::ProgressReporter;
|
||||
use tools_common::types::{Job, JobStatus, Tool};
|
||||
|
||||
mod compress;
|
||||
mod convert;
|
||||
mod resize;
|
||||
|
||||
/// Process an image tool job.
|
||||
pub async fn process_job(
|
||||
job: Job,
|
||||
_redis: &redis::Client,
|
||||
_config: &WorkerConfig,
|
||||
redis: &redis::Client,
|
||||
config: &WorkerConfig,
|
||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
tracing::info!(job_id = %job.id, tool = %job.tool.as_str(), "Processing image job (stub)");
|
||||
// TODO: Phase 2.2 - implement actual image processing
|
||||
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||
tracing::info!(job_id = %job.id, "Image job completed");
|
||||
tracing::info!(job_id = %job.id, tool = %job.tool.as_str(), "Processing image job");
|
||||
|
||||
let nats = async_nats::connect(&config.nats_url).await?;
|
||||
let progress = ProgressReporter::new(redis.clone(), nats, job.id, job.tool.clone());
|
||||
|
||||
progress
|
||||
.report(JobStatus::Processing { stage: "load".to_string(), progress: 10 }, "load", 10, "Memuat gambar...")
|
||||
.await?;
|
||||
|
||||
let input_path = Path::new(&job.file_path);
|
||||
let img = image::open(input_path)
|
||||
.map_err(|e| format!("Failed to load image: {}", e))?;
|
||||
|
||||
progress
|
||||
.report(JobStatus::Processing { stage: "process".to_string(), progress: 50 }, "process", 50, "Memproses...")
|
||||
.await?;
|
||||
|
||||
let output_dir = config.storage_path.join("output");
|
||||
tokio::fs::create_dir_all(&output_dir).await?;
|
||||
|
||||
let result_path = match job.tool {
|
||||
Tool::ImageCompress => compress::process(&img, &job.options, &output_dir, &progress).await?,
|
||||
Tool::ImageResize => resize::process(&img, &job.options, &output_dir, &progress).await?,
|
||||
Tool::ImageConvert => convert::process(&img, &job.options, &output_dir, &progress).await?,
|
||||
_ => {
|
||||
// Fallback: save as-is
|
||||
let output_path = output_dir.join(format!("{}.png", job.id));
|
||||
img.save(&output_path)?;
|
||||
output_path
|
||||
}
|
||||
};
|
||||
|
||||
progress
|
||||
.report(JobStatus::Completed, "complete", 100, "Selesai")
|
||||
.await?;
|
||||
|
||||
// Update Redis with result
|
||||
let mut conn = redis.get_multiplexed_async_connection().await?;
|
||||
crate::nats::consumer::JobConsumer::update_job_result(
|
||||
&mut conn,
|
||||
job.id,
|
||||
&result_path.to_string_lossy(),
|
||||
job.ttl_seconds,
|
||||
)
|
||||
.await?;
|
||||
|
||||
tracing::info!(job_id = %job.id, output = %result_path.display(), "Image job completed");
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user