fix: correct magic file type and mime encoding detection

Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-18 08:50:01 -03:00
parent 1e4e039672
commit 1b6ff44312

View File

@@ -28,29 +28,28 @@ impl MagicFileMetaPlugin {
} }
fn get_magic_result(&self, flags: CookieFlags) -> io::Result<String> { fn get_magic_result(&self, flags: CookieFlags) -> io::Result<String> {
if let Some(ref cookie) = self.cookie { // Create a new cookie with the specific flags for this request
let result = cookie.buffer(&self.buffer) let cookie = Cookie::open(flags)
.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 open magic cookie: {}", e)))?;
cookie.load(&[] as &[&str])
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("Failed to load magic database: {}", e)))?;
// Clean up the result - remove extra whitespace and take first part if needed let result = cookie.buffer(&self.buffer)
let trimmed = result.trim(); .map_err(|e| io::Error::new(io::ErrorKind::Other, format!("Failed to analyze buffer: {}", e)))?;
// For some magic results, we might want just the first part before semicolon or comma // Clean up the result - remove extra whitespace and take first part if needed
let cleaned = if trimmed.contains(';') { let trimmed = result.trim();
trimmed.split(';').next().unwrap_or(trimmed).trim()
} else if trimmed.contains(',') && flags.contains(CookieFlags::MIME_TYPE | CookieFlags::MIME_ENCODING) {
trimmed.split(',').next().unwrap_or(trimmed).trim()
} else {
trimmed
};
Ok(cleaned.to_string()) // For some magic results, we might want just the first part before semicolon or comma
let cleaned = if trimmed.contains(';') {
trimmed.split(';').next().unwrap_or(trimmed).trim()
} else if trimmed.contains(',') && flags.contains(CookieFlags::MIME_TYPE | CookieFlags::MIME_ENCODING) {
trimmed.split(',').next().unwrap_or(trimmed).trim()
} else { } else {
Err(io::Error::new( trimmed
io::ErrorKind::Other, };
"Magic cookie not initialized".to_string(),
)) Ok(cleaned.to_string())
}
} }
fn save_all_magic_metadata(&mut self) -> Result<()> { fn save_all_magic_metadata(&mut self) -> Result<()> {
@@ -122,13 +121,6 @@ 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 magic cookie with flags for comprehensive detection
let cookie = Cookie::open(CookieFlags::MIME_TYPE | CookieFlags::MIME_ENCODING | CookieFlags::MIME)
.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(())
} }