collect hashes and file types

This commit is contained in:
🪞👃🪞 2025-03-03 21:05:18 +02:00
parent 01bc1b7b47
commit 747abab922
5 changed files with 183 additions and 31 deletions

View file

@ -1,14 +1,21 @@
#![feature(os_str_display)]
use tek_tui::*;
use tek_tui::tek_input::*;
use tek_tui::tek_output::*;
use crate::crossterm::event::{Event, KeyEvent, KeyCode, KeyModifiers, KeyEventState, KeyEventKind};
use clap::{arg, command, value_parser, ArgAction, Command};
use walkdir::WalkDir;
use std::sync::{Arc, RwLock};
use std::path::{Path, PathBuf};
use std::env::current_dir;
use std::fs::read;
use tek_tui::*;
use tek_tui::tek_output::*;
use tek_tui::tek_input::*;
use crate::crossterm::event::{Event, KeyEvent, KeyCode, KeyModifiers, KeyEventState, KeyEventKind};
use clap::{arg, command, value_parser, ArgAction, Command};
use walkdir::WalkDir;
use sha2::{Sha256, Digest};
use xxhash_rust::xxh3::xxh3_64;
use base64::prelude::*;
use file_type::FileType;
mod keys;
mod view;
@ -25,20 +32,24 @@ fn cli () -> clap::Command {
.arg(arg!([path] "Path to root directory").value_parser(value_parser!(PathBuf)))
}
struct Taggart {
root: PathBuf,
paths: Vec<Entry>,
cursor: usize,
offset: usize,
column: usize,
size: Measure<TuiOut>,
root: PathBuf,
paths: Vec<Entry>,
cursor: usize,
offset: usize,
column: usize,
size: Measure<TuiOut>,
editing: Option<(usize, usize)>,
show_hash: bool,
}
#[derive(Ord, Eq, PartialEq, PartialOrd, Default)]
struct Entry {
path: PathBuf,
is_dir: bool,
is_mus: bool,
is_img: bool,
depth: usize,
path: PathBuf,
is_dir: bool,
is_mus: bool,
is_img: bool,
depth: usize,
hash: Option<String>,
file_type: Option<&'static FileType>,
}
fn main () -> Usually<()> {
@ -55,6 +66,18 @@ impl Taggart {
} else {
current_dir()?
};
Ok(Self {
paths: Self::collect(&root)?,
root,
cursor: 0,
offset: 0,
column: 0,
size: Measure::new(),
editing: None,
show_hash: false,
})
}
fn collect (root: &impl AsRef<Path>) -> Usually<Vec<Entry>> {
let mut paths = vec![];
for entry in WalkDir::new(&root)
.into_iter()
@ -70,28 +93,36 @@ impl Taggart {
}
let depth = entry.depth();
let path = entry.into_path();
let (is_dir, is_mus, is_img) = if path.is_dir() {
(true, false, false)
let (is_dir, is_mus, is_img, hash, file_type) = if path.is_dir() {
(true, false, false, None, None)
} else {
(false, false, false)
let bytes = read(&path)?;
let hash = hex::encode(xxh3_64(&bytes).to_be_bytes());
let file_type = FileType::try_from_reader(&*bytes)?;
let mime_type = file_type.media_types().get(0);
let is_mus = match mime_type {
Some(&"audio/mpeg3") => true,
_ => false,
};
let is_img = match mime_type {
Some(&"image/png") => true,
_ => false,
};
println!("{hash} {file_type:?} ({}b)\n{}\n", bytes.len(), path.display());
(false, is_mus, is_img, Some(hash), Some(file_type))
};
paths.push(Entry {
path: path.strip_prefix(&root)?.into(),
is_dir: path.is_dir(),
is_mus: false,
is_img: false,
depth
depth,
hash,
file_type
});
}
paths.sort();
Ok(Self {
root,
paths,
cursor: 0,
offset: 0,
column: 0,
size: Measure::new(),
})
Ok(paths)
}
}