refactor: extract magic type processing into helper function
Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
@@ -90,90 +90,79 @@ impl MagicFileMetaPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn save_all_magic_metadata(&mut self, conn: &Connection) -> Result<()> {
|
/// Helper function to process all magic types and collect metadata
|
||||||
if let Some(item_id) = self.item_id {
|
fn process_magic_types(&self, item_id: i64) -> Vec<crate::meta_plugin::MetaData> {
|
||||||
// Save all three magic outputs: mime_type, mime_encoding, and file_type
|
let mut metadata = Vec::new();
|
||||||
if let Ok(mime_type) = self.get_magic_result(CookieFlags::MIME_TYPE) {
|
|
||||||
if !mime_type.is_empty() {
|
// Define the types to process with their corresponding flags
|
||||||
let _ = self.save_meta(conn, item_id, "mime_type", mime_type);
|
let types_to_process = [
|
||||||
|
("mime_type", CookieFlags::MIME_TYPE),
|
||||||
|
("mime_encoding", CookieFlags::MIME_ENCODING),
|
||||||
|
("file_type", CookieFlags::default()),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (name, flags) in types_to_process {
|
||||||
|
if let Ok(result) = self.get_magic_result(flags) {
|
||||||
|
if !result.is_empty() {
|
||||||
|
// Use process_metadata_outputs to handle output mapping
|
||||||
|
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
||||||
|
name,
|
||||||
|
result,
|
||||||
|
&self.outputs
|
||||||
|
) {
|
||||||
|
metadata.push(meta_data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Ok(mime_encoding) = self.get_magic_result(CookieFlags::MIME_ENCODING) {
|
|
||||||
if !mime_encoding.is_empty() {
|
|
||||||
let _ = self.save_meta(conn, item_id, "mime_encoding", mime_encoding);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Ok(file_type) = self.get_magic_result(CookieFlags::default()) {
|
|
||||||
if !file_type.is_empty() {
|
|
||||||
let _ = self.save_meta(conn, item_id, "file_type", file_type);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
self.is_saved = true;
|
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
|
metadata
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MetaPlugin for MagicFileMetaPlugin {
|
impl MetaPlugin for MagicFileMetaPlugin {
|
||||||
|
|
||||||
fn initialize(&mut self, item_id: i64) -> Result<MetaPluginResponse> {
|
fn initialize(&mut self, item_id: i64) -> crate::meta_plugin::MetaPluginResponse {
|
||||||
self.item_id = Some(item_id);
|
self.item_id = Some(item_id);
|
||||||
|
|
||||||
// Initialize the magic cookie once
|
// Initialize the magic cookie once
|
||||||
let cookie = Cookie::open(Default::default())
|
let cookie = match Cookie::open(Default::default()) {
|
||||||
.map_err(|e| anyhow::anyhow!("Failed to open magic cookie: {}", e))?;
|
Ok(cookie) => cookie,
|
||||||
cookie.load(&[] as &[&str])
|
Err(e) => {
|
||||||
.map_err(|e| anyhow::anyhow!("Failed to load magic database: {}", e))?;
|
return crate::meta_plugin::MetaPluginResponse {
|
||||||
|
metadata: Vec::new(),
|
||||||
|
is_finalized: true,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if let Err(e) = cookie.load(&[] as &[&str]) {
|
||||||
|
return crate::meta_plugin::MetaPluginResponse {
|
||||||
|
metadata: Vec::new(),
|
||||||
|
is_finalized: true,
|
||||||
|
};
|
||||||
|
}
|
||||||
self.cookie = Some(cookie);
|
self.cookie = Some(cookie);
|
||||||
|
|
||||||
Ok(MetaPluginResponse::default())
|
crate::meta_plugin::MetaPluginResponse {
|
||||||
}
|
metadata: Vec::new(),
|
||||||
|
is_finalized: false,
|
||||||
fn finalize(&mut self) -> Result<MetaPluginResponse> {
|
|
||||||
let mut metadata = Vec::new();
|
|
||||||
|
|
||||||
// Save all magic metadata if not already saved
|
|
||||||
if let Some(item_id) = self.item_id {
|
|
||||||
if let Some(cookie) = &self.cookie {
|
|
||||||
// Get mime type
|
|
||||||
if let Ok(mime_type) = self.get_magic_result(CookieFlags::MIME_TYPE) {
|
|
||||||
if !mime_type.is_empty() {
|
|
||||||
if let Some(meta) = self.create_meta(item_id, "mime_type", mime_type) {
|
|
||||||
metadata.push(meta);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get mime encoding
|
|
||||||
if let Ok(mime_encoding) = self.get_magic_result(CookieFlags::MIME_ENCODING) {
|
|
||||||
if !mime_encoding.is_empty() {
|
|
||||||
if let Some(meta) = self.create_meta(item_id, "mime_encoding", mime_encoding) {
|
|
||||||
metadata.push(meta);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get file type
|
|
||||||
if let Ok(file_type) = self.get_magic_result(CookieFlags::default()) {
|
|
||||||
if !file_type.is_empty() {
|
|
||||||
if let Some(meta) = self.create_meta(item_id, "file_type", file_type) {
|
|
||||||
metadata.push(meta);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(MetaPluginResponse {
|
|
||||||
metadata: if metadata.is_empty() { None } else { Some(metadata) },
|
|
||||||
is_finalized: true,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(&mut self, data: &[u8]) -> Result<MetaPluginResponse> {
|
fn finalize(&mut self) -> crate::meta_plugin::MetaPluginResponse {
|
||||||
|
let metadata = if let Some(item_id) = self.item_id {
|
||||||
|
self.process_magic_types(item_id)
|
||||||
|
} else {
|
||||||
|
Vec::new()
|
||||||
|
};
|
||||||
|
|
||||||
|
crate::meta_plugin::MetaPluginResponse {
|
||||||
|
metadata,
|
||||||
|
is_finalized: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update(&mut self, data: &[u8]) -> crate::meta_plugin::MetaPluginResponse {
|
||||||
let mut metadata = Vec::new();
|
let mut metadata = Vec::new();
|
||||||
|
|
||||||
// Only collect up to max_buffer_size
|
// Only collect up to max_buffer_size
|
||||||
@@ -185,42 +174,15 @@ impl MetaPlugin for MagicFileMetaPlugin {
|
|||||||
// Check if we've reached our buffer limit and return metadata
|
// Check if we've reached our buffer limit and return metadata
|
||||||
if self.buffer.len() >= self.max_buffer_size {
|
if self.buffer.len() >= self.max_buffer_size {
|
||||||
if let Some(item_id) = self.item_id {
|
if let Some(item_id) = self.item_id {
|
||||||
if let Some(cookie) = &self.cookie {
|
metadata = self.process_magic_types(item_id);
|
||||||
// Get mime type
|
|
||||||
if let Ok(mime_type) = self.get_magic_result(CookieFlags::MIME_TYPE) {
|
|
||||||
if !mime_type.is_empty() {
|
|
||||||
if let Some(meta) = self.create_meta(item_id, "mime_type", mime_type) {
|
|
||||||
metadata.push(meta);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get mime encoding
|
|
||||||
if let Ok(mime_encoding) = self.get_magic_result(CookieFlags::MIME_ENCODING) {
|
|
||||||
if !mime_encoding.is_empty() {
|
|
||||||
if let Some(meta) = self.create_meta(item_id, "mime_encoding", mime_encoding) {
|
|
||||||
metadata.push(meta);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get file type
|
|
||||||
if let Ok(file_type) = self.get_magic_result(CookieFlags::default()) {
|
|
||||||
if !file_type.is_empty() {
|
|
||||||
if let Some(meta) = self.create_meta(item_id, "file_type", file_type) {
|
|
||||||
metadata.push(meta);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(MetaPluginResponse {
|
crate::meta_plugin::MetaPluginResponse {
|
||||||
metadata: if metadata.is_empty() { None } else { Some(metadata) },
|
metadata,
|
||||||
is_finalized: !metadata.is_empty(),
|
is_finalized: !metadata.is_empty(),
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn meta_name(&self) -> String {
|
fn meta_name(&self) -> String {
|
||||||
|
|||||||
Reference in New Issue
Block a user