|
| 1 | +use axum::extract::Path; |
| 2 | +use axum::extract::Request; |
| 3 | +use axum::response::IntoResponse; |
| 4 | +use axum::Extension; |
| 5 | +use editoast_authz::BuiltinRole; |
| 6 | +use editoast_derive::EditoastError; |
| 7 | +use thiserror::Error; |
| 8 | +use tower::ServiceExt; |
| 9 | +use tower_http::services::ServeFile; |
| 10 | + |
| 11 | +use crate::client::get_dynamic_assets_path; |
| 12 | +use crate::error::Result; |
| 13 | +use crate::views::AuthenticationExt; |
| 14 | +use crate::views::AuthorizationError; |
| 15 | + |
| 16 | +crate::routes! { |
| 17 | + "/fonts/{font}/{glyph}" => fonts, |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(Debug, Error, EditoastError)] |
| 21 | +#[editoast_error(base_id = "fonts")] |
| 22 | +enum FontErrors { |
| 23 | + #[error("File '{file}' not found")] |
| 24 | + #[editoast_error(status = 404)] |
| 25 | + FileNotFound { file: String }, |
| 26 | +} |
| 27 | + |
| 28 | +/// This endpoint is used by map libre to retrieve the fonts. They are separated by font and unicode block |
| 29 | +
|
| 30 | +#[utoipa::path( |
| 31 | + get, path = "", |
| 32 | + tag = "fonts", |
| 33 | + params( |
| 34 | + ("font" = String, Path, description = "Requested font"), |
| 35 | + ("glyph" = String, Path, description = "Requested unicode block"), |
| 36 | + ), |
| 37 | + responses( |
| 38 | + (status = 200, description = "Glyphs in PBF format of the font at the requested unicode block"), |
| 39 | + (status = 404, description = "Font not found"), |
| 40 | + ), |
| 41 | +)] |
| 42 | +async fn fonts( |
| 43 | + Extension(auth): AuthenticationExt, |
| 44 | + Path((font, file_name)): Path<(String, String)>, |
| 45 | + request: Request, |
| 46 | +) -> Result<impl IntoResponse> { |
| 47 | + let authorized = auth |
| 48 | + .check_roles([BuiltinRole::MapRead].into()) |
| 49 | + .await |
| 50 | + .map_err(AuthorizationError::AuthError)?; |
| 51 | + if !authorized { |
| 52 | + return Err(AuthorizationError::Forbidden.into()); |
| 53 | + } |
| 54 | + |
| 55 | + let path = get_dynamic_assets_path().join(format!("fonts/glyphs/{font}/{file_name}")); |
| 56 | + |
| 57 | + if !path.is_file() { |
| 58 | + return Err(FontErrors::FileNotFound { file: file_name }.into()); |
| 59 | + } |
| 60 | + |
| 61 | + Ok(ServeFile::new(&path).oneshot(request).await) |
| 62 | +} |
| 63 | + |
| 64 | +#[cfg(test)] |
| 65 | +mod tests { |
| 66 | + use crate::views::test_app::TestAppBuilder; |
| 67 | + |
| 68 | + use super::*; |
| 69 | + use axum::http::StatusCode; |
| 70 | + use rstest::rstest; |
| 71 | + |
| 72 | + #[rstest] |
| 73 | + async fn test_font() { |
| 74 | + let app = TestAppBuilder::default_app(); |
| 75 | + let request = app.get("/fonts/Roboto%20Bold/0-255.pbf"); |
| 76 | + let response = app.fetch(request).assert_status(StatusCode::OK); |
| 77 | + assert_eq!("application/octet-stream", response.content_type()); |
| 78 | + let response = response.bytes(); |
| 79 | + let expected = |
| 80 | + std::fs::read(get_dynamic_assets_path().join("fonts/glyphs/Roboto Bold/0-255.pbf")) |
| 81 | + .unwrap(); |
| 82 | + assert_eq!(response, expected); |
| 83 | + } |
| 84 | + |
| 85 | + #[rstest] |
| 86 | + async fn test_font_not_found() { |
| 87 | + let app = TestAppBuilder::default_app(); |
| 88 | + let request = app.get("/fonts/Comic%20Sans/0-255.pbf"); |
| 89 | + app.fetch(request).assert_status(StatusCode::NOT_FOUND); |
| 90 | + } |
| 91 | +} |
0 commit comments