use crate::Result;
use crate::core::config::ExtractionConfig;
use crate::core::config::extraction::FileExtractionConfig;
use crate::types::ExtractionResult;
#[cfg(feature = "tokio-runtime")]
use std::path::{Path, PathBuf};
#[cfg(feature = "tokio-runtime")]
use once_cell::sync::OnceCell;
#[cfg(feature = "tokio-runtime")]
use super::batch::{batch_extract_bytes, batch_extract_file};
#[cfg(feature = "tokio-runtime")]
use super::bytes::extract_bytes;
#[cfg(feature = "tokio-runtime")]
use super::file::extract_file;
#[cfg(not(feature = "tokio-runtime"))]
use super::helpers::error_extraction_result;
#[cfg(feature = "tokio-runtime")]
static GLOBAL_RUNTIME: OnceCell<tokio::runtime::Runtime> = OnceCell::new();
#[cfg(feature = "tokio-runtime")]
fn global_runtime() -> crate::Result<&'static tokio::runtime::Runtime> {
GLOBAL_RUNTIME.get_or_try_init(|| {
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.map_err(|e| crate::KreuzbergError::Plugin {
message: format!("Failed to create global Tokio runtime: {e}"),
plugin_name: "runtime".to_string(),
})
})
}
#[cfg(feature = "tokio-runtime")]
pub fn extract_file_sync(
path: impl AsRef<Path>,
mime_type: Option<&str>,
config: &ExtractionConfig,
) -> Result<ExtractionResult> {
global_runtime()?.block_on(extract_file(path, mime_type, config))
}
#[cfg(feature = "tokio-runtime")]
pub fn extract_bytes_sync(content: &[u8], mime_type: &str, config: &ExtractionConfig) -> Result<ExtractionResult> {
global_runtime()?.block_on(extract_bytes(content, mime_type, config))
}
#[cfg(not(feature = "tokio-runtime"))]
pub fn extract_bytes_sync(content: &[u8], mime_type: &str, config: &ExtractionConfig) -> Result<ExtractionResult> {
super::legacy::extract_bytes_sync_impl(content, Some(mime_type), Some(config))
}
#[cfg(feature = "tokio-runtime")]
pub fn batch_extract_file_sync(
items: Vec<(PathBuf, Option<FileExtractionConfig>)>,
config: &ExtractionConfig,
) -> Result<Vec<ExtractionResult>> {
global_runtime()?.block_on(batch_extract_file(items, config))
}
#[cfg(feature = "tokio-runtime")]
pub fn batch_extract_bytes_sync(
items: Vec<(Vec<u8>, String, Option<FileExtractionConfig>)>,
config: &ExtractionConfig,
) -> Result<Vec<ExtractionResult>> {
global_runtime()?.block_on(batch_extract_bytes(items, config))
}
#[cfg(not(feature = "tokio-runtime"))]
pub fn batch_extract_bytes_sync(
items: Vec<(Vec<u8>, String, Option<FileExtractionConfig>)>,
config: &ExtractionConfig,
) -> Result<Vec<ExtractionResult>> {
let mut results = Vec::with_capacity(items.len());
for (content, mime_type, file_config) in items {
let resolved = match &file_config {
Some(fc) => config.with_file_overrides(fc),
None => config.clone(),
};
let result = extract_bytes_sync(&content, &mime_type, &resolved);
results.push(result.unwrap_or_else(|e| error_extraction_result(&e, None)));
}
Ok(results)
}