butterfly_dl/core/
stream.rs1use futures::TryStreamExt;
6use std::pin::Pin;
7use std::sync::Arc;
8use std::task::{Context, Poll};
9use tokio::io::{AsyncRead, ReadBuf};
10
11pub enum DownloadStream {
13 Http(Box<dyn AsyncRead + Send + Unpin>),
15}
16
17impl AsyncRead for DownloadStream {
18 fn poll_read(
19 mut self: Pin<&mut Self>,
20 cx: &mut Context<'_>,
21 buf: &mut ReadBuf<'_>,
22 ) -> Poll<std::io::Result<()>> {
23 match &mut *self {
24 DownloadStream::Http(stream) => Pin::new(stream).poll_read(cx, buf),
25 }
26 }
27}
28
29pub type ProgressCallback = Arc<dyn Fn(u64, u64) + Send + Sync>;
31
32#[derive(Debug, Clone, PartialEq)]
34pub enum OverwriteBehavior {
35 Prompt,
37 Force,
39 NeverOverwrite,
41}
42
43impl Default for OverwriteBehavior {
44 fn default() -> Self {
45 Self::Prompt
46 }
47}
48
49pub struct DownloadOptions {
51 pub progress: Option<ProgressCallback>,
53
54 pub buffer_size: usize,
56
57 pub max_connections: usize,
59
60 pub overwrite: OverwriteBehavior,
62}
63
64impl Default for DownloadOptions {
65 fn default() -> Self {
66 Self {
67 progress: None,
68 buffer_size: 64 * 1024, max_connections: 16,
70 overwrite: OverwriteBehavior::default(),
71 }
72 }
73}
74
75pub fn create_http_stream(response: reqwest::Response) -> DownloadStream {
77 let stream = Box::new(tokio_util::io::StreamReader::new(
78 response.bytes_stream().map_err(std::io::Error::other),
79 ));
80 DownloadStream::Http(stream)
81}