Skip to main content

fast_down/
url_info.rs

1use std::sync::Arc;
2use url::Url;
3
4#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct UrlInfo {
7    pub size: u64,
8    /// 服务器返回的原始文件名,必须清洗掉不合法字符才能安全使用
9    #[cfg_attr(
10        feature = "sanitize-filename",
11        doc = "用 [`UrlInfo::filename()`] 方法处理文件名"
12    )]
13    #[cfg_attr(
14        not(feature = "sanitize-filename"),
15        doc = "开启 `sanitize-filename` 特性后,可使用 `filename()` 方法处理文件名。"
16    )]
17    pub raw_name: String,
18    pub supports_range: bool,
19    pub fast_download: bool,
20    pub final_url: Url,
21    pub file_id: FileId,
22}
23
24#[cfg(feature = "sanitize-filename")]
25impl UrlInfo {
26    #[must_use]
27    pub fn filename(&self) -> String {
28        sanitize_filename::sanitize_with_options(
29            &self.raw_name,
30            sanitize_filename::Options {
31                windows: cfg!(windows),
32                truncate: true,
33                replacement: "_",
34            },
35        )
36    }
37}
38
39#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
40#[derive(Debug, Clone, PartialEq, Eq, Default)]
41pub struct FileId {
42    pub etag: Option<Arc<str>>,
43    pub last_modified: Option<Arc<str>>,
44}
45
46impl FileId {
47    pub fn new(etag: Option<&str>, last_modified: Option<&str>) -> Self {
48        Self {
49            etag: etag.map(Arc::from),
50            last_modified: last_modified.map(Arc::from),
51        }
52    }
53}