1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-11-21 23:00:50 +03:00

Mount the static assets on /assets

This commit is contained in:
Quentin Gliech
2022-09-30 15:31:47 +02:00
parent eeae943208
commit 93ce5c797c
8 changed files with 92 additions and 24 deletions

View File

@@ -16,14 +16,16 @@
use std::{collections::HashMap, str::FromStr};
use mas_router::{Route, UrlBuilder};
use tera::{helpers::tests::number_args_allowed, Tera, Value};
use url::Url;
pub fn register(tera: &mut Tera) {
pub fn register(tera: &mut Tera, url_builder: UrlBuilder) {
tera.register_tester("empty", self::tester_empty);
tera.register_function("add_params_to_uri", function_add_params_to_uri);
tera.register_function("merge", function_merge);
tera.register_function("dict", function_dict);
tera.register_function("static_asset", make_static_asset(url_builder));
}
fn tester_empty(value: Option<&Value>, params: &[Value]) -> Result<bool, tera::Error> {
@@ -115,3 +117,26 @@ fn function_dict(params: &HashMap<String, Value>) -> Result<Value, tera::Error>
let ret = params.clone().into_iter().collect();
Ok(Value::Object(ret))
}
fn make_static_asset(url_builder: UrlBuilder) -> impl tera::Function {
Box::new(
move |args: &HashMap<String, Value>| -> Result<Value, tera::Error> {
if let Some(path) = args.get("path").and_then(Value::as_str) {
let absolute = args
.get("absolute")
.and_then(Value::as_bool)
.unwrap_or(false);
let path = path.to_owned();
let url = if absolute {
url_builder.static_asset(path).into()
} else {
let destination = mas_router::StaticAsset::new(path);
destination.relative_url().into_owned()
};
Ok(Value::String(url))
} else {
Err(tera::Error::msg("Invalid parameter 'path'"))
}
},
)
}