de-enshitified file saving logic
This commit is contained in:
parent
9bfa8f9108
commit
3497312fd4
@ -1,30 +1,35 @@
|
|||||||
use std::{ffi::OsStr, io::ErrorKind, path::PathBuf};
|
use std::{ffi::OsStr, io::ErrorKind, path::PathBuf};
|
||||||
|
|
||||||
|
use reqwest::header::HeaderValue;
|
||||||
use tokio::fs;
|
use tokio::fs;
|
||||||
use tracing::{error, trace};
|
use tracing::{error, trace};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
pub fn as_path(url: &Url) -> PathBuf {
|
pub fn as_path(url: &Url, content_type: &HeaderValue) -> PathBuf {
|
||||||
// extract data from url to save it accurately
|
// extract data from url to save it accurately
|
||||||
let url_path = PathBuf::from("./downloaded/".to_string() + url.domain().unwrap_or("UnknownDomain") + url.path());
|
let mut url_path = PathBuf::from("./downloaded/".to_string() + url.domain().unwrap_or("UnknownDomain") + url.path());
|
||||||
|
|
||||||
// if it's a file
|
if let Ok(header) = content_type.to_str() {
|
||||||
let (basepath, filename) = if url_path.extension().filter(valid_file_extension).is_some() {
|
// text/html; charset=UTF-8; option=value
|
||||||
// get everything up till the file
|
let ttype = if let Some((t, _)) = header.split_once(';') {
|
||||||
let basepath = url_path.ancestors().skip(1).take(1).collect::<PathBuf>();
|
t
|
||||||
// get the file name
|
|
||||||
let filename = url_path.file_name().expect("This should exist").to_string_lossy();
|
|
||||||
trace!("Save path: {:?} and base path: {:?}", &url_path, &basepath);
|
|
||||||
(basepath, filename.to_string())
|
|
||||||
} else {
|
} else {
|
||||||
(url_path.clone(), "index.html".into())
|
header
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut path = PathBuf::new();
|
if let Some((ttype, subtype)) = ttype.split_once('/') {
|
||||||
path = path.join(basepath);
|
trace!("Found Content-Type to be: {ttype}/{subtype} for {}", url.to_string());
|
||||||
path = path.join(filename);
|
// If the Content-Type header is "*/html" (most likely "text/html") and the path's
|
||||||
|
// extension is anything but html:
|
||||||
|
if subtype=="html" && !url_path.extension().is_some_and(|f| f=="html" || f=="htm" ) {
|
||||||
|
// time to slap a index.html to the end of that path there!
|
||||||
|
url_path = url_path.join("index.html");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
trace!("Final path for {} is: {:?}", url, url_path);
|
||||||
|
|
||||||
path
|
url_path
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn init(filename: &PathBuf) -> Option<fs::File> {
|
pub async fn init(filename: &PathBuf) -> Option<fs::File> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user