rustybit_lib/storage/storage_manager/
file_metadata.rs

1use std::ops::Deref;
2use std::path::Path;
3
4use crate::parser::Info;
5use crate::storage::FileInfo;
6
7#[derive(Debug)]
8pub struct TorrentFileMetadata {
9    pub file_infos: Vec<FileInfo>,
10}
11
12impl TorrentFileMetadata {
13    pub fn new(info: &mut Info, base_path: &Path) -> anyhow::Result<Self> {
14        let mut file_infos = Vec::with_capacity(1);
15        if let Some(files) = info.files.as_deref() {
16            file_infos.reserve(files.len());
17            for file in files.iter() {
18                let mut path = base_path.to_path_buf();
19                file.path.iter().for_each(|path_part| {
20                    path.push(path_part.deref());
21                });
22
23                file_infos.push(FileInfo::new(path, file.length));
24            }
25        } else {
26            let mut path = base_path.to_path_buf();
27            path.push(&*info.name);
28
29            let length = info.length.ok_or_else(|| {
30                anyhow::anyhow!(
31                    "Error while starting up a torrent: the `length` field is missing a single-file download mode",
32                )
33            })?;
34
35            file_infos.push(FileInfo::new(path, length));
36        };
37
38        Ok(TorrentFileMetadata { file_infos })
39    }
40}