From 1b6ff4431226fe9108a392be974076fb923116aa Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Mon, 18 Aug 2025 08:50:01 -0300 Subject: [PATCH] fix: correct magic file type and mime encoding detection Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) --- src/meta_plugin/magic.rs | 46 +++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/src/meta_plugin/magic.rs b/src/meta_plugin/magic.rs index 3b2925a..9406e8b 100644 --- a/src/meta_plugin/magic.rs +++ b/src/meta_plugin/magic.rs @@ -28,29 +28,28 @@ impl MagicFileMetaPlugin { } fn get_magic_result(&self, flags: CookieFlags) -> io::Result { - if let Some(ref cookie) = self.cookie { - let result = cookie.buffer(&self.buffer) - .map_err(|e| io::Error::new(io::ErrorKind::Other, format!("Failed to analyze buffer: {}", e)))?; + // Create a new cookie with the specific flags for this request + let cookie = Cookie::open(flags) + .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 trimmed = result.trim(); - - // 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 { - trimmed - }; + let result = cookie.buffer(&self.buffer) + .map_err(|e| io::Error::new(io::ErrorKind::Other, format!("Failed to analyze buffer: {}", e)))?; - Ok(cleaned.to_string()) + // Clean up the result - remove extra whitespace and take first part if needed + let trimmed = result.trim(); + + // 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 { - Err(io::Error::new( - io::ErrorKind::Other, - "Magic cookie not initialized".to_string(), - )) - } + trimmed + }; + + Ok(cleaned.to_string()) } 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 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(()) }