use std::io::Read;
use std::path::Path;
const FILE_HEADER_PROBE_BYTES: usize = 64 * 1024;
fn read_file_header(path: &Path) -> Option<Vec<u8>> {
let file = std::fs::File::open(path).ok()?;
let mut buf = Vec::with_capacity(FILE_HEADER_PROBE_BYTES.min(8 * 1024));
file.take(FILE_HEADER_PROBE_BYTES as u64)
.read_to_end(&mut buf)
.ok()?;
Some(buf)
}
#[allow(dead_code)]
pub fn get_file_contents(entry: &crate::fs::DirEntry) -> String {
let cache_key =
crate::cmd::probe_cache::CacheKey::for_file(&entry.path, entry.size, entry.modified);
if let Some(cached) = crate::cmd::probe_cache::ProbeCache::get(&cache_key) {
return cached;
}
let ext = std::path::Path::new(&entry.name)
.extension()
.and_then(|e| e.to_str())
.unwrap_or("");
let result = match ext {
"png" | "jpg" | "jpeg" => read_file_header(&entry.path)
.and_then(|bytes| extract_image_resolution(&bytes, ext))
.unwrap_or_default(),
"zip" => read_file_header(&entry.path)
.and_then(|bytes| count_zip_entries(&bytes))
.map(|c| c.to_string())
.unwrap_or_default(),
"db" | "sqlite" | "sqlite3" => count_sqlite_tables(&entry.path)
.map(|c| format!("{}t", c))
.unwrap_or_default(),
"mp4" | "mov" | "m4v" | "webm" | "mkv" => {
extract_video_duration(&entry.path).unwrap_or_default()
}
_ => {
if entry.size < 1024 * 1024 {
if let Ok(content) = std::fs::read_to_string(&entry.path) {
return content.lines().count().to_string();
}
}
String::new()
}
};
crate::cmd::probe_cache::ProbeCache::put(cache_key, result.clone());
result
}
pub fn count_items_in_dir(entry: &crate::fs::DirEntry) -> usize {
let cache_key =
crate::cmd::probe_cache::CacheKey::for_dir(&entry.path, entry.size, entry.modified);
if let Some(cached) = crate::cmd::probe_cache::ProbeCache::get(&cache_key) {
if let Ok(n) = cached.parse::<usize>() {
return n;
}
}
let count = std::fs::read_dir(&entry.path)
.map(|d| d.count())
.unwrap_or(0);
crate::cmd::probe_cache::ProbeCache::put(cache_key, count.to_string());
count
}
fn extract_image_resolution(bytes: &[u8], ext: &str) -> Option<String> {
let ext = ext.strip_prefix('.').unwrap_or(ext);
if ext == "png" && bytes.len() >= 24 {
let w = u32::from_be_bytes([bytes[16], bytes[17], bytes[18], bytes[19]]) as usize;
let h = u32::from_be_bytes([bytes[20], bytes[21], bytes[22], bytes[23]]) as usize;
if w > 0 && h > 0 {
return Some(format!("{}x{}", w, h));
}
} else if ext == "jpg" || ext == "jpeg" {
let mut i = 2;
while i < bytes.len().saturating_sub(9) {
if bytes[i] == 0xFF
&& bytes[i + 1] >= 0xC0
&& bytes[i + 1] <= 0xCF
&& bytes[i + 1] != 0xC4
&& bytes[i + 1] != 0xC8
&& bytes[i + 1] != 0xCC
{
let h = ((bytes[i + 5] as usize) << 8) | (bytes[i + 6] as usize);
let w = ((bytes[i + 7] as usize) << 8) | (bytes[i + 8] as usize);
if w > 0 && h > 0 {
return Some(format!("{}x{}", w, h));
}
}
i += 1;
}
}
None
}
fn count_zip_entries(bytes: &[u8]) -> Option<usize> {
let mut count = 0;
let mut i = 0;
while i < bytes.len().saturating_sub(4) {
if bytes[i] == 0x50 && bytes[i + 1] == 0x4B && bytes[i + 2] == 0x03 && bytes[i + 3] == 0x04
{
count += 1;
i += 4;
} else {
i += 1;
}
}
if count > 0 {
Some(count)
} else {
None
}
}
fn count_sqlite_tables(path: &Path) -> Option<usize> {
let bytes = std::fs::read(path).ok()?;
if bytes.len() < 16 {
return None;
}
let header = std::str::from_utf8(&bytes[..16]).ok()?;
if !header.starts_with("SQLite format 3") {
return None;
}
use std::process::Command;
let output = Command::new("sqlite3")
.arg(path)
.arg("SELECT COUNT(*) FROM sqlite_master WHERE type='table';")
.output()
.ok()?;
let count = String::from_utf8_lossy(&output.stdout)
.trim()
.parse()
.ok()?;
Some(count)
}
fn extract_video_duration(path: &Path) -> Option<String> {
let buf = read_file_header(path)?;
parse_mp4_duration(&buf)
}
fn parse_mp4_duration(buf: &[u8]) -> Option<String> {
let mut i = 0;
while i < buf.len().saturating_sub(8) {
let size = u32::from_be_bytes([buf[i], buf[i + 1], buf[i + 2], buf[i + 3]]) as usize;
if size < 8 {
break;
}
if buf[i + 4] == 0x6D && buf[i + 5] == 0x6F && buf[i + 6] == 0x6F && buf[i + 7] == 0x76 {
let mut j = i + 8;
let moov_end = i + size;
while j < moov_end.saturating_sub(8) && j < buf.len().saturating_sub(8) {
let atom_size =
u32::from_be_bytes([buf[j], buf[j + 1], buf[j + 2], buf[j + 3]]) as usize;
if atom_size < 8 || atom_size > size {
break;
}
if buf[j + 4] == 0x6D
&& buf[j + 5] == 0x76
&& buf[j + 6] == 0x68
&& buf[j + 7] == 0x64
{
let version = buf[j + 8];
let (timescale, duration) = if version == 0 {
let ts = u32::from_be_bytes([
buf[j + 20],
buf[j + 21],
buf[j + 22],
buf[j + 23],
]);
let dur = u32::from_be_bytes([
buf[j + 24],
buf[j + 25],
buf[j + 26],
buf[j + 27],
]);
(ts as u64, dur as u64)
} else {
let ts = u32::from_be_bytes([
buf[j + 28],
buf[j + 29],
buf[j + 30],
buf[j + 31],
]);
let dur = u64::from_be_bytes([
buf[j + 32],
buf[j + 33],
buf[j + 34],
buf[j + 35],
buf[j + 36],
buf[j + 37],
buf[j + 38],
buf[j + 39],
]);
(ts as u64, dur)
};
if timescale > 0 && duration > 0 {
let seconds = duration / timescale;
let mins = seconds / 60;
let secs = seconds % 60;
if mins >= 60 {
let hours = mins / 60;
let mins = mins % 60;
return Some(format!("{}:{:02}:{:02}", hours, mins, secs));
} else if mins > 0 {
return Some(format!("{}:{:02}", mins, secs));
}
return Some(format!("{}s", seconds));
}
}
j += atom_size;
}
}
i += size;
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extract_image_resolution_png() {
let mut png = vec![0u8; 24];
png[0] = 0x89;
png[1] = 0x50; png[2] = 0x4E; png[3] = 0x47; png[4] = 0x0D;
png[5] = 0x0A;
png[6] = 0x1A;
png[7] = 0x0A;
png[16] = 0x00;
png[17] = 0x00;
png[18] = 0x07;
png[19] = 0x80;
png[20] = 0x00;
png[21] = 0x00;
png[22] = 0x04;
png[23] = 0x38;
let result = extract_image_resolution(&png, ".png");
assert_eq!(result, Some("1920x1080".to_string()));
}
#[test]
fn test_extract_image_resolution_too_short() {
let png = vec![0u8; 10];
let result = extract_image_resolution(&png, ".png");
assert_eq!(result, None);
}
#[test]
fn test_count_zip_entries() {
let mut zip = Vec::new();
for _ in 0..3 {
zip.extend_from_slice(&[0x50, 0x4B, 0x03, 0x04]); zip.extend_from_slice(&[0u8; 26]); }
let result = count_zip_entries(&zip);
assert_eq!(result, Some(3));
}
#[test]
fn test_count_zip_entries_empty() {
let zip = vec![0u8; 100];
let result = count_zip_entries(&zip);
assert_eq!(result, None);
}
#[test]
fn test_count_items_in_dir() {
let tmp = tempfile::tempdir().unwrap();
let dir_path = tmp.path();
std::fs::write(dir_path.join("file1.txt"), "a").unwrap();
std::fs::write(dir_path.join("file2.txt"), "b").unwrap();
std::fs::create_dir(dir_path.join("subdir")).unwrap();
let entry = crate::fs::DirEntry {
name: "test".to_string(),
path: dir_path.to_path_buf(),
is_dir: true,
is_file: false,
is_symlink: false,
is_exec: false,
size: 0,
modified: None,
perms: String::new(),
owner: String::new(),
group: String::new(),
symlink_target: None,
symlink_valid: true,
content_probe: None,
};
let count = count_items_in_dir(&entry);
assert_eq!(count, 3); }
#[test]
fn test_count_items_in_nonexistent_dir() {
let entry = crate::fs::DirEntry {
name: "nonexistent".to_string(),
path: "/tmp/nonexistent_dir_12345".into(),
is_dir: true,
is_file: false,
is_symlink: false,
is_exec: false,
size: 0,
modified: None,
perms: String::new(),
owner: String::new(),
group: String::new(),
symlink_target: None,
symlink_valid: true,
content_probe: None,
};
let count = count_items_in_dir(&entry);
assert_eq!(count, 0);
}
#[test]
fn test_get_file_contents_text() {
let tmp = tempfile::tempdir().unwrap();
let file_path = tmp.path().join("test.txt");
std::fs::write(&file_path, "line1\nline2\nline3\n").unwrap();
let entry = crate::fs::DirEntry {
name: "test.txt".to_string(),
path: file_path,
is_dir: false,
is_file: true,
is_symlink: false,
is_exec: false,
size: 18,
modified: None,
perms: String::new(),
owner: String::new(),
group: String::new(),
symlink_target: None,
symlink_valid: true,
content_probe: None,
};
let contents = get_file_contents(&entry);
assert_eq!(contents, "3"); }
#[test]
fn test_get_file_contents_empty() {
let entry = crate::fs::DirEntry {
name: "unknown.xyz".to_string(),
path: "/tmp/nonexistent".into(),
is_dir: false,
is_file: true,
is_symlink: false,
is_exec: false,
size: 0,
modified: None,
perms: String::new(),
owner: String::new(),
group: String::new(),
symlink_target: None,
symlink_valid: true,
content_probe: None,
};
let contents = get_file_contents(&entry);
assert_eq!(contents, "");
}
}