Skip to content

Commit

Permalink
Merge pull request #63 from instaclustr/io-directory-fix
Browse files Browse the repository at this point in the history
Refactor IO Directory Processing and Compression Logic
  • Loading branch information
cjrolo authored Oct 31, 2023
2 parents 09f5182 + 6560a4d commit f93ab82
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 122 deletions.
96 changes: 30 additions & 66 deletions brro-compressor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use brro_compressor::utils::writers::wav_writer;
use clap::{arg, command, Parser};
use log::{debug, error};
use std::error::Error;
use std::path::{Path, PathBuf};
use std::path::PathBuf;

/// Processes the given input based on the provided arguments.
fn process_args(arguments: &Args) -> Result<(), Box<dyn Error>> {
Expand All @@ -16,7 +16,7 @@ fn process_args(arguments: &Args) -> Result<(), Box<dyn Error>> {
// If the input path points to a single file
if metadata.is_file() {
debug!("Target is a file");
process_single_file(&arguments.input, arguments)?;
process_single_file(arguments.input.clone(), arguments)?;
}
// If the input path points to a directory
else if metadata.is_dir() {
Expand All @@ -30,84 +30,48 @@ fn process_args(arguments: &Args) -> Result<(), Box<dyn Error>> {

Ok(())
}

/// Processes all files in a given directory.
fn process_directory(arguments: &Args) -> Result<(), Box<dyn Error>> {
if arguments.uncompress {
let file_name = arguments
.input
.file_name()
.ok_or("Failed to retrieve file name.")?;
let new_name = format!("{}-decompressed", file_name.to_string_lossy());
let base_dir = arguments.input.with_file_name(new_name);

std::fs::create_dir_all(&base_dir)?;
//read

// TODO: This should be calling `process_single_file` and avoid code duplication
for file in bro_reader::dir_reader(&arguments.input)? {
//decompress
let decompressed_data = decompress_data(&file.contents);
//write
let path = base_dir.join(
file.original_path
.file_name()
.ok_or("path has no file name")?,
);
// TODO: Decompression shouldn't optimize the WAV
wav_writer::write_optimal_wav(path, decompressed_data, 1);
}
Ok(())
} else {
let file_name = arguments
.input
.file_name()
.ok_or("Failed to retrieve file name.")?;
let new_name = format!("{}-compressed", file_name.to_string_lossy());
let base_dir = arguments.input.with_file_name(new_name);

std::fs::create_dir_all(&base_dir)?;

//read
for file in wav_reader::dir_reader(&arguments.input)? {
let compressed_data = compress_data(&file.contents, &file.tag, arguments);
let mut path = base_dir.join(
file.original_path
.file_name()
.ok_or("path has no file name")?,
);
path.set_extension("bro");
std::fs::write(path, compressed_data)?;
// Assuming you want to process each file inside this directory
for entry in std::fs::read_dir(arguments.input.clone())? {
let path = entry?.path();
if path.is_file() {
process_single_file(path, arguments)?;
}
Ok(())
}
Ok(())
}

/// Processes a single file.
fn process_single_file(file_path: &Path, arguments: &Args) -> Result<(), Box<dyn Error>> {
fn process_single_file(mut file_path: PathBuf, arguments: &Args) -> Result<(), Box<dyn Error>> {
debug!("Processing single file...");
if arguments.uncompress {
//read
let vec = bro_reader::read_file(&arguments.input)?;
let arr: &[u8] = &vec;
//decompress
let decompressed_data = decompress_data(arr);
if arguments.verbose {
println!("Output={:?}", decompressed_data);
if let Some(vec) = bro_reader::read_file(&file_path)?{
let arr: &[u8] = &vec;
//decompress
let decompressed_data = decompress_data(arr);
if arguments.verbose {
println!("Output={:?}", decompressed_data);
}
// TODO: Decompression shouldn't optimize the WAV
wav_writer::write_optimal_wav(file_path, decompressed_data, 1);
}
wav_writer::write_optimal_wav(arguments.input.clone(), decompressed_data, 1);
} else {
//read
let (vec, tag) = wav_reader::read_file(file_path)?;
if arguments.verbose {
println!("Input={:?}", vec);
}
//compress
let compressed_data = compress_data(&vec, &tag, arguments);
if let Some(data) = wav_reader::read_file(&file_path)? {
let (vec, tag) = data;
if arguments.verbose {
println!("Input={:?}", vec);
}
//compress
let compressed_data = compress_data(&vec, &tag, arguments);

//write
let mut path = arguments.input.clone();
path.set_extension("bro");
std::fs::write(path, compressed_data)?;
//write
file_path.set_extension("bro");
std::fs::write(file_path, compressed_data)?;
}
}
Ok(())
}
Expand Down
30 changes: 4 additions & 26 deletions brro-compressor/src/utils/readers/bro_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::fs;
use std::fs::File;
use std::io::{self, Error, Read};
use std::path::{Path, PathBuf};
use std::path::Path;

// Function to process a WAV file
fn process_bro_file(file_path: &Path) -> io::Result<Vec<u8>> {
Expand All @@ -12,34 +12,12 @@ fn process_bro_file(file_path: &Path) -> io::Result<Vec<u8>> {
Ok(contents)
}

pub struct BroFile {
pub contents: Vec<u8>,
pub original_path: PathBuf,
}

// Function to read and process files in a directory
pub fn dir_reader(directory_path: &Path) -> io::Result<Vec<BroFile>> {
let mut files = vec![];

// Iterate through entries (files and subdirectories) in the given directory
for entry in fs::read_dir(directory_path)? {
let file_path = entry?.path();

files.push(BroFile {
contents: read_file(&file_path)?,
original_path: file_path,
})
}

Ok(files)
}

pub fn read_file(file_path: &Path) -> Result<Vec<u8>, Error> {
pub fn read_file(file_path: &Path) -> Result<Option<Vec<u8>>, Error> {
if is_bro_file(file_path)? {
// If it's a WAV file, process it using the process_wav_file function
Ok(process_bro_file(file_path)?)
Ok(Some(process_bro_file(file_path)?))
} else {
Err(Error::new(io::ErrorKind::Other, "File is not a bro file"))
Ok(None)
}
}
fn is_bro_file(file_path: &Path) -> io::Result<bool> {
Expand Down
34 changes: 7 additions & 27 deletions brro-compressor/src/utils/readers/wav_reader.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Implement a streaming reader here
use std::fs;
use std::io::{self, Error, Read};
use std::path::{Path, PathBuf};
use std::path::Path;
use log::debug;
use regex::Regex;
use types::metric_tag::MetricTag;
Expand All @@ -20,25 +20,20 @@ fn is_wav_file(file_path: &Path) -> io::Result<bool> {
}

// Function to process a WAV file
fn process_wav_file(file_path: &Path) -> io::Result<(Vec<f64>, MetricTag)> {
fn process_wav_file(file_path: &Path) -> io::Result<Option<(Vec<f64>, MetricTag)>> {
let full_path_str = file_path.to_str().unwrap_or("");
debug!("File: {} ,", full_path_str);
let wav_data = read_metrics_from_wav(full_path_str);
debug!("Data Len: {}", wav_data.len());
// Depending on Metric Tag, apply a transformation
let tag = tag_metric(full_path_str);
Ok((wav_data, tag))
Ok(Some((wav_data, tag)))
}

// Function to process a RAW file
fn process_raw_file(file_path: &Path) -> io::Result<(Vec<f64>, MetricTag)> {
todo!("Handle RAW file processing here (for example, decoding or encoding): {file_path:?}");
}

pub struct WavFile {
pub contents: Vec<f64>,
pub tag: MetricTag,
pub original_path: PathBuf,
fn process_raw_file(file_path: &Path) -> io::Result<Option<(Vec<f64>, MetricTag)>> {
log::error!("Raw files not supported yet. File path: {:?}", file_path.display());
Ok(None)
}

/// Read a file by chunks and processes the chunks
Expand All @@ -59,23 +54,8 @@ pub fn process_by_chunk(file_path: &Path) -> Result<(), std::io::Error> {
Ok(())
}

// Function to read and process files in a directory
pub fn dir_reader(directory_path: &Path) -> io::Result<Vec<WavFile>> {
let mut files = vec!();
for entry in fs::read_dir(directory_path)? {
let file_path = entry?.path();

let (contents, tag) = read_file(&file_path)?;
files.push(WavFile {
contents,
tag,
original_path: file_path,
})
}
Ok(files)
}

pub fn read_file(file_path: &Path) -> Result<(Vec<f64>, MetricTag), Error> {
pub fn read_file(file_path: &Path) -> Result<Option<(Vec<f64>, MetricTag)>, Error> {
if is_wav_file(file_path)? {
// If it's a WAV file, process it using the process_wav_file function
process_wav_file(file_path)
Expand Down
5 changes: 2 additions & 3 deletions brro-compressor/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ fn test_suite(compressor: &str) {
fn compress_dir(compressor: &str) {
let tmp_dir = tempdir().unwrap();
let input = tmp_dir.path().join("input");
let output = tmp_dir.path().join("input-compressed");
std::fs::create_dir(&input).unwrap();
std::fs::copy("tests/wavs/memory_used.wav", input.join("1.wav")).unwrap();
std::fs::copy("tests/wavs/uptime.wav", input.join("2.wav")).unwrap();

run_compressor(&[input.to_str().unwrap(), "--compressor", compressor]);
assert!(output.join("1.bro").is_file());
assert!(output.join("2.bro").is_file());
assert!(input.join("1.bro").is_file());
assert!(input.join("2.bro").is_file());
}

fn compress_file(compressor: &str) {
Expand Down

0 comments on commit f93ab82

Please sign in to comment.