Skip to content

Commit

Permalink
New file matching feature whe reading bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
CHRISCARLON committed Sep 18, 2024
1 parent 022ed6c commit abf1d88
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nebby"
version = "0.1.1"
version = "0.1.2"
edition = "2021"

[[bin]]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ Christopher Carlon

## Version

0.1.1
0.1.2
46 changes: 42 additions & 4 deletions src/bytes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,53 @@
use reqwest::blocking::get;
use std::io::Read;

pub fn view_bytes(url: &str) -> Result<[u8; 100], Box<dyn std::error::Error>> {
#[derive(Debug)]
pub enum FileType {
PDF,
PNG,
JPEG,
GIF,
ZIP,
XLSX,
DOCX,
Unknown,
}

pub fn view_bytes(url: &str) -> Result<([u8; 100], FileType), Box<dyn std::error::Error>> {
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",
}
}
10 changes: 5 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -109,16 +109,16 @@ fn process_excel_with_header(
fn process_view_bytes(url: &str) -> Result<(), Box<dyn std::error::Error>> {
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);
if (i + 1) % 16 == 0 {
println!();
}
}
println!();
println!("\nDetected file type: {}", get_file_type_string(&file_type));
Ok(())
}

Expand Down

0 comments on commit abf1d88

Please sign in to comment.