Skip to content

Commit

Permalink
Added recursive addition of resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcode66 committed Oct 11, 2021
1 parent 6691e7f commit b585988
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 9 deletions.
49 changes: 49 additions & 0 deletions backend/src/file_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ pub trait FileSource {
fn read_to_vec<P: AsRef<Path>>(&mut self, path: P) -> Result<Vec<u8>, Error>;
fn read_to_string<P: AsRef<Path>>(&mut self, path: P) -> Result<String, Error>;
fn open_image<P: AsRef<Path>>(&mut self, path: P) -> Result<DynamicImage, Error>;
fn exist<P: AsRef<Path>>(&mut self, path: P) -> Result<bool, Error>;
fn is_dir<P: AsRef<Path>>(&mut self, path: P) -> Result<bool, Error>;
fn is_file<P: AsRef<Path>>(&mut self, path: P) -> Result<bool, Error>;
fn get_names<P: AsRef<Path>>(&mut self, path: P) -> Result<Vec<String>, Error>;
}

pub struct FileSystem;
Expand All @@ -23,6 +27,25 @@ impl FileSource for FileSystem {
fn open_image<P: AsRef<Path>>(&mut self, path: P) -> Result<DynamicImage, Error> {
Ok(image::open(path)?)
}
fn exist<P: AsRef<Path>>(&mut self, path: P) -> Result<bool, Error> {
Ok(path.as_ref().exists())
}
fn is_dir<P: AsRef<Path>>(&mut self, path: P) -> Result<bool, Error> {
Ok(path.as_ref().is_dir())
}
fn is_file<P: AsRef<Path>>(&mut self, path: P) -> Result<bool, Error> {
Ok(path.as_ref().is_file())
}
fn get_names<P: AsRef<Path>>(&mut self, path: P) -> Result<Vec<String>, Error> {
let mut names: Vec<String> = Vec::new();
for entry in fs::read_dir(path)? {
let entry = entry?;
let entry_path = entry.path();
let file_name = entry_path.file_name().expect("Entry has no name");
names.push(String::from(file_name.to_str().unwrap()));
}
Ok(names)
}
}

impl<R: Read + Seek> FileSource for ZipArchive<R> {
Expand Down Expand Up @@ -50,4 +73,30 @@ impl<R: Read + Seek> FileSource for ZipArchive<R> {
let buf = self.read_to_vec(path)?;
Ok(image::load_from_memory(&buf)?)
}
fn exist<P: AsRef<Path>>(&mut self, path: P) -> Result<bool, Error> {
Ok(self.by_name(
path.as_ref()
.to_str()
.ok_or_else(|| err_msg("Invalid path"))?
).is_ok())
}
fn is_dir<P: AsRef<Path>>(&mut self, path: P) -> Result<bool, Error> {
let file = self.by_name(
path.as_ref()
.to_str()
.ok_or_else(|| err_msg("Invalid path"))?
)?;
Ok(file.is_dir())
}
fn is_file<P: AsRef<Path>>(&mut self, path: P) -> Result<bool, Error> {
let file = self.by_name(
path.as_ref()
.to_str()
.ok_or_else(|| err_msg("Invalid path"))?
)?;
Ok(file.is_file())
}
fn get_names<P: AsRef<Path>>(&mut self, _path: P) -> Result<Vec<String>, Error> {
Err(failure::err_msg("Unsupported operation on ZipArchive: get_names"))
}
}
28 changes: 19 additions & 9 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,15 +334,25 @@ fn add_file_to_iso<F: FileSource>(
iso: &mut Directory,
files: &mut F
) -> Result<(), Error> {
iso.resolve_and_create_path(&iso_path).data = files
.read_to_vec(actual_path)
.with_context(|_| {
format!(
"Couldn't read the file \"{}\" to store it in the ISO.",
actual_path.display()
)
})?
.into();
if files.is_dir(actual_path)? {
let names = files.get_names(actual_path)?;
for name in names {
let iso_path = String::from(iso_path) + &String::from('/') + &name;
let mut actual_path = actual_path.clone();
actual_path.push(name);
add_file_to_iso(&iso_path, &actual_path, iso, files)?;
}
} else {
iso.resolve_and_create_path(&iso_path).data = files
.read_to_vec(actual_path)
.with_context(|_| {
format!(
"Couldn't read the file \"{}\" to store it in the ISO.",
actual_path.display()
)
})?
.into();
}
Ok(())
}

Expand Down

0 comments on commit b585988

Please sign in to comment.