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:
@@ -82,17 +82,26 @@ impl MetaPlugin for BinaryMetaPlugin {
|
||||
true
|
||||
}
|
||||
|
||||
fn finalize(&mut self, conn: &Connection) -> Result<()> {
|
||||
fn finalize(&mut self) -> Result<PluginResponse> {
|
||||
let mut metadata = Vec::new();
|
||||
|
||||
// Save the binary detection result when finalizing, if not already saved
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self, data: &[u8], conn: &Connection) {
|
||||
// If we've already saved the metadata, no need to collect more data
|
||||
if self.is_saved {
|
||||
return;
|
||||
Ok(PluginResponse {
|
||||
metadata: Some(metadata),
|
||||
is_finalized: true,
|
||||
})
|
||||
}
|
||||
|
||||
fn update(&mut self, data: &[u8]) -> Result<PluginResponse> {
|
||||
// Calculate how much data we can still accept
|
||||
let remaining_capacity = self.max_buffer_size.saturating_sub(self.buffer.len());
|
||||
if remaining_capacity > 0 {
|
||||
@@ -103,19 +112,32 @@ impl MetaPlugin for BinaryMetaPlugin {
|
||||
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 {
|
||||
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 {
|
||||
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);
|
||||
Ok(())
|
||||
Ok(PluginResponse::default())
|
||||
}
|
||||
|
||||
fn configure_options(&mut self, options: &std::collections::HashMap<String, serde_yaml::Value>) -> Result<()> {
|
||||
|
||||
@@ -160,11 +160,11 @@ impl MetaPlugin for ReadTimeMetaPlugin {
|
||||
true
|
||||
}
|
||||
|
||||
fn finalize(&mut self, _conn: &Connection) -> Result<()> {
|
||||
Ok(())
|
||||
fn finalize(&mut self) -> Result<PluginResponse> {
|
||||
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() {
|
||||
self.start_time = Some(Instant::now());
|
||||
}
|
||||
@@ -252,15 +252,36 @@ impl MetaPlugin for ReadRateMetaPlugin {
|
||||
true
|
||||
}
|
||||
|
||||
fn finalize(&mut self, _conn: &Connection) -> Result<()> {
|
||||
Ok(())
|
||||
fn finalize(&mut self) -> Result<PluginResponse> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self, data: &[u8], _conn: &Connection) {
|
||||
Ok(PluginResponse {
|
||||
metadata: Some(metadata),
|
||||
is_finalized: true,
|
||||
})
|
||||
}
|
||||
|
||||
fn update(&mut self, data: &[u8]) -> Result<PluginResponse> {
|
||||
if self.start_time.is_none() {
|
||||
self.start_time = Some(Instant::now());
|
||||
}
|
||||
self.bytes_read += data.len() as u64;
|
||||
Ok(PluginResponse::default())
|
||||
}
|
||||
|
||||
fn meta_name(&self) -> String {
|
||||
|
||||
@@ -122,7 +122,7 @@ impl MetaPlugin for MagicFileMetaPlugin {
|
||||
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);
|
||||
|
||||
// Initialize the magic cookie once
|
||||
@@ -132,33 +132,98 @@ impl MetaPlugin for MagicFileMetaPlugin {
|
||||
.map_err(|e| anyhow::anyhow!("Failed to load magic database: {}", e))?;
|
||||
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
|
||||
if !self.is_saved {
|
||||
if let Err(e) = self.save_all_magic_metadata(conn) {
|
||||
eprintln!("Warning: Failed to save magic metadata: {}", e);
|
||||
if let Some(item_id) = self.item_id {
|
||||
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);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn update(&mut self, data: &[u8], conn: &Connection) {
|
||||
// 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: true,
|
||||
})
|
||||
}
|
||||
|
||||
fn update(&mut self, data: &[u8]) -> Result<PluginResponse> {
|
||||
let mut metadata = Vec::new();
|
||||
|
||||
// Only collect up to max_buffer_size
|
||||
let remaining_capacity = self.max_buffer_size.saturating_sub(self.buffer.len());
|
||||
if remaining_capacity > 0 {
|
||||
let bytes_to_copy = std::cmp::min(data.len(), remaining_capacity);
|
||||
self.buffer.extend_from_slice(&data[..bytes_to_copy]);
|
||||
|
||||
// Check if we've reached our buffer limit and save if so
|
||||
if self.buffer.len() >= self.max_buffer_size && !self.is_saved {
|
||||
if let Err(e) = self.save_all_magic_metadata(conn) {
|
||||
eprintln!("Warning: Failed to save magic metadata early: {}", e);
|
||||
// Check if we've reached our buffer limit and return metadata
|
||||
if self.buffer.len() >= self.max_buffer_size {
|
||||
if let Some(item_id) = self.item_id {
|
||||
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 {
|
||||
|
||||
@@ -95,7 +95,7 @@ impl MetaPlugin for MetaPluginProgram {
|
||||
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);
|
||||
|
||||
// Store item ID for later use
|
||||
@@ -122,11 +122,13 @@ impl MetaPlugin for MetaPluginProgram {
|
||||
self.writer = Some(Box::new(stdin));
|
||||
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");
|
||||
let mut metadata = Vec::new();
|
||||
|
||||
if let Some(process) = self.process.take() {
|
||||
// Close stdin to signal end of input
|
||||
drop(self.writer.take());
|
||||
@@ -147,9 +149,11 @@ impl MetaPlugin for MetaPluginProgram {
|
||||
debug!("META: Program output: {}", 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 {
|
||||
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 {
|
||||
@@ -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 Err(e) = writer.write_all(data) {
|
||||
debug!("META: Failed to write to process stdin: {}", e);
|
||||
}
|
||||
}
|
||||
Ok(PluginResponse::default())
|
||||
}
|
||||
|
||||
fn meta_name(&self) -> String {
|
||||
|
||||
@@ -60,13 +60,14 @@ impl MetaPlugin for CwdMetaPlugin {
|
||||
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
|
||||
Ok(())
|
||||
Ok(PluginResponse::default())
|
||||
}
|
||||
|
||||
fn update(&mut self, _data: &[u8], _conn: &Connection) {
|
||||
fn update(&mut self, _data: &[u8]) -> Result<PluginResponse> {
|
||||
// No update needed
|
||||
Ok(PluginResponse::default())
|
||||
}
|
||||
|
||||
fn meta_name(&self) -> String {
|
||||
|
||||
Reference in New Issue
Block a user