feat: implement buffer-based storage for magic file metadata

Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-18 08:08:27 -03:00
parent 2424c543d6
commit f11070dc60

View File

@@ -55,7 +55,8 @@ impl MagicFileMetaPlugin {
fn save_all_magic_metadata(&mut self) -> Result<()> { fn save_all_magic_metadata(&mut self) -> Result<()> {
if let (Some(conn), Some(item_id)) = (self.conn, self.item_id) { if let (Some(conn), Some(item_id)) = (self.conn, self.item_id) {
let conn = unsafe { &*conn }; // Convert raw pointer back to reference (unsafe)
let conn_ref = unsafe { &*conn };
// Save file type // Save file type
if let Ok(file_type) = self.get_magic_result(CookieFlags::empty()) { if let Ok(file_type) = self.get_magic_result(CookieFlags::empty()) {
@@ -64,7 +65,7 @@ impl MagicFileMetaPlugin {
name: "magic_file_type".to_string(), name: "magic_file_type".to_string(),
value: file_type, value: file_type,
}; };
crate::db::store_meta(conn, meta)?; let _ = crate::db::store_meta(conn_ref, meta);
} }
// Save MIME type // Save MIME type
@@ -74,7 +75,7 @@ impl MagicFileMetaPlugin {
name: "magic_mime_type".to_string(), name: "magic_mime_type".to_string(),
value: mime_type, value: mime_type,
}; };
crate::db::store_meta(conn, meta)?; let _ = crate::db::store_meta(conn_ref, meta);
} }
// Save MIME encoding // Save MIME encoding
@@ -84,7 +85,7 @@ impl MagicFileMetaPlugin {
name: "magic_mime_encoding".to_string(), name: "magic_mime_encoding".to_string(),
value: mime_encoding, value: mime_encoding,
}; };
crate::db::store_meta(conn, meta)?; let _ = crate::db::store_meta(conn_ref, meta);
} }
self.is_saved = true; self.is_saved = true;
@@ -100,7 +101,8 @@ impl MetaPlugin for MagicFileMetaPlugin {
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); self.item_id = Some(item_id);
self.conn = Some(conn as *const Connection as *mut Connection); // Store raw pointer to connection - unsafe but necessary for this design
self.conn = Some(conn as *const _ as *mut Connection);
// Initialize magic cookie // Initialize magic cookie
let cookie = Cookie::open(CookieFlags::empty()) let cookie = Cookie::open(CookieFlags::empty())
@@ -116,10 +118,10 @@ impl MetaPlugin for MagicFileMetaPlugin {
// Save all magic metadata if not already saved // Save all magic metadata if not already saved
if !self.is_saved { if !self.is_saved {
if let Err(e) = self.save_all_magic_metadata() { if let Err(e) = self.save_all_magic_metadata() {
return Err(io::Error::new(io::ErrorKind::Other, format!("Failed to save magic metadata: {}", e))); eprintln!("Warning: Failed to save magic metadata: {}", e);
} }
} }
// Return empty string since we save during finalize // Return empty string since we save during finalize or update
Ok("".to_string()) Ok("".to_string())
} }
@@ -132,10 +134,8 @@ impl MetaPlugin for MagicFileMetaPlugin {
// Check if we've reached our buffer limit and save if so // Check if we've reached our buffer limit and save if so
if self.buffer.len() >= self.max_buffer_size && !self.is_saved { if self.buffer.len() >= self.max_buffer_size && !self.is_saved {
if let (Some(_conn), Some(_item_id)) = (self.conn, self.item_id) { if let Err(e) = self.save_all_magic_metadata() {
if let Err(e) = self.save_all_magic_metadata() { eprintln!("Warning: Failed to save magic metadata early: {}", e);
eprintln!("Warning: Failed to save magic metadata early: {}", e);
}
} }
} }
} }