1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
 * Created on Wed Jul 12 2023
 *
 * Copyright (c) storycraft. Licensed under the Apache Licence 2.0.
 */

use std::{
    fs::File,
    io::{self, BufReader, Read, Seek, Write},
    path::Path,
};

use easy_ext::ext;
use path_slash::PathExt;
use walkdir::WalkDir;

use crate::SpxBuilder;

#[ext(StreamExt)]
pub impl<W: Write + Seek> SpxBuilder<W> {
    /// Write a file entry from [`Read`] stream
    fn write_stream(&mut self, name: String, mut stream: impl Read) -> io::Result<u64> {
        io::copy(&mut stream, &mut self.start_file(name)?)
    }
}

#[ext(FileExt)]
pub impl<W: Write + Seek> SpxBuilder<W> {

    /// Write every disk files from base directory
    fn from_dir(&mut self, base: impl AsRef<Path>) -> io::Result<u64> {
        let mut count = 0;

        for entry in WalkDir::new(&base) {
            let entry = entry?;
            if entry.file_type().is_dir() {
                continue;
            }

            let path = entry.path();
            let striped_path = entry.path().strip_prefix(&base).unwrap();

            self.write_stream(
                striped_path.to_slash_lossy().to_string(),
                BufReader::new(File::open(path)?),
            )?;
            count += 1;
        }

        Ok(count)
    }
}