From ce0f270ecdbf604c8f5b5cd4faae0872dc2d59b2 Mon Sep 17 00:00:00 2001 From: Pierre Jarriges <pierre.jarriges@tutanota.com> Date: Mon, 26 Sep 2022 09:12:15 +0200 Subject: [PATCH] relocate static assets in /assets sub dir --- README.md | 10 +++--- src/service/admin.rs | 4 +-- src/service/files.rs | 2 +- src/static_files/static_files.rs | 61 ++++++++++++++++++++++---------- src/website/page.rs | 38 ++++++++++---------- 5 files changed, 69 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 8cfdc88..618f004 100644 --- a/README.md +++ b/README.md @@ -104,11 +104,11 @@ A website data is store as a JSON file with the following structure } ], "assets_index": [ - "/images/toto.jpg", - "/sounds/toto.mp3", - "/video/toto.mp4", - "/docs/toto.xcf", - "/source_code/toto.js" + "/assets/images/toto.jpg", + "/assets/sounds/toto.mp3", + "/assets/video/toto.mp4", + "/assets/docs/toto.xcf", + "/assets/source_code/toto.js" ] } ``` \ No newline at end of file diff --git a/src/service/admin.rs b/src/service/admin.rs index 4bce663..aa53c86 100644 --- a/src/service/admin.rs +++ b/src/service/admin.rs @@ -67,7 +67,7 @@ pub async fn admin_login() -> impl Responder { <meta http-equiv='X-UA-Compatible' content='IE=edge'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <title>Krutacea - Admin Login</title> - <link rel='stylesheet' href='/default/admin.css'> + <link rel='stylesheet' href='/assets/default/admin.css'> </head> <body> @@ -83,7 +83,7 @@ pub async fn admin_login() -> impl Responder { <input type='submit' /> </form> </body> -<script src='/default/admin.js'></script> +<script src='/assets/default/admin.js'></script> </html> ", ) diff --git a/src/service/files.rs b/src/service/files.rs index 25bb61e..d961eae 100644 --- a/src/service/files.rs +++ b/src/service/files.rs @@ -48,7 +48,7 @@ async fn write_uploaded_file( filename: &String, upload_type: UploadFileType, ) -> Result<String, String> { - let root = &website.static_files_manager.dir; + let root = &website.static_files_manager.dir.join("assets"); let sub_dir = dirname_from_type(&upload_type); let filepath = root.join(sub_dir).join(&filename); diff --git a/src/static_files/static_files.rs b/src/static_files/static_files.rs index 74cd130..5fd6643 100644 --- a/src/static_files/static_files.rs +++ b/src/static_files/static_files.rs @@ -43,24 +43,39 @@ impl StaticFilesManager { fn create_dir_tree(dir: &PathBuf) -> Result<PathBuf, String> { if !dir.exists() { if let Err(err) = std::fs::create_dir_all(&dir) { - return Err(format!("{}", err)); + return Err(format!( + "Error creating StaticFilesManager root directory - {}", + err + )); }; } if let Err(err) = Self::create_assets_directories_structure(&dir) { - return Err(format!("{}", err)); + return Err(format!( + "Error creating StaticFilesManager assets directory structure - {}", + err + )); }; if let Err(err) = Self::copy_default_files(&dir) { - return Err(format!("{}", err)); + return Err(format!( + "Error copying StaticFilesManager default assets directory- {}", + err + )); } Ok(dir.clone()) } fn create_assets_directories_structure(root: &PathBuf) -> Result<(), std::io::Error> { + let assets_dir = root.join("assets"); + + if !assets_dir.exists() { + std::fs::create_dir(&assets_dir)?; + } + for d in STATIC_ASSETS_DIRECTORIES { - let p = root.join(d); + let p = assets_dir.join(d); if !p.exists() { std::fs::create_dir(p)?; } @@ -71,7 +86,7 @@ impl StaticFilesManager { fn copy_default_files(static_dir: &PathBuf) -> Result<(), String> { let local_default_static = std::env::current_dir().unwrap().join("default_static"); - let default_static = static_dir.join("default"); + let default_static = static_dir.join("assets").join("default"); let mut cpy_options = fs_extra::dir::CopyOptions::new(); cpy_options.content_only = true; cpy_options.overwrite = true; @@ -187,7 +202,7 @@ mod test_static_files_manager { let _manager = StaticFilesManager::testing_new(&test_dir).unwrap(); for d in STATIC_ASSETS_DIRECTORIES { - let p = test_dir.join(d); + let p = test_dir.join("assets").join(d); let exists = p.exists(); assert!( exists, @@ -204,13 +219,15 @@ mod test_static_files_manager { fn test_indexation() { let test_dir = create_test_dir(); let mut manager = StaticFilesManager::testing_new(&test_dir).unwrap(); - let file_pth = test_dir.join("docs").join("testing.txt"); + let file_pth = test_dir.join("assets").join("docs").join("testing.txt"); std::fs::File::create(&file_pth).unwrap(); manager = manager.build(); assert!( - manager.index.contains(&"docs/testing.txt".to_string()), - "Index doesn't contain path /docs/testing.txt\n{:?}", + manager + .index + .contains(&"assets/docs/testing.txt".to_string()), + "Index doesn't contain path /assets/docs/testing.txt\n{:?}", remove_test_dir(&test_dir) ); @@ -218,31 +235,35 @@ mod test_static_files_manager { } #[test] - fn test_pushd_and_remove_path() { + fn test_push_and_remove_path() { let test_dir = create_test_dir(); let mut manager = StaticFilesManager::testing_new(&test_dir).unwrap().build(); - let file_pth = test_dir.join("docs").join("testing.txt"); + let file_pth = test_dir.join("assets").join("docs").join("testing.txt"); std::fs::File::create(&file_pth).unwrap(); - let indexed_path = Path::new("docs/testing.txt"); + let indexed_path = Path::new("assets/docs/testing.txt"); let added = manager.push_path(&indexed_path); assert_eq!( added, - vec![PathBuf::from("docs/testing.txt")], + vec![PathBuf::from("assets/docs/testing.txt")], "Path was not added\n{:?}", remove_test_dir(&test_dir) ); assert!( - manager.index.contains(&"docs/testing.txt".to_string()), - "Index doesn't contain path /docs/testing.txt\n{:?}", + manager + .index + .contains(&"assets/docs/testing.txt".to_string()), + "Index doesn't contain path /assets/docs/testing.txt\n{:?}", remove_test_dir(&test_dir) ); - manager.remove_path("docs/testing.txt".to_string()); + manager.remove_path("assets/docs/testing.txt".to_string()); assert!( - !manager.index.contains(&"docs/testing.txt".to_string()), + !manager + .index + .contains(&"assets/docs/testing.txt".to_string()), "Path docs/testing.txt should have been removed\n{:?}", remove_test_dir(&test_dir) ); @@ -254,7 +275,7 @@ mod test_static_files_manager { fn test_push_unexisting_path() { let test_dir = create_test_dir(); let mut manager = StaticFilesManager::testing_new(&test_dir).unwrap().build(); - let indexed_path = Path::new("images/unexisting.png"); + let indexed_path = Path::new("assets/images/unexisting.png"); let added = manager.push_path(&indexed_path); assert_eq!( @@ -265,7 +286,9 @@ mod test_static_files_manager { ); assert!( - !manager.index.contains(&"images/unexisting.png".to_string()), + !manager + .index + .contains(&"assets/images/unexisting.png".to_string()), "Index shouldn't container unexisting path\n{:?}", remove_test_dir(&test_dir) ); diff --git a/src/website/page.rs b/src/website/page.rs index c618440..7be9be6 100644 --- a/src/website/page.rs +++ b/src/website/page.rs @@ -109,7 +109,7 @@ pub struct CSSLinks(Vec<String>); impl CSSLinks { pub fn new() -> Self { - CSSLinks(vec!["/default/style.css".to_owned()]) + CSSLinks(vec!["/assets/default/style.css".to_owned()]) } } @@ -282,18 +282,18 @@ mod test_pages { description: String::from("test descr"), url_slug: String::from("test-page"), css: CSSLinks(vec![ - "/source_code/mystyle.css".to_string(), - "/source_code/mystyle2.css".to_string(), + "/assets/source_code/mystyle.css".to_string(), + "/assets/source_code/mystyle2.css".to_string(), ]), js: JSLinks(vec![ - "/source_code/myscript.js".to_string(), - "/source_code/myscript2.js".to_string(), + "/assets/source_code/myscript.js".to_string(), + "/assets/source_code/myscript2.js".to_string(), ]), - favicon: FaviconLink(String::from("/images/testicon.ico")), + favicon: FaviconLink(String::from("/assets/images/testicon.ico")), author: String::from("test author"), image: ImageLinks(vec![ - "/images/testimage.png".to_string(), - "/images/testimage2.png".to_string(), + "/assets/images/testimage.png".to_string(), + "/assets/images/testimage2.png".to_string(), ]), } } @@ -383,7 +383,7 @@ mod test_pages { let pmd = test_page_metadata(); assert_eq!( pmd.favicon.to_string(), - "<link rel='icon' type='image/*' href='/images/testicon.ico'/>" + "<link rel='icon' type='image/*' href='/assets/images/testicon.ico'/>" ) } @@ -392,8 +392,8 @@ mod test_pages { let pmd = test_page_metadata(); assert_eq!( pmd.css.to_string(), - "<link rel='stylesheet' href='/source_code/mystyle.css'> -<link rel='stylesheet' href='/source_code/mystyle2.css'>" + "<link rel='stylesheet' href='/assets/source_code/mystyle.css'> +<link rel='stylesheet' href='/assets/source_code/mystyle2.css'>" ) } @@ -402,8 +402,8 @@ mod test_pages { let pmd = test_page_metadata(); assert_eq!( pmd.js.to_string(), - "<script src='/source_code/myscript.js'></script> -<script src='/source_code/myscript2.js'></script>" + "<script src='/assets/source_code/myscript.js'></script> +<script src='/assets/source_code/myscript2.js'></script>" ) } @@ -412,12 +412,12 @@ mod test_pages { let pmd = test_page_metadata(); assert_eq!( pmd.image.to_string(), - "<meta name='image' content='/images/testimage.png'/> -<meta property='og:image' content='/images/testimage.png'/> -<meta property='twitter:image' content='/images/testimage.png'/> -<meta name='image' content='/images/testimage2.png'/> -<meta property='og:image' content='/images/testimage2.png'/> -<meta property='twitter:image' content='/images/testimage2.png'/>" + "<meta name='image' content='/assets/images/testimage.png'/> +<meta property='og:image' content='/assets/images/testimage.png'/> +<meta property='twitter:image' content='/assets/images/testimage.png'/> +<meta name='image' content='/assets/images/testimage2.png'/> +<meta property='og:image' content='/assets/images/testimage2.png'/> +<meta property='twitter:image' content='/assets/images/testimage2.png'/>" ) } -- GitLab