From abf1d88e1e59a2bd497123c6954914af4a581f82 Mon Sep 17 00:00:00 2001 From: christophercarlon Date: Wed, 18 Sep 2024 21:44:29 +0100 Subject: [PATCH] New file matching feature whe reading bytes --- Cargo.toml | 2 +- README.md | 2 +- src/bytes/mod.rs | 46 ++++++++++++++++++++++++++++++++++++++++++---- src/main.rs | 10 +++++----- 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b7c763f..ad3b474 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nebby" -version = "0.1.1" +version = "0.1.2" edition = "2021" [[bin]] diff --git a/README.md b/README.md index 41c9879..6af24fd 100644 --- a/README.md +++ b/README.md @@ -61,4 +61,4 @@ Christopher Carlon ## Version -0.1.1 +0.1.2 diff --git a/src/bytes/mod.rs b/src/bytes/mod.rs index cdeb288..3ba88c9 100644 --- a/src/bytes/mod.rs +++ b/src/bytes/mod.rs @@ -1,15 +1,53 @@ use reqwest::blocking::get; use std::io::Read; -pub fn view_bytes(url: &str) -> Result<[u8; 100], Box> { +#[derive(Debug)] +pub enum FileType { + PDF, + PNG, + JPEG, + GIF, + ZIP, + XLSX, + DOCX, + Unknown, +} + +pub fn view_bytes(url: &str) -> Result<([u8; 100], FileType), Box> { let response = get(url)?; let mut buffer = [0u8; 100]; let bytes_read = response.take(100).read(&mut buffer)?; - if bytes_read < 100 { - // If less than 100 bytes were read, fill the rest with zeros buffer[bytes_read..].fill(0); } + let file_type = identify_file_type(&buffer); + Ok((buffer, file_type)) +} + +fn identify_file_type(bytes: &[u8]) -> FileType { + match bytes { + [0x25, 0x50, 0x44, 0x46, ..] => FileType::PDF, + [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, ..] => FileType::PNG, + [0xFF, 0xD8, 0xFF, ..] => FileType::JPEG, + [0x47, 0x49, 0x46, 0x38, ..] => FileType::GIF, + [0x50, 0x4B, 0x03, 0x04, rest @ ..] => match rest { + [0x14, 0x00, 0x06, 0x00, ..] => FileType::XLSX, + [0x14, 0x00, 0x08, 0x00, ..] => FileType::DOCX, + _ => FileType::ZIP, + }, + _ => FileType::Unknown, + } +} - Ok(buffer) +pub fn get_file_type_string(file_type: &FileType) -> &'static str { + match file_type { + FileType::PDF => "PDF", + FileType::PNG => "PNG", + FileType::JPEG => "JPEG", + FileType::GIF => "GIF", + FileType::ZIP => "ZIP", + FileType::XLSX => "Excel (XLSX)", + FileType::DOCX => "Word (DOCX)", + FileType::Unknown => "Unknown", + } } diff --git a/src/main.rs b/src/main.rs index 669e573..2d90bcb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ mod bytes; mod excel; mod utils; use api::simple_api_get_reqwest; -use bytes::view_bytes; +use bytes::{get_file_type_string, view_bytes}; use clap::{Parser, Subcommand}; use excel::{ analyze_excel_formatting, display_remote_basic_info, @@ -12,7 +12,7 @@ use excel::{ use utils::create_progress_bar; #[derive(Parser, Debug)] -#[command(author = "Christopher Carlon", version = "0.1.1", about = "Nebby - quickly review basic information about remote xlsx files and API GET requests", long_about = None)] +#[command(author = "Christopher Carlon", version = "0.1.2", about = "Nebby - quickly review basic information about remote xlsx files and API GET requests", long_about = None)] struct Cli { #[command(subcommand)] command: Commands, @@ -109,8 +109,8 @@ fn process_excel_with_header( fn process_view_bytes(url: &str) -> Result<(), Box> { validate_url(url)?; let pb = create_progress_bar("Viewing first 100 bytes..."); - let bytes = view_bytes(url)?; - pb.finish_with_message("Bytes Processed"); + let (bytes, file_type) = view_bytes(url)?; + pb.finish_and_clear(); println!("First 100 bytes:"); for (i, byte) in bytes.iter().enumerate() { print!("{:02X} ", byte); @@ -118,7 +118,7 @@ fn process_view_bytes(url: &str) -> Result<(), Box> { println!(); } } - println!(); + println!("\nDetected file type: {}", get_file_type_string(&file_type)); Ok(()) }