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,40 +82,62 @@ impl MetaPlugin for BinaryMetaPlugin {
|
|||||||
true
|
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
|
// 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 we've already saved the metadata, no need to collect more data
|
|
||||||
if self.is_saved {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 {
|
||||||
// Determine how much data to copy
|
// Determine how much data to copy
|
||||||
let bytes_to_take = std::cmp::min(data.len(), remaining_capacity);
|
let bytes_to_take = std::cmp::min(data.len(), remaining_capacity);
|
||||||
|
|
||||||
// Add data to our buffer
|
// Add data to our buffer
|
||||||
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<()> {
|
||||||
|
|||||||
@@ -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());
|
||||||
}
|
}
|
||||||
@@ -251,16 +251,37 @@ impl MetaPlugin for ReadRateMetaPlugin {
|
|||||||
fn is_internal(&self) -> bool {
|
fn is_internal(&self) -> bool {
|
||||||
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 {
|
||||||
|
|||||||
@@ -122,43 +122,108 @@ 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
|
||||||
let cookie = Cookie::open(Default::default())
|
let cookie = Cookie::open(Default::default())
|
||||||
.map_err(|e| anyhow::anyhow!("Failed to open magic cookie: {}", e))?;
|
.map_err(|e| anyhow::anyhow!("Failed to open magic cookie: {}", e))?;
|
||||||
cookie.load(&[] as &[&str])
|
cookie.load(&[] as &[&str])
|
||||||
.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 {
|
||||||
|
|||||||
@@ -95,9 +95,9 @@ 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
|
||||||
self.item_id = Some(item_id);
|
self.item_id = Some(item_id);
|
||||||
|
|
||||||
@@ -122,18 +122,20 @@ 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());
|
||||||
|
|
||||||
// Wait for the process to complete
|
// Wait for the process to complete
|
||||||
let output = process.wait_with_output()?;
|
let output = process.wait_with_output()?;
|
||||||
|
|
||||||
if output.status.success() {
|
if output.status.success() {
|
||||||
// Process the output
|
// Process the output
|
||||||
let output_str = String::from_utf8_lossy(&output.stdout);
|
let output_str = String::from_utf8_lossy(&output.stdout);
|
||||||
@@ -142,14 +144,16 @@ impl MetaPlugin for MetaPluginProgram {
|
|||||||
} else {
|
} else {
|
||||||
output_str.trim().to_string()
|
output_str.trim().to_string()
|
||||||
};
|
};
|
||||||
|
|
||||||
if !result.is_empty() {
|
if !result.is_empty() {
|
||||||
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 {
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user