Skip to main content

rusty_cat/presigned/
range_download_plan.rs

1use reqwest::header::HeaderMap;
2
3/// Provider-neutral presigned range-download plan.
4#[derive(Debug, Clone)]
5pub struct PresignedRangeDownloadPlan {
6    /// Known remote object size. When present, download prepare skips HEAD.
7    pub total_size: Option<u64>,
8    /// Optional dedicated HEAD URL.
9    pub head_url: Option<String>,
10    /// URL used for range GET requests.
11    pub range_url: String,
12    /// Optional range URL expiration timestamp in Unix seconds.
13    pub range_expires_at_unix_secs: Option<u64>,
14    /// Refresh threshold in seconds. Range URL is refreshed before a chunk when
15    /// `now + refresh_before_secs >= range_expires_at_unix_secs`.
16    pub refresh_before_secs: u64,
17    /// Headers added to HEAD requests.
18    pub head_headers: HeaderMap,
19    /// Headers added to range GET requests.
20    pub range_headers: HeaderMap,
21}
22
23impl PresignedRangeDownloadPlan {
24    /// Creates a plan using the same URL for range GET and optional HEAD.
25    pub fn new(range_url: impl Into<String>) -> Self {
26        Self {
27            total_size: None,
28            head_url: None,
29            range_url: range_url.into(),
30            range_expires_at_unix_secs: None,
31            refresh_before_secs: 60,
32            head_headers: HeaderMap::new(),
33            range_headers: HeaderMap::new(),
34        }
35    }
36
37    /// Sets known remote object size and enables HEAD skipping.
38    pub fn with_total_size(mut self, total_size: u64) -> Self {
39        self.total_size = Some(total_size);
40        self
41    }
42
43    /// Sets dedicated HEAD URL.
44    pub fn with_head_url(mut self, url: impl Into<String>) -> Self {
45        self.head_url = Some(url.into());
46        self
47    }
48
49    /// Sets range URL expiration timestamp in Unix seconds.
50    pub fn with_range_expires_at_unix_secs(mut self, expires_at_unix_secs: u64) -> Self {
51        self.range_expires_at_unix_secs = Some(expires_at_unix_secs);
52        self
53    }
54
55    /// Sets range URL refresh threshold in seconds.
56    pub fn with_refresh_before_secs(mut self, secs: u64) -> Self {
57        self.refresh_before_secs = secs;
58        self
59    }
60
61    /// Replaces HEAD headers.
62    pub fn with_head_headers(mut self, headers: HeaderMap) -> Self {
63        self.head_headers = headers;
64        self
65    }
66
67    /// Replaces range GET headers.
68    pub fn with_range_headers(mut self, headers: HeaderMap) -> Self {
69        self.range_headers = headers;
70        self
71    }
72}