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
204
205
206
207
208
209
210
use std::io::Read;
use std::time::Instant;
use std::{
    fs::{File, OpenOptions},
    io::{Seek, SeekFrom, Write},
    path::PathBuf,
};

use curl::easy::{Handler, ReadError, WriteError};
use log::trace;
use tokio::sync::mpsc::Sender;

/// This is an information about the transfer(Download/Upload) speed that will be sent across tasks.
/// It is useful to get the transfer speed and displayed it according to
/// user's application.
#[derive(Clone, Debug)]
pub struct TransferSpeed(f64);

impl TransferSpeed {
    pub fn as_bytes_per_sec(&self) -> u64 {
        self.0 as u64
    }
}

impl From<u64> for TransferSpeed {
    fn from(value: u64) -> Self {
        Self(value as f64)
    }
}

impl From<usize> for TransferSpeed {
    fn from(value: usize) -> Self {
        Self(value as f64)
    }
}

impl From<i32> for TransferSpeed {
    fn from(value: i32) -> Self {
        Self(value as f64)
    }
}

impl From<i64> for TransferSpeed {
    fn from(value: i64) -> Self {
        Self(value as f64)
    }
}

impl From<f64> for TransferSpeed {
    fn from(value: f64) -> Self {
        Self(value)
    }
}
/// Stores the path for the downloaded file or the uploaded file.
/// Internally it will also monitor the bytes transferred and the Download/Upload speed.
#[derive(Clone, Debug)]
pub struct FileInfo {
    /// File path to download or file path of the source file to be uploaded.
    pub path: PathBuf,
    /// Sends the transfer speed information via channel to another task.
    /// This is an optional parameter depends on the user application.
    send_speed_info: Option<Sender<TransferSpeed>>,
    bytes_transferred: usize,
    transfer_started: Instant,
    transfer_speed: TransferSpeed,
}

impl FileInfo {
    /// Sets the destination file path to download or file path of the source file to be uploaded.
    pub fn path(path: PathBuf) -> Self {
        Self {
            path,
            send_speed_info: None,
            bytes_transferred: 0,
            transfer_started: Instant::now(),
            transfer_speed: TransferSpeed::from(0),
        }
    }

    /// Sets the FileInfo struct with a message passing channel to send transfer speed information across user applications.
    /// It uses a tokio bounded channel to send the information across tasks.
    pub fn with_transfer_speed_sender(mut self, send_speed_info: Sender<TransferSpeed>) -> Self {
        self.send_speed_info = Some(send_speed_info);
        self
    }

    fn update_bytes_transferred(&mut self, transferred: usize) {
        self.bytes_transferred += transferred;

        let now = Instant::now();
        let difference = now.duration_since(self.transfer_started);

        self.transfer_speed =
            TransferSpeed::from((self.bytes_transferred) as f64 / difference.as_secs_f64());
    }

    fn bytes_transferred(&self) -> usize {
        self.bytes_transferred
    }

    fn transfer_speed(&self) -> TransferSpeed {
        self.transfer_speed.clone()
    }
}

fn send_transfer_info(info: &FileInfo) {
    if let Some(tx) = info.send_speed_info.clone() {
        let transfer_speed = info.transfer_speed();
        tokio::spawn(async move {
            tx.send(transfer_speed).await.map_err(|e| {
                trace!("{:?}", e);
            })
        });
    }
}

/// This is an extended trait for the curl::easy::Handler trait.
pub trait ExtendedHandler: Handler {
    // Return the response body if the Collector if available.
    fn get_response_body(&self) -> Option<Vec<u8>> {
        None
    }
}

/// The Collector will handle two types in order to store data, via File or via RAM.
/// Collector::File(FileInfo) is useful to be able to download and upload files.
/// Collector::Ram(`Vec<u8>`) is used to store response body into Memory.
#[derive(Clone, Debug)]
pub enum Collector {
    /// Collector::File(FileInfo) is useful to be able to download and upload files.
    File(FileInfo),
    /// Collector::Ram(`Vec<u8>`) is used to store response body into Memory.
    Ram(Vec<u8>),
}

impl Handler for Collector {
    /// This will store the response from the server
    /// to the data vector or into a file depends on the
    /// Collector being used.
    fn write(&mut self, data: &[u8]) -> Result<usize, WriteError> {
        match self {
            Collector::File(info) => {
                let mut file = OpenOptions::new()
                    .create(true)
                    .append(true)
                    .open(info.path.clone())
                    .map_err(|e| {
                        trace!("{}", e);
                        WriteError::Pause
                    })?;

                file.write_all(data).map_err(|e| {
                    trace!("{}", e);
                    WriteError::Pause
                })?;

                info.update_bytes_transferred(data.len());

                send_transfer_info(info);
                Ok(data.len())
            }
            Collector::Ram(container) => {
                container.extend_from_slice(data);
                Ok(data.len())
            }
        }
    }
    /// This will read the chunks of data from a file that will be uploaded
    /// to the server. This will be use if the Collector is Collector::File(FileInfo).
    fn read(&mut self, data: &mut [u8]) -> Result<usize, ReadError> {
        match self {
            Collector::File(info) => {
                let mut file = File::open(info.path.clone()).map_err(|e| {
                    trace!("{}", e);
                    ReadError::Abort
                })?;

                file.seek(SeekFrom::Start(info.bytes_transferred() as u64))
                    .map_err(|e| {
                        trace!("{}", e);
                        ReadError::Abort
                    })?;

                let read_size = file.read(data).map_err(|e| {
                    trace!("{}", e);
                    ReadError::Abort
                })?;

                info.update_bytes_transferred(read_size);

                send_transfer_info(info);
                Ok(read_size)
            }
            Collector::Ram(_) => Ok(0),
        }
    }
}

impl ExtendedHandler for Collector {
    /// If Collector::File(FileInfo) is set, there will be no response body since the response
    /// will be stored into a file.
    ///
    /// If Collector::Ram(`Vec<u8>`) is set, the response body can be obtain here.
    fn get_response_body(&self) -> Option<Vec<u8>> {
        match self {
            Collector::File(_) => None,
            Collector::Ram(container) => Some(container.clone()),
        }
    }
}