use crate::config::Settings; use crate::meta_plugin::{get_meta_plugin, MetaPlugin, MetaPluginType}; use crate::modes::common::{settings_digest_type, settings_meta_plugin_types}; use clap::Command; use log::debug; use rusqlite::Connection; use std::collections::HashMap; pub struct MetaService; impl MetaService { pub fn new() -> Self { Self } pub fn get_plugins(&self, cmd: &mut Command, settings: &Settings) -> Vec> { let mut meta_plugin_types: Vec = settings_meta_plugin_types(cmd, settings); let digest_type = settings_digest_type(cmd, settings); let digest_meta_plugin_type = match digest_type { MetaPluginType::DigestSha256 => Some(MetaPluginType::DigestSha256), MetaPluginType::DigestMd5 => Some(MetaPluginType::DigestMd5), _ => None, }; if let Some(digest_plugin_type) = digest_meta_plugin_type { if !meta_plugin_types.contains(&digest_plugin_type) { meta_plugin_types.push(digest_plugin_type); } } debug!("MetaService: Meta plugin types: {:?}", meta_plugin_types); let mut meta_plugins: Vec> = meta_plugin_types .iter() .map(|meta_plugin_type| get_meta_plugin(meta_plugin_type.clone())) .collect(); if let Some(meta_plugin_configs) = &settings.meta_plugins { for meta_plugin in meta_plugins.iter_mut() { let plugin_name = meta_plugin.meta_name(); if let Some(config) = meta_plugin_configs.iter().find(|c| c.name == plugin_name) { let mut configured_outputs = meta_plugin.outputs().clone(); for (key, value) in &config.outputs { configured_outputs.insert(key.clone(), serde_yaml::Value::String(value.clone())); } let mut configured_options = meta_plugin.default_options(); for (key, value) in &config.options { configured_options.insert(key.clone(), value.clone()); } if let Err(e) = meta_plugin.configure_outputs(&configured_outputs) { eprintln!("Warning: Failed to configure outputs for meta plugin '{}': {}", plugin_name, e); } if let Err(e) = meta_plugin.configure_options(&configured_options) { eprintln! ( "Warning: Failed to configure options for meta plugin '{}': {}", plugin_name, e ); } } } } let original_len = meta_plugins.len(); meta_plugins.retain(|meta_plugin| meta_plugin.is_supported()); if meta_plugins.len() < original_len { // This is not perfect as it doesn't say which one, but avoids complex logic from save.rs eprintln!("Warning: Some meta plugins are enabled but not supported on this system"); } meta_plugins } pub fn initialize_plugins( &self, plugins: &mut [Box], conn: &Connection, item_id: i64, ) { for meta_plugin in plugins.iter_mut() { if let Err(e) = meta_plugin.initialize(conn, item_id) { eprintln!("Warning: Failed to initialize meta plugin: {}", e); } } } pub fn process_chunk( &self, plugins: &mut [Box], chunk: &[u8], conn: &Connection, ) { for meta_plugin in plugins.iter_mut() { meta_plugin.update(chunk, conn); } } pub fn finalize_plugins(&self, plugins: &mut [Box], conn: &Connection) { for meta_plugin in plugins.iter_mut() { if let Err(e) = meta_plugin.finalize(conn) { eprintln!("Warning: Failed to finalize meta plugin: {}", e); } } } pub fn collect_initial_meta(&self) -> HashMap { let mut item_meta: HashMap = crate::modes::common::get_meta_from_env(); if let Ok(hostname) = gethostname::gethostname().into_string() { if !item_meta.contains_key("hostname") { item_meta.insert("hostname".to_string(), hostname); } } item_meta } } impl Default for MetaService { fn default() -> Self { Self::new() } }