feat: add fallback to file command when magic crate is disabled
Co-authored-by: aider (openai/andrew/openrouter/sonoma-sky-alpha) <aider@aider.chat>
This commit is contained in:
@@ -1,19 +1,27 @@
|
|||||||
|
#[cfg(feature = "magic")]
|
||||||
use magic::{Cookie, CookieFlags};
|
use magic::{Cookie, CookieFlags};
|
||||||
use std::io;
|
#[cfg(not(feature = "magic"))]
|
||||||
|
use std::process::{Command, Stdio};
|
||||||
|
use std::io::{self, Write};
|
||||||
|
#[cfg(not(feature = "magic"))]
|
||||||
|
use which::which;
|
||||||
|
use log::debug;
|
||||||
|
|
||||||
use crate::common::PIPESIZE;
|
use crate::common::PIPESIZE;
|
||||||
|
|
||||||
use crate::meta_plugin::{MetaPlugin, MetaPluginType};
|
use crate::meta_plugin::{MetaPlugin, MetaPluginType, BaseMetaPlugin, MetaPluginResponse, MetaData};
|
||||||
|
|
||||||
|
#[cfg(feature = "magic")]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct MagicFileMetaPlugin {
|
pub struct MagicFileMetaPlugin {
|
||||||
buffer: Vec<u8>,
|
buffer: Vec<u8>,
|
||||||
max_buffer_size: usize,
|
max_buffer_size: usize,
|
||||||
is_finalized: bool,
|
is_finalized: bool,
|
||||||
cookie: Option<Cookie>,
|
cookie: Option<Cookie>,
|
||||||
base: crate::meta_plugin::BaseMetaPlugin,
|
base: BaseMetaPlugin,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "magic")]
|
||||||
impl MagicFileMetaPlugin {
|
impl MagicFileMetaPlugin {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
options: Option<std::collections::HashMap<String, serde_yaml::Value>>,
|
options: Option<std::collections::HashMap<String, serde_yaml::Value>>,
|
||||||
@@ -49,7 +57,7 @@ impl MagicFileMetaPlugin {
|
|||||||
final_options.insert("max_buffer_size".to_string(), serde_yaml::Value::Number(PIPESIZE.into()));
|
final_options.insert("max_buffer_size".to_string(), serde_yaml::Value::Number(PIPESIZE.into()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut base = crate::meta_plugin::BaseMetaPlugin::new();
|
let mut base = BaseMetaPlugin::new();
|
||||||
base.outputs = final_outputs;
|
base.outputs = final_outputs;
|
||||||
base.options = final_options;
|
base.options = final_options;
|
||||||
|
|
||||||
@@ -86,159 +94,4 @@ impl MagicFileMetaPlugin {
|
|||||||
|
|
||||||
Ok(cleaned.to_string())
|
Ok(cleaned.to_string())
|
||||||
} else {
|
} else {
|
||||||
Err(io::Error::other("Magic cookie not initialized"))
|
Err(io::Error::other("Magic cookie not
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Helper function to process all magic types and collect metadata
|
|
||||||
fn process_magic_types(&self) -> Vec<crate::meta_plugin::MetaData> {
|
|
||||||
let mut metadata = Vec::new();
|
|
||||||
|
|
||||||
// Define the types to process with their corresponding flags
|
|
||||||
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.iter() {
|
|
||||||
if let Ok(result) = self.get_magic_result(*flags)
|
|
||||||
&& !result.is_empty() {
|
|
||||||
// Use process_metadata_outputs to handle output mapping
|
|
||||||
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
|
||||||
name,
|
|
||||||
serde_yaml::Value::String(result),
|
|
||||||
self.base.outputs()
|
|
||||||
) {
|
|
||||||
metadata.push(meta_data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
metadata
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MetaPlugin for MagicFileMetaPlugin {
|
|
||||||
fn is_finalized(&self) -> bool {
|
|
||||||
self.is_finalized
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_finalized(&mut self, finalized: bool) {
|
|
||||||
self.is_finalized = finalized;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn initialize(&mut self) -> crate::meta_plugin::MetaPluginResponse {
|
|
||||||
// Initialize the magic cookie once
|
|
||||||
let cookie = match Cookie::open(Default::default()) {
|
|
||||||
Ok(cookie) => cookie,
|
|
||||||
Err(_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);
|
|
||||||
|
|
||||||
crate::meta_plugin::MetaPluginResponse {
|
|
||||||
metadata: Vec::new(),
|
|
||||||
is_finalized: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn finalize(&mut self) -> crate::meta_plugin::MetaPluginResponse {
|
|
||||||
// If already finalized, don't process again
|
|
||||||
if self.is_finalized {
|
|
||||||
return crate::meta_plugin::MetaPluginResponse {
|
|
||||||
metadata: Vec::new(),
|
|
||||||
is_finalized: true,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
let metadata = self.process_magic_types();
|
|
||||||
|
|
||||||
// Mark as finalized
|
|
||||||
self.is_finalized = true;
|
|
||||||
|
|
||||||
crate::meta_plugin::MetaPluginResponse {
|
|
||||||
metadata,
|
|
||||||
is_finalized: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn update(&mut self, data: &[u8]) -> crate::meta_plugin::MetaPluginResponse {
|
|
||||||
// If already finalized, don't process more data
|
|
||||||
if self.is_finalized {
|
|
||||||
return crate::meta_plugin::MetaPluginResponse {
|
|
||||||
metadata: Vec::new(),
|
|
||||||
is_finalized: true,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut metadata = Vec::new();
|
|
||||||
|
|
||||||
// Only collect up to max_buffer_size
|
|
||||||
let remaining_capacity = self.max_buffer_size.saturating_sub(self.buffer.len());
|
|
||||||
if remaining_capacity > 0 {
|
|
||||||
let bytes_to_copy = std::cmp::min(data.len(), remaining_capacity);
|
|
||||||
self.buffer.extend_from_slice(&data[..bytes_to_copy]);
|
|
||||||
|
|
||||||
// Check if we've reached our buffer limit and return metadata
|
|
||||||
if self.buffer.len() >= self.max_buffer_size {
|
|
||||||
metadata = self.process_magic_types();
|
|
||||||
|
|
||||||
// Mark as finalized when we've processed enough data
|
|
||||||
self.is_finalized = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let is_finalized = !metadata.is_empty();
|
|
||||||
crate::meta_plugin::MetaPluginResponse {
|
|
||||||
metadata,
|
|
||||||
is_finalized,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn meta_type(&self) -> MetaPluginType {
|
|
||||||
MetaPluginType::MagicFile
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
|
|
||||||
self.base.outputs()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
|
|
||||||
self.base.outputs_mut()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn default_outputs(&self) -> Vec<String> {
|
|
||||||
vec!["mime_type".to_string(), "mime_encoding".to_string(), "file_type".to_string()]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn options(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
|
|
||||||
self.base.options()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
|
|
||||||
self.base.options_mut()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
use crate::meta_plugin::register_meta_plugin;
|
|
||||||
|
|
||||||
// Register the plugin at module initialization time
|
|
||||||
#[ctor::ctor]
|
|
||||||
fn register_magic_file_plugin() {
|
|
||||||
register_meta_plugin(MetaPluginType::MagicFile, |options, outputs| {
|
|
||||||
Box::new(MagicFileMetaPlugin::new(options, outputs))
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@@ -34,6 +34,9 @@ pub use shell_pid::ShellPidMetaPlugin;
|
|||||||
pub use keep_pid::KeepPidMetaPlugin;
|
pub use keep_pid::KeepPidMetaPlugin;
|
||||||
pub use env::EnvMetaPlugin;
|
pub use env::EnvMetaPlugin;
|
||||||
|
|
||||||
|
#[cfg(not(feature = "magic"))]
|
||||||
|
pub use magic_file::FallbackMagicFileMetaPlugin as MagicFileMetaPlugin;
|
||||||
|
|
||||||
type PluginConstructor = fn(Option<HashMap<String, serde_yaml::Value>>, Option<HashMap<String, serde_yaml::Value>>) -> Box<dyn MetaPlugin>;
|
type PluginConstructor = fn(Option<HashMap<String, serde_yaml::Value>>, Option<HashMap<String, serde_yaml::Value>>) -> Box<dyn MetaPlugin>;
|
||||||
|
|
||||||
/// Represents metadata to be stored.
|
/// Represents metadata to be stored.
|
||||||
@@ -191,7 +194,6 @@ impl MetaPlugin for BaseMetaPlugin {
|
|||||||
#[derive(Debug, Eq, PartialEq, Clone, Hash, strum::EnumIter, strum::Display, strum::EnumString, Serialize, Deserialize)]
|
#[derive(Debug, Eq, PartialEq, Clone, Hash, strum::EnumIter, strum::Display, strum::EnumString, Serialize, Deserialize)]
|
||||||
#[strum(serialize_all = "snake_case", ascii_case_insensitive)]
|
#[strum(serialize_all = "snake_case", ascii_case_insensitive)]
|
||||||
pub enum MetaPluginType {
|
pub enum MetaPluginType {
|
||||||
#[cfg(feature = "magic")]
|
|
||||||
MagicFile,
|
MagicFile,
|
||||||
Cwd,
|
Cwd,
|
||||||
Text,
|
Text,
|
||||||
|
|||||||
Reference in New Issue
Block a user