feat: implement core services and refactor modes
Co-authored-by: aider (openai/andrew/openrouter/google/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
100
src/modes/get.rs
100
src/modes/get.rs
@@ -1,13 +1,12 @@
|
||||
use anyhow::anyhow;
|
||||
use std::io::{Read, Write};
|
||||
use anyhow::{anyhow, Result};
|
||||
use std::io::{Write};
|
||||
|
||||
use crate::compression_engine::{CompressionType, get_compression_engine};
|
||||
use crate::common::is_binary::is_binary;
|
||||
use crate::config;
|
||||
use crate::core::item_service::ItemService;
|
||||
use clap::Command;
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
use is_terminal::IsTerminal;
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub fn mode_get(
|
||||
cmd: &mut Command,
|
||||
@@ -16,7 +15,7 @@ pub fn mode_get(
|
||||
tags: &mut Vec<String>,
|
||||
conn: &mut rusqlite::Connection,
|
||||
data_path: PathBuf,
|
||||
) -> anyhow::Result<()> {
|
||||
) -> Result<()> {
|
||||
if !ids.is_empty() && !tags.is_empty() {
|
||||
cmd.error(clap::error::ErrorKind::InvalidValue, "Both ID and tags given, you must supply exactly one ID or at least one tag when using --get").exit();
|
||||
} else if ids.len() > 1 {
|
||||
@@ -32,70 +31,39 @@ pub fn mode_get(
|
||||
}
|
||||
}
|
||||
|
||||
let item_maybe = match tags.is_empty() && meta.is_empty() {
|
||||
true => match ids.iter().next() {
|
||||
Some(item_id) => crate::db::get_item(conn, *item_id)?,
|
||||
None => crate::db::get_item_last(conn)?,
|
||||
},
|
||||
false => crate::db::get_item_matching(conn, tags, &meta)?,
|
||||
};
|
||||
let item_service = ItemService::new(data_path);
|
||||
let item_with_content =
|
||||
item_service.find_item(conn, ids, tags, &meta)
|
||||
.and_then(|item_with_meta| {
|
||||
let item_id = item_with_meta.item.id.unwrap();
|
||||
item_service.get_item_content(conn, item_id)
|
||||
})
|
||||
.map_err(|e| anyhow!("Unable to find matching item in database: {}", e))?;
|
||||
|
||||
if let Some(item) = item_maybe {
|
||||
let item_id = item.id.ok_or_else(|| anyhow!("Item missing ID"))?;
|
||||
// Validate that item ID is positive to prevent path traversal issues
|
||||
if item_id <= 0 {
|
||||
return Err(anyhow!("Invalid item ID: {}", item_id));
|
||||
}
|
||||
let content = &item_with_content.content;
|
||||
|
||||
let mut item_path = data_path.clone();
|
||||
item_path.push(item_id.to_string());
|
||||
// Determine if we should detect binary data
|
||||
let mut detect_binary = !settings.force && std::io::stdout().is_terminal();
|
||||
|
||||
// Determine if we should detect binary data
|
||||
let mut detect_binary = !settings.force && std::io::stdout().is_terminal();
|
||||
|
||||
// If we're detecting binary and there's binary metadata, check it
|
||||
if detect_binary {
|
||||
let item_meta = crate::db::get_item_meta(conn, &item)?;
|
||||
let binary_meta = item_meta.into_iter().find(|meta| meta.name == "binary");
|
||||
if let Some(binary_meta) = binary_meta {
|
||||
if binary_meta.value == "false" {
|
||||
// If metadata says it's not binary, don't detect
|
||||
detect_binary = false;
|
||||
} else if binary_meta.value == "true" {
|
||||
// If metadata says it's binary, error immediately
|
||||
return Err(anyhow!("Refusing to output binary data to TTY, use --force to override"));
|
||||
}
|
||||
if detect_binary {
|
||||
let meta_map = item_with_content.item_with_meta.meta_as_map();
|
||||
if let Some(binary_val) = meta_map.get("binary") {
|
||||
if binary_val == "false" {
|
||||
detect_binary = false;
|
||||
} else if binary_val == "true" {
|
||||
return Err(anyhow!(
|
||||
"Refusing to output binary data to TTY, use --force to override"
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let compression_type = CompressionType::from_str(&item.compression)?;
|
||||
let compression_engine = get_compression_engine(compression_type)?;
|
||||
|
||||
// If we need to detect binary, read first 4KB and check
|
||||
if detect_binary {
|
||||
// Open the file through compression engine to read first 4KB
|
||||
let mut reader = compression_engine.open(item_path.clone())?;
|
||||
let mut buffer = [0u8; 4096];
|
||||
let bytes_read = reader.read(&mut buffer)?;
|
||||
|
||||
// Check if this data is binary
|
||||
if is_binary(&buffer[..bytes_read]) {
|
||||
return Err(anyhow!("Refusing to output binary data to TTY, use --force to override"));
|
||||
}
|
||||
|
||||
// If not binary, output the data we've read
|
||||
std::io::stdout().write_all(&buffer[..bytes_read])?;
|
||||
|
||||
// Continue reading and outputting the rest of the data
|
||||
let mut stdout = std::io::stdout();
|
||||
std::io::copy(&mut reader, &mut stdout)?;
|
||||
} else {
|
||||
// No binary detection needed, just output the data
|
||||
compression_engine.cat(item_path.clone())?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!("Unable to find matching item in database"))
|
||||
}
|
||||
|
||||
if detect_binary && is_binary(content) {
|
||||
return Err(anyhow!(
|
||||
"Refusing to output binary data to TTY, use --force to override"
|
||||
));
|
||||
}
|
||||
|
||||
std::io::stdout().write_all(content)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user