refactor: update meta plugins to use new trait interface

Co-authored-by: aider (openai/andrew/openrouter/mistralai/mistral-medium-3.1) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-26 15:35:57 -03:00
parent f48d7b33b8
commit 7b43827926
5 changed files with 169 additions and 51 deletions

View File

@@ -82,17 +82,26 @@ impl MetaPlugin for BinaryMetaPlugin {
true true
} }
fn finalize(&mut self, conn: &Connection) -> Result<()> { fn finalize(&mut self) -> Result<PluginResponse> {
// Save the binary detection result when finalizing, if not already saved let mut metadata = Vec::new();
self.save_metadata(conn)
}
fn update(&mut self, data: &[u8], conn: &Connection) { // Save the binary detection result when finalizing, if not already saved
// If we've already saved the metadata, no need to collect more data if let Some(item_id) = self.item_id {
if self.is_saved { let is_binary_result = is_binary(&self.buffer);
return; let value = if is_binary_result { "true".to_string() } else { "false".to_string() };
if let Some(meta) = self.create_meta(item_id, "binary", value) {
metadata.push(meta);
}
} }
Ok(PluginResponse {
metadata: Some(metadata),
is_finalized: true,
})
}
fn update(&mut self, data: &[u8]) -> Result<PluginResponse> {
// Calculate how much data we can still accept // Calculate how much data we can still accept
let remaining_capacity = self.max_buffer_size.saturating_sub(self.buffer.len()); let remaining_capacity = self.max_buffer_size.saturating_sub(self.buffer.len());
if remaining_capacity > 0 { if remaining_capacity > 0 {
@@ -103,19 +112,32 @@ impl MetaPlugin for BinaryMetaPlugin {
self.buffer.extend_from_slice(&data[..bytes_to_take]); self.buffer.extend_from_slice(&data[..bytes_to_take]);
} }
// If we've reached our buffer limit, save the metadata immediately // If we've reached our buffer limit, return metadata
let mut metadata = Vec::new();
if self.buffer.len() >= self.max_buffer_size { if self.buffer.len() >= self.max_buffer_size {
let _ = self.save_metadata(conn); if let Some(item_id) = self.item_id {
let is_binary_result = is_binary(&self.buffer);
let value = if is_binary_result { "true".to_string() } else { "false".to_string() };
if let Some(meta) = self.create_meta(item_id, "binary", value) {
metadata.push(meta);
}
}
} }
Ok(PluginResponse {
metadata: if metadata.is_empty() { None } else { Some(metadata) },
is_finalized: !metadata.is_empty(),
})
} }
fn meta_name(&self) -> String { fn meta_name(&self) -> String {
self.meta_name.clone() self.meta_name.clone()
} }
fn initialize(&mut self, _conn: &Connection, item_id: i64) -> Result<()> { fn initialize(&mut self, item_id: i64) -> Result<PluginResponse> {
self.item_id = Some(item_id); self.item_id = Some(item_id);
Ok(()) Ok(PluginResponse::default())
} }
fn configure_options(&mut self, options: &std::collections::HashMap<String, serde_yaml::Value>) -> Result<()> { fn configure_options(&mut self, options: &std::collections::HashMap<String, serde_yaml::Value>) -> Result<()> {

View File

@@ -160,11 +160,11 @@ impl MetaPlugin for ReadTimeMetaPlugin {
true true
} }
fn finalize(&mut self, _conn: &Connection) -> Result<()> { fn finalize(&mut self) -> Result<PluginResponse> {
Ok(()) Ok(PluginResponse::default())
} }
fn update(&mut self, _data: &[u8], _conn: &Connection) { fn update(&mut self, _data: &[u8]) -> Result<PluginResponse> {
if self.start_time.is_none() { if self.start_time.is_none() {
self.start_time = Some(Instant::now()); self.start_time = Some(Instant::now());
} }
@@ -252,15 +252,36 @@ impl MetaPlugin for ReadRateMetaPlugin {
true true
} }
fn finalize(&mut self, _conn: &Connection) -> Result<()> { fn finalize(&mut self) -> Result<PluginResponse> {
Ok(()) let mut metadata = Vec::new();
if let Some(start_time) = self.start_time {
if let Some(item_id) = self.item_id {
let duration = start_time.elapsed();
let rate = if duration.as_secs_f64() > 0.0 {
format!("{:.2} KB/s", (self.bytes_read as f64 / 1024.0) / duration.as_secs_f64())
} else {
"N/A".to_string()
};
if let Some(meta) = self.create_meta(item_id, "read_rate", rate) {
metadata.push(meta);
}
}
}
Ok(PluginResponse {
metadata: Some(metadata),
is_finalized: true,
})
} }
fn update(&mut self, data: &[u8], _conn: &Connection) { fn update(&mut self, data: &[u8]) -> Result<PluginResponse> {
if self.start_time.is_none() { if self.start_time.is_none() {
self.start_time = Some(Instant::now()); self.start_time = Some(Instant::now());
} }
self.bytes_read += data.len() as u64; self.bytes_read += data.len() as u64;
Ok(PluginResponse::default())
} }
fn meta_name(&self) -> String { fn meta_name(&self) -> String {

View File

@@ -122,7 +122,7 @@ impl MetaPlugin for MagicFileMetaPlugin {
true true
} }
fn initialize(&mut self, _conn: &Connection, item_id: i64) -> Result<()> { fn initialize(&mut self, item_id: i64) -> Result<PluginResponse> {
self.item_id = Some(item_id); self.item_id = Some(item_id);
// Initialize the magic cookie once // Initialize the magic cookie once
@@ -132,33 +132,98 @@ impl MetaPlugin for MagicFileMetaPlugin {
.map_err(|e| anyhow::anyhow!("Failed to load magic database: {}", e))?; .map_err(|e| anyhow::anyhow!("Failed to load magic database: {}", e))?;
self.cookie = Some(cookie); self.cookie = Some(cookie);
Ok(()) Ok(PluginResponse::default())
} }
fn finalize(&mut self, conn: &Connection) -> Result<()> { fn finalize(&mut self) -> Result<PluginResponse> {
let mut metadata = Vec::new();
// Save all magic metadata if not already saved // Save all magic metadata if not already saved
if !self.is_saved { if let Some(item_id) = self.item_id {
if let Err(e) = self.save_all_magic_metadata(conn) { if let Some(cookie) = &self.cookie {
eprintln!("Warning: Failed to save magic metadata: {}", e); // Get mime type
if let Ok(mime_type) = self.get_magic_result(CookieFlags::MIME_TYPE) {
if !mime_type.is_empty() {
if let Some(meta) = self.create_meta(item_id, "mime_type", mime_type) {
metadata.push(meta);
}
}
}
// Get mime encoding
if let Ok(mime_encoding) = self.get_magic_result(CookieFlags::MIME_ENCODING) {
if !mime_encoding.is_empty() {
if let Some(meta) = self.create_meta(item_id, "mime_encoding", mime_encoding) {
metadata.push(meta);
}
}
}
// Get file type
if let Ok(file_type) = self.get_magic_result(CookieFlags::default()) {
if !file_type.is_empty() {
if let Some(meta) = self.create_meta(item_id, "file_type", file_type) {
metadata.push(meta);
}
}
}
} }
} }
Ok(())
Ok(PluginResponse {
metadata: if metadata.is_empty() { None } else { Some(metadata) },
is_finalized: true,
})
} }
fn update(&mut self, data: &[u8], conn: &Connection) { fn update(&mut self, data: &[u8]) -> Result<PluginResponse> {
let mut metadata = Vec::new();
// Only collect up to max_buffer_size // Only collect up to max_buffer_size
let remaining_capacity = self.max_buffer_size.saturating_sub(self.buffer.len()); let remaining_capacity = self.max_buffer_size.saturating_sub(self.buffer.len());
if remaining_capacity > 0 { if remaining_capacity > 0 {
let bytes_to_copy = std::cmp::min(data.len(), remaining_capacity); let bytes_to_copy = std::cmp::min(data.len(), remaining_capacity);
self.buffer.extend_from_slice(&data[..bytes_to_copy]); self.buffer.extend_from_slice(&data[..bytes_to_copy]);
// Check if we've reached our buffer limit and save if so // Check if we've reached our buffer limit and return metadata
if self.buffer.len() >= self.max_buffer_size && !self.is_saved { if self.buffer.len() >= self.max_buffer_size {
if let Err(e) = self.save_all_magic_metadata(conn) { if let Some(item_id) = self.item_id {
eprintln!("Warning: Failed to save magic metadata early: {}", e); if let Some(cookie) = &self.cookie {
// Get mime type
if let Ok(mime_type) = self.get_magic_result(CookieFlags::MIME_TYPE) {
if !mime_type.is_empty() {
if let Some(meta) = self.create_meta(item_id, "mime_type", mime_type) {
metadata.push(meta);
}
}
}
// Get mime encoding
if let Ok(mime_encoding) = self.get_magic_result(CookieFlags::MIME_ENCODING) {
if !mime_encoding.is_empty() {
if let Some(meta) = self.create_meta(item_id, "mime_encoding", mime_encoding) {
metadata.push(meta);
}
}
}
// Get file type
if let Ok(file_type) = self.get_magic_result(CookieFlags::default()) {
if !file_type.is_empty() {
if let Some(meta) = self.create_meta(item_id, "file_type", file_type) {
metadata.push(meta);
}
}
}
}
} }
} }
} }
Ok(PluginResponse {
metadata: if metadata.is_empty() { None } else { Some(metadata) },
is_finalized: !metadata.is_empty(),
})
} }
fn meta_name(&self) -> String { fn meta_name(&self) -> String {

View File

@@ -95,7 +95,7 @@ impl MetaPlugin for MetaPluginProgram {
false false
} }
fn initialize(&mut self, _conn: &rusqlite::Connection, item_id: i64) -> Result<()> { fn initialize(&mut self, item_id: i64) -> Result<PluginResponse> {
debug!("META: Initializing program plugin: {:?}", self); debug!("META: Initializing program plugin: {:?}", self);
// Store item ID for later use // Store item ID for later use
@@ -122,11 +122,13 @@ impl MetaPlugin for MetaPluginProgram {
self.writer = Some(Box::new(stdin)); self.writer = Some(Box::new(stdin));
self.process = Some(process); self.process = Some(process);
Ok(()) Ok(PluginResponse::default())
} }
fn finalize(&mut self, conn: &Connection) -> Result<()> { fn finalize(&mut self) -> Result<PluginResponse> {
debug!("META: Finalizing program plugin"); debug!("META: Finalizing program plugin");
let mut metadata = Vec::new();
if let Some(process) = self.process.take() { if let Some(process) = self.process.take() {
// Close stdin to signal end of input // Close stdin to signal end of input
drop(self.writer.take()); drop(self.writer.take());
@@ -147,9 +149,11 @@ impl MetaPlugin for MetaPluginProgram {
debug!("META: Program output: {}", result); debug!("META: Program output: {}", result);
self.result = Some(result); self.result = Some(result);
// Save the result to database if we have item_id // Create metadata to be stored
if let Some(item_id) = self.item_id { if let Some(item_id) = self.item_id {
let _ = self.save_meta(conn, item_id, &self.meta_name.clone(), self.result.clone().unwrap()); if let Some(meta) = self.create_meta(item_id, &self.meta_name, result) {
metadata.push(meta);
}
} }
} }
} else { } else {
@@ -160,15 +164,20 @@ impl MetaPlugin for MetaPluginProgram {
} }
} }
} }
Ok(())
Ok(PluginResponse {
metadata: if metadata.is_empty() { None } else { Some(metadata) },
is_finalized: true,
})
} }
fn update(&mut self, data: &[u8], _conn: &Connection) { fn update(&mut self, data: &[u8]) -> Result<PluginResponse> {
if let Some(ref mut writer) = self.writer { if let Some(ref mut writer) = self.writer {
if let Err(e) = writer.write_all(data) { if let Err(e) = writer.write_all(data) {
debug!("META: Failed to write to process stdin: {}", e); debug!("META: Failed to write to process stdin: {}", e);
} }
} }
Ok(PluginResponse::default())
} }
fn meta_name(&self) -> String { fn meta_name(&self) -> String {

View File

@@ -60,13 +60,14 @@ impl MetaPlugin for CwdMetaPlugin {
true true
} }
fn finalize(&mut self, _conn: &Connection) -> Result<()> { fn finalize(&mut self) -> Result<PluginResponse> {
// Since we save during initialize(), return Ok to avoid duplicate saves // Since we save during initialize(), return Ok to avoid duplicate saves
Ok(()) Ok(PluginResponse::default())
} }
fn update(&mut self, _data: &[u8], _conn: &Connection) { fn update(&mut self, _data: &[u8]) -> Result<PluginResponse> {
// No update needed // No update needed
Ok(PluginResponse::default())
} }
fn meta_name(&self) -> String { fn meta_name(&self) -> String {