Compare commits
	
		
			2 Commits
		
	
	
		
			a23429104c
			...
			2de01b2a0e
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 2de01b2a0e | |||
| be0fd5505b | 
@@ -6,19 +6,30 @@ use url::Url;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#[instrument(skip(data))]
 | 
					#[instrument(skip(data))]
 | 
				
			||||||
pub async fn store(data: &str, url: &Url) {
 | 
					pub async fn store(data: &str, url: &Url) {
 | 
				
			||||||
    let path = PathBuf::from("./downloaded".to_string() + url.path());
 | 
					    // extract data from url to save it accurately
 | 
				
			||||||
    let basepath = path.ancestors().skip(1).take(1).collect::<PathBuf>();
 | 
					    let url_path = PathBuf::from("./downloaded/".to_string() + url.domain().unwrap_or("UnknownDomain") + url.path());
 | 
				
			||||||
    trace!("Save path: {:?} and base path: {:?}", &path, &basepath);
 | 
					
 | 
				
			||||||
    if let Err(err) = fs::create_dir_all(&basepath).await {
 | 
					    // if it's a file
 | 
				
			||||||
        let ex = path.ancestors().fold(String::new(), |mut s, item| {
 | 
					    let (basepath, filename) = if url_path.extension().is_some() {
 | 
				
			||||||
            s += ", ";
 | 
					        // get everything up till the file
 | 
				
			||||||
            s += &item.to_string_lossy().to_string();
 | 
					        let basepath = url_path.ancestors().skip(1).take(1).collect::<PathBuf>();
 | 
				
			||||||
            s
 | 
					        // get the file name
 | 
				
			||||||
        });
 | 
					        let filename = url_path.file_name().expect("This should exist").to_string_lossy();
 | 
				
			||||||
        error!("Dir creation: {err} {:?} {ex}", basepath);
 | 
					        trace!("Save path: {:?} and base path: {:?}", &url_path, &basepath);
 | 
				
			||||||
 | 
					        (basepath, filename.to_string())
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        if let Err(err) = fs::write(&path, data).await {
 | 
					        (url_path.clone(), "index.html".into())
 | 
				
			||||||
            error!("File creation: {err} {:?}", path);
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // create the folders
 | 
				
			||||||
 | 
					    if let Err(err) = fs::create_dir_all(&basepath).await {
 | 
				
			||||||
 | 
					        error!("Dir creation: {err} {:?}", basepath);
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					        // FIXME I don't think this handles index.html files well...
 | 
				
			||||||
 | 
					        // TODO this should probably append .html to non-described files
 | 
				
			||||||
 | 
					        // create the file if that was successful
 | 
				
			||||||
 | 
					        if let Err(err) = fs::write(&basepath.join(filename), data).await {
 | 
				
			||||||
 | 
					            error!("File creation: {err} {:?}", url_path);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -167,6 +167,7 @@ async fn process(mut site: Website, db: Surreal<Client>, reqwest: reqwest::Clien
 | 
				
			|||||||
            .text()
 | 
					            .text()
 | 
				
			||||||
            .await
 | 
					            .await
 | 
				
			||||||
            .expect("Failed to read http response's body!");
 | 
					            .expect("Failed to read http response's body!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Store document
 | 
					        // Store document
 | 
				
			||||||
        filesystem::store(&data, &site.site).await;
 | 
					        filesystem::store(&data, &site.site).await;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user