use rocket::{fs::NamedFile, get}; use std::io::Write; async fn download_file(url: &str, file: &str) { let content = reqwest::get(url).await.expect("couldn't download file"); std::fs::File::create(file) .expect("could not create file") .write_all(&content.bytes().await.expect("could not get bytes")) .expect("could not write file"); } #[get("/bootstrap.min.css")] pub async fn bootstrap_css() -> Option { NamedFile::open("./cache/bootstrap.min.css").await.ok() } #[get("/bootstrap-icons.css")] pub async fn bootstrap_icons() -> Option { NamedFile::open("./cache/bootstrap-icons.css").await.ok() } #[get("/bootstrap.bundle.min.js")] pub async fn bootstrap_js() -> Option { NamedFile::open("./cache/bootstrap.bundle.min.js") .await .ok() } #[get("/fonts/bootstrap-icons.woff2")] pub async fn bootstrap_font1() -> Option { NamedFile::open("./cache/fonts/bootstrap-icons.woff2") .await .ok() } #[get("/fonts/bootstrap-icons.woff")] pub async fn bootstrap_font2() -> Option { NamedFile::open("./cache/fonts/bootstrap-icons.woff") .await .ok() } /// Cache Bootstrap files locally pub async fn cache_bootstrap() { std::fs::create_dir_all("./cache/fonts").expect("couldn't create cache dir"); download_file( "https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css", "./cache/bootstrap.min.css", ) .await; download_file( "https://cdn.jsdelivr.net/npm/bootstrap-icons@1.9.1/font/bootstrap-icons.css", "./cache/bootstrap-icons.css", ) .await; download_file( "https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/js/bootstrap.bundle.min.js", "./cache/bootstrap.bundle.min.js", ) .await; download_file("https://cdn.jsdelivr.net/npm/bootstrap-icons@1.9.1/font/fonts/bootstrap-icons.woff2?8d200481aa7f02a2d63a331fc782cfaf", "./cache/fonts/bootstrap-icons.woff2").await; download_file("https://cdn.jsdelivr.net/npm/bootstrap-icons@1.9.1/font/fonts/bootstrap-icons.woff?8d200481aa7f02a2d63a331fc782cfaf", "./cache/fonts/bootstrap-icons.woff").await; }