1
0
Fork 0
mirror of https://github.com/lise-henry/crowbook synced 2024-05-28 14:06:29 +02:00

zipper now create directory when needs to be

This commit is contained in:
Elisabeth Henry 2016-02-20 00:28:42 +01:00
parent 91d99ca9a6
commit 48bd6db5e4
3 changed files with 14 additions and 10 deletions

View File

@ -38,7 +38,7 @@ impl<'a> EpubRenderer<'a> {
/// Render a book
pub fn render_book(&mut self) -> Result<String> {
let mut zipper = try!(Zipper::new(&self.book.temp_dir, &["META-INF"]));
let mut zipper = try!(Zipper::new(&self.book.temp_dir));
// Write mimetype
try!(zipper.write("mimetype", b"application/epub+zip"));

View File

@ -29,7 +29,7 @@ impl<'a> LatexRenderer<'a> {
let base_file = try!(Path::new(pdf_file).file_stem().ok_or(Error::Render("could not stem pdf filename")));
let tex_file = format!("{}.tex", base_file.to_str().unwrap());
let content = try!(self.render_book());
let mut zipper = try!(Zipper::new(&self.book.temp_dir, &[]));
let mut zipper = try!(Zipper::new(&self.book.temp_dir));
try!(zipper.write(&tex_file, &content.as_bytes()));
zipper.generate_pdf(&self.book.tex_command, &tex_file, pdf_file)
} else {

View File

@ -19,7 +19,7 @@ impl Zipper {
///
/// path: the path to a temporary directory (zipper will create a random dir in it and clean it later)
/// inner_dirs: a vec of inner directory to create in this directory
pub fn new(path: &str, inner_dirs: &[&str]) -> Result<Zipper> {
pub fn new(path: &str) -> Result<Zipper> {
let uuid = uuid::Uuid::new_v4();
let zipper_path = Path::new(path).join(uuid.to_simple_string());
@ -28,12 +28,6 @@ impl Zipper {
.create(&zipper_path)
.map_err(|_| Error::Zipper(format!("could not create temporary directory in {}", path))));
for inner in inner_dirs {
try!(DirBuilder::new()
.recursive(true)
.create(zipper_path.join(inner))
.map_err(|_| Error::Zipper(format!("could not create temporary inner directory {}", inner))));
}
Ok(Zipper {
args: vec!(),
path: zipper_path,
@ -42,7 +36,17 @@ impl Zipper {
/// writes a content to a temporary file
pub fn write(&mut self, file: &str, content: &[u8]) -> Result<()> {
if let Ok(mut f) = File::create(self.path.join(file)) {
let dest_file = self.path.join(file);
let dest_dir = dest_file.parent().expect("This file should have a parent, it has just been joined to a directory!");
if !fs::metadata(dest_dir).is_ok() { // dir does not exist, create it
try!(DirBuilder::new()
.recursive(true)
.create(&dest_dir)
.map_err(|_| Error::Zipper(format!("could not create temporary directory in {}", dest_dir.display()))));
}
if let Ok(mut f) = File::create(&dest_file) {
if f.write_all(content).is_ok() {
self.args.push(String::from(file));
Ok(())