gitload/
lib.rs

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use std::{
    fs::{create_dir_all, File},
    io::Write,
    path::PathBuf,
    sync::{Arc, Mutex},
};

use anyhow::{anyhow, Result};
use tokio::sync::broadcast::{channel, Sender};

#[derive(serde::Deserialize, Debug)]
struct Node {
    path: String,
    size: Option<isize>,
}

#[derive(serde::Deserialize, Debug)]
struct FileTree {
    tree: Vec<Node>,
}

#[derive(Clone, Copy, Debug)]
pub struct Process {
    pub current: usize,
    pub all: usize,
}

impl Process {
    fn new(n: usize) -> Arc<Mutex<Self>> {
        let this = Self { current: 0, all: n };
        Arc::new(Mutex::new(this))
    }

    fn deep_clone(&self) -> Self {
        Self {
            current: self.current,
            all: self.all,
        }
    }

    fn done(&mut self) {
        self.current += 1;
    }

    pub fn percent(&self) -> f64 {
        self.current as f64 / self.all as f64
    }

    pub fn is_over(&self) -> bool {
        self.current == self.all
    }
}

macro_rules! send_if_err {
    ($tx: expr,$result: expr) => {
        if let Err(err) = $result {
            $tx.send(Err(err.to_string())).unwrap();
            return;
        }
        $result.unwrap()
    };
}

impl FileTree {
    async fn download(&self, downloader: Arc<Downloader>, tx: Sender<Result<Process, String>>) {
        let tasks: Vec<_> = self
            .tree
            .iter()
            .filter(|node| node.size.is_some())
            .map(|node| Arc::new(PathBuf::from(&node.path)))
            .filter(|path| {
                let src = PathBuf::from(&downloader.remote_path);
                path.starts_with(src)
            })
            .collect();
        let process = Process::new(tasks.len());
        tasks.iter().for_each(|path| {
            let src = PathBuf::from(&downloader.remote_path);
            let dst = PathBuf::from(&downloader.local_path);
            let path = path.clone();
            let tx = tx.clone();
            let downloader = downloader.clone();
            let process = process.clone();
            tokio::spawn(async move {
                let dst = dst.join(path.strip_prefix(&src).unwrap());
                let dst_dir = dst.parent().unwrap();
                create_dir_all(dst_dir).unwrap();
                send_if_err!(
                    tx,
                    downloader
                        .download_single(path.as_os_str().to_str().unwrap(), dst.to_str().unwrap())
                        .await
                );
                let mut lock = process.lock().unwrap();
                lock.done();
                let process = lock.deep_clone();
                tx.send(Ok(process)).unwrap();
            });
        });
    }
}

pub struct Downloader {
    user: String,
    repo: String,
    branch: String,
    remote_path: String,
    local_path: String,
    process_handler: fn(Process),
}

impl Downloader {
    const USER_AGENT:&'static str="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.5410.0 Safari/537.36";

    async fn download_single(&self, path: &str, dst: &str) -> Result<()> {
        let url = format!(
            "https://raw.githubusercontent.com/{}/{}/{}/{path}",
            &self.user, &self.repo, &self.branch
        );
        let client = reqwest::ClientBuilder::new()
            .user_agent(Self::USER_AGENT)
            .build()?;
        let res = client.get(url).send().await?.text().await?;
        let mut file = File::create(dst)?;
        file.write_all(res.as_bytes())?;
        Ok(())
    }

    pub async fn download(self) -> Result<()> {
        let url = format!(
            "https://api.github.com/repos/{}/{}/git/trees/{}?recursive=1",
            &self.user, &self.repo, &self.branch
        );
        let client = reqwest::ClientBuilder::new()
            .user_agent(Self::USER_AGENT)
            .build()?;
        let res = client.get(url).send().await?.text().await?;
        let file_tree: FileTree = serde_json::from_str(&res)?;

        let (tx, mut rx) = channel::<Result<Process, String>>(5);

        let me = Arc::new(self);

        file_tree.download(me.clone(), tx).await;

        loop {
            let process = rx.recv().await?.map_err(|err| anyhow!(err))?;
            (me.process_handler)(process);
            if process.is_over() {
                return Ok(());
            }
        }
    }
}

#[derive(Default)]
pub struct DownloaderBuilder {
    user: String,
    repo: String,
    branch: Option<String>,
    remote_path: String,
    local_path: Option<String>,
    process_handler: Option<fn(Process)>,
}

impl DownloaderBuilder {
    pub fn new(user: &str, repo: &str, remote: &str) -> Self {
        Self {
            user: user.into(),
            repo: repo.into(),
            remote_path: remote.into(),
            ..Default::default()
        }
    }

    pub fn branch(mut self, branch: &str) -> Self {
        self.branch = Some(branch.into());
        self
    }

    pub fn local_path(mut self, local: &str) -> Self {
        self.local_path = Some(local.into());
        self
    }

    pub fn on_process(mut self, f: fn(Process)) -> Self {
        self.process_handler = Some(f);
        self
    }

    pub fn build(self) -> Downloader {
        let path = PathBuf::from(&self.remote_path);
        let name = path.file_name().unwrap().to_str().unwrap().to_string();
        Downloader {
            user: self.user,
            repo: self.repo,
            remote_path: self.remote_path,
            branch: self.branch.unwrap_or("main".into()),
            local_path: self.local_path.unwrap_or(name),
            process_handler: self.process_handler.unwrap_or(|_| {}),
        }
    }
}