use reqwest::header::{HeaderMap, ACCEPT_RANGES, CONTENT_DISPOSITION, CONTENT_LENGTH, ETAG, LAST_MODIFIED};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RemoteMetadata {
pub final_url: String,
pub content_length: Option<u64>,
pub accept_ranges: bool,
pub content_disposition: Option<String>,
pub etag: Option<String>,
pub last_modified: Option<String>,
}
impl RemoteMetadata {
pub fn from_headers(final_url: String, headers: &HeaderMap) -> Self {
let content_length = headers
.get(CONTENT_LENGTH)
.and_then(|v| v.to_str().ok())
.and_then(|v| v.parse::<u64>().ok());
let accept_ranges = headers
.get(ACCEPT_RANGES)
.and_then(|v| v.to_str().ok())
.map(|v| v.to_lowercase().contains("bytes"))
.unwrap_or(false);
let content_disposition = headers
.get(CONTENT_DISPOSITION)
.and_then(|v| v.to_str().ok())
.map(|v| v.to_string());
let etag = headers
.get(ETAG)
.and_then(|v| v.to_str().ok())
.map(|v| v.to_string());
let last_modified = headers
.get(LAST_MODIFIED)
.and_then(|v| v.to_str().ok())
.map(|v| v.to_string());
Self {
final_url,
content_length,
accept_ranges,
content_disposition,
etag,
last_modified,
}
}
}