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    pub content_type: Option<String>,
23}
24
25#[cfg(feature = "sanitize-filename")]
26impl UrlInfo {
27    #[must_use]
28    pub fn filename(&self) -> String {
29        sanitize_filename::sanitize_with_options(
30            &self.raw_name,
31            sanitize_filename::Options {
32                windows: cfg!(windows),
33                truncate: true,
34                replacement: "_",
35            },
36        )
37    }
38}
39
40#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
41#[derive(Debug, Clone, PartialEq, Eq, Default)]
42pub struct FileId {
43    pub etag: Option<Arc<str>>,
44    pub last_modified: Option<Arc<str>>,
45}
46
47impl FileId {
48    pub fn new(etag: Option<&str>, last_modified: Option<&str>) -> Self {
49        Self {
50            etag: etag.map(Arc::from),
51            last_modified: last_modified.map(Arc::from),
52        }
53    }
54}