Skip to main content

brows3_lib/transfer/
mod.rs

1pub mod download;
2pub mod manager;
3mod speed;
4
5pub use manager::TransferManager;
6
7use chrono::Utc;
8use serde::{Deserialize, Serialize};
9use std::path::PathBuf;
10use uuid::Uuid;
11
12#[derive(Debug, Clone, Serialize, Deserialize)]
13pub enum TransferType {
14    Upload,
15    Download,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
19pub enum TransferStatus {
20    Pending,
21    InProgress,
22    Completed,
23    Failed(String),
24    Paused,
25    Cancelled,
26}
27
28#[derive(Debug, Clone, Serialize, Deserialize)]
29pub struct TransferJob {
30    pub id: String,
31    pub profile_id: String,
32    pub bucket: String,
33    pub bucket_region: Option<String>,
34    pub key: String,
35    pub local_path: String,
36    #[serde(skip)]
37    pub download_destination: Option<std::sync::Arc<download::DownloadDestination>>,
38    pub transfer_type: TransferType,
39    pub status: TransferStatus,
40    pub total_bytes: u64,
41    pub processed_bytes: u64,
42    #[serde(default)]
43    pub bytes_per_second: f64,
44    #[serde(skip)]
45    pub progress: std::sync::Arc<std::sync::Mutex<speed::Progress>>,
46    pub created_at: i64,          // Timestamp (ms)
47    pub finished_at: Option<i64>, // Timestamp (ms)
48    // Grouping fields
49    pub parent_group_id: Option<String>,
50    pub group_name: Option<String>,
51    pub is_group_root: bool,
52}
53
54#[derive(Debug, Clone, Serialize, Deserialize)]
55pub struct TransferEvent {
56    pub job_id: String,
57    pub bytes_per_second: f64,
58    pub processed_bytes: u64,
59    pub total_bytes: u64,
60    pub status: TransferStatus,
61    pub finished_at: Option<i64>,
62}
63
64impl TransferJob {
65    pub fn new(
66        transfer_type: TransferType,
67        profile_id: String,
68        bucket: String,
69        bucket_region: Option<String>,
70        key: String,
71        local_path: PathBuf,
72        total_bytes: u64,
73    ) -> Self {
74        Self {
75            id: Uuid::new_v4().to_string(),
76            profile_id,
77            bucket,
78            bucket_region,
79            key,
80            local_path: local_path.to_string_lossy().to_string(),
81            download_destination: None,
82            transfer_type,
83            status: TransferStatus::Pending,
84            total_bytes,
85            processed_bytes: 0,
86            bytes_per_second: 0.0,
87            progress: Default::default(),
88            created_at: Utc::now().timestamp_millis(),
89            finished_at: None,
90            parent_group_id: None,
91            group_name: None,
92            is_group_root: false,
93        }
94    }
95
96    pub fn with_group(mut self, group_id: String, name: String) -> Self {
97        self.parent_group_id = Some(group_id);
98        self.group_name = Some(name);
99        self
100    }
101}
102
103#[cfg(test)]
104mod tests {
105    use super::{TransferJob, TransferType};
106    use std::path::PathBuf;
107
108    #[test]
109    fn transfer_job_keeps_the_profile_that_queued_it() {
110        let job = TransferJob::new(
111            TransferType::Download,
112            "profile-a".to_string(),
113            "bucket".to_string(),
114            Some("ap-southeast-2".to_string()),
115            "folder/file.txt".to_string(),
116            PathBuf::from("/tmp/file.txt"),
117            42,
118        );
119
120        assert_eq!(job.profile_id, "profile-a");
121    }
122}