feat: initialize cookie once and use set_flags for subsequent operations
Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
@@ -28,28 +28,30 @@ impl MagicFileMetaPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn get_magic_result(&self, flags: CookieFlags) -> io::Result<String> {
|
fn get_magic_result(&self, flags: CookieFlags) -> io::Result<String> {
|
||||||
// Create a new cookie with the specific flags for this request
|
// Use the existing cookie and just change flags
|
||||||
let cookie = Cookie::open(flags)
|
if let Some(cookie) = &self.cookie {
|
||||||
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("Failed to open magic cookie: {}", e)))?;
|
cookie.set_flags(flags)
|
||||||
cookie.load(&[] as &[&str])
|
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("Failed to set magic flags: {}", e)))?;
|
||||||
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("Failed to load magic database: {}", e)))?;
|
|
||||||
|
|
||||||
let result = cookie.buffer(&self.buffer)
|
let result = cookie.buffer(&self.buffer)
|
||||||
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("Failed to analyze buffer: {}", e)))?;
|
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("Failed to analyze buffer: {}", e)))?;
|
||||||
|
|
||||||
// Clean up the result - remove extra whitespace and take first part if needed
|
// Clean up the result - remove extra whitespace and take first part if needed
|
||||||
let trimmed = result.trim();
|
let trimmed = result.trim();
|
||||||
|
|
||||||
// For some magic results, we might want just the first part before semicolon or comma
|
// For some magic results, we might want just the first part before semicolon or comma
|
||||||
let cleaned = if trimmed.contains(';') {
|
let cleaned = if trimmed.contains(';') {
|
||||||
trimmed.split(';').next().unwrap_or(trimmed).trim()
|
trimmed.split(';').next().unwrap_or(trimmed).trim()
|
||||||
} else if trimmed.contains(',') && flags.contains(CookieFlags::MIME_TYPE | CookieFlags::MIME_ENCODING) {
|
} else if trimmed.contains(',') && flags.contains(CookieFlags::MIME_TYPE | CookieFlags::MIME_ENCODING) {
|
||||||
trimmed.split(',').next().unwrap_or(trimmed).trim()
|
trimmed.split(',').next().unwrap_or(trimmed).trim()
|
||||||
|
} else {
|
||||||
|
trimmed
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(cleaned.to_string())
|
||||||
} else {
|
} else {
|
||||||
trimmed
|
Err(io::Error::new(io::ErrorKind::Other, "Magic cookie not initialized"))
|
||||||
};
|
}
|
||||||
|
|
||||||
Ok(cleaned.to_string())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn save_all_magic_metadata(&mut self) -> Result<()> {
|
fn save_all_magic_metadata(&mut self) -> Result<()> {
|
||||||
@@ -109,6 +111,13 @@ impl MetaPlugin for MagicFileMetaPlugin {
|
|||||||
// Store raw pointer to connection - unsafe but necessary for this design
|
// Store raw pointer to connection - unsafe but necessary for this design
|
||||||
self.conn = Some(conn as *const _ as *mut Connection);
|
self.conn = Some(conn as *const _ as *mut Connection);
|
||||||
|
|
||||||
|
// Initialize the magic cookie once
|
||||||
|
let cookie = Cookie::open(Default::default())
|
||||||
|
.map_err(|e| anyhow::anyhow!("Failed to open magic cookie: {}", e))?;
|
||||||
|
cookie.load(&[] as &[&str])
|
||||||
|
.map_err(|e| anyhow::anyhow!("Failed to load magic database: {}", e))?;
|
||||||
|
self.cookie = Some(cookie);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user