refactor: remove connection storage from plugin structs and pass as argument
Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
@@ -11,7 +11,6 @@ pub struct MagicFileMetaPlugin {
|
||||
max_buffer_size: usize,
|
||||
is_saved: bool,
|
||||
item_id: Option<i64>,
|
||||
conn: Option<*mut Connection>,
|
||||
cookie: Option<Cookie>,
|
||||
output_names: std::collections::HashMap<String, String>,
|
||||
}
|
||||
@@ -23,7 +22,6 @@ impl MagicFileMetaPlugin {
|
||||
max_buffer_size: 4096, // Same as BinaryMetaPlugin
|
||||
is_saved: false,
|
||||
item_id: None,
|
||||
conn: None,
|
||||
cookie: None,
|
||||
output_names: std::collections::HashMap::new(),
|
||||
}
|
||||
@@ -56,15 +54,12 @@ impl MagicFileMetaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
fn save_all_magic_metadata(&mut self) -> Result<()> {
|
||||
if let (Some(conn), Some(item_id)) = (self.conn, self.item_id) {
|
||||
// Convert raw pointer back to reference (unsafe)
|
||||
let conn_ref = unsafe { &*conn };
|
||||
|
||||
fn save_all_magic_metadata(&mut self, conn: &Connection) -> Result<()> {
|
||||
if let Some(item_id) = self.item_id {
|
||||
// Only save MIME type since that's what's mapped in the config
|
||||
if let Ok(mime_type) = self.get_magic_result(CookieFlags::MIME_TYPE) {
|
||||
if !mime_type.is_empty() {
|
||||
let _ = output_metadata(conn_ref, item_id, "mime_type", mime_type, &self.output_names);
|
||||
let _ = output_metadata(conn, item_id, "mime_type", mime_type, &self.output_names);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,10 +74,8 @@ impl MetaPlugin for MagicFileMetaPlugin {
|
||||
true
|
||||
}
|
||||
|
||||
fn initialize(&mut self, conn: &Connection, item_id: i64) -> Result<()> {
|
||||
fn initialize(&mut self, _conn: &Connection, item_id: i64) -> Result<()> {
|
||||
self.item_id = Some(item_id);
|
||||
// Store raw pointer to connection - unsafe but necessary for this design
|
||||
self.conn = Some(conn as *const _ as *mut Connection);
|
||||
|
||||
// Initialize the magic cookie once
|
||||
let cookie = Cookie::open(Default::default())
|
||||
@@ -94,17 +87,17 @@ impl MetaPlugin for MagicFileMetaPlugin {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn finalize(&mut self) -> Result<()> {
|
||||
fn finalize(&mut self, conn: &Connection) -> Result<()> {
|
||||
// Save all magic metadata if not already saved
|
||||
if !self.is_saved {
|
||||
if let Err(e) = self.save_all_magic_metadata() {
|
||||
if let Err(e) = self.save_all_magic_metadata(conn) {
|
||||
eprintln!("Warning: Failed to save magic metadata: {}", e);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn update(&mut self, data: &[u8]) {
|
||||
fn update(&mut self, data: &[u8], conn: &Connection) {
|
||||
// Only collect up to max_buffer_size
|
||||
let remaining_capacity = self.max_buffer_size.saturating_sub(self.buffer.len());
|
||||
if remaining_capacity > 0 {
|
||||
@@ -113,7 +106,7 @@ impl MetaPlugin for MagicFileMetaPlugin {
|
||||
|
||||
// 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() {
|
||||
if let Err(e) = self.save_all_magic_metadata(conn) {
|
||||
eprintln!("Warning: Failed to save magic metadata early: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user