2026-02-12 07:09:37 +01:00
|
|
|
(* https://docs.taler.net/design-documents/003-tos-rendering.html
|
|
|
|
|
https://docs.taler.net/design-documents/003-tos-rendering.html
|
2025-09-23 20:58:34 +02:00
|
|
|
|
2026-02-12 07:09:37 +01:00
|
|
|
must support `text/plain` and `text/markdown` *)
|
2025-10-13 00:29:33 +02:00
|
|
|
|
2025-10-03 18:35:21 +02:00
|
|
|
type t =
|
|
|
|
|
| Terms
|
|
|
|
|
| Privacy
|
2025-09-27 18:49:16 +02:00
|
|
|
|
2026-02-12 07:09:37 +01:00
|
|
|
module Assets_config = struct
|
|
|
|
|
(* hardcoded config just for static assets *)
|
|
|
|
|
let default_lang = "en"
|
|
|
|
|
let default_mimetype = ("text", "plain")
|
|
|
|
|
let default_extension = ".txt"
|
|
|
|
|
let default_encoding : [< `Identity | `DEFLATE | `Gzip ] = `Identity
|
|
|
|
|
let base_dir = function Terms -> "terms" | Privacy -> "privacy"
|
|
|
|
|
|
|
|
|
|
(* TODO this should be in the config like terms_etag *)
|
|
|
|
|
let terms_legal_version = "0"
|
|
|
|
|
let privacy_legal_version = "0"
|
|
|
|
|
end
|
|
|
|
|
|
2025-11-20 18:38:01 +01:00
|
|
|
let etag k =
|
2026-02-12 07:09:37 +01:00
|
|
|
let etag =
|
|
|
|
|
match k with Terms -> Config.terms_etag | Privacy -> Config.privacy_etag
|
|
|
|
|
in
|
2026-02-12 11:25:57 +01:00
|
|
|
etag |> Headers_lib.Etag.parse |> Result.get_ok
|
2025-09-27 18:49:16 +02:00
|
|
|
|
|
|
|
|
let legal_version = function
|
2026-02-12 07:09:37 +01:00
|
|
|
| Terms -> Assets_config.terms_legal_version
|
|
|
|
|
| Privacy -> Assets_config.privacy_legal_version
|
2025-09-27 18:49:16 +02:00
|
|
|
|
2025-09-23 20:58:34 +02:00
|
|
|
let supported_lang_arr, supported_ext_arr =
|
2025-09-27 18:49:16 +02:00
|
|
|
let aux t =
|
2026-02-12 07:09:37 +01:00
|
|
|
let prefix = Fpath.v (Assets_config.base_dir t) in
|
|
|
|
|
let path_l = List.map Fpath.v Assets_crunch.file_list in
|
|
|
|
|
let path_l = List.filter_map (Fpath.rem_prefix prefix) path_l in
|
2025-09-23 20:58:34 +02:00
|
|
|
let ext_l =
|
|
|
|
|
path_l |> List.map Fpath.get_ext |> List.sort_uniq String.compare
|
|
|
|
|
in
|
|
|
|
|
let lang_l =
|
2026-02-12 07:09:37 +01:00
|
|
|
List.map
|
|
|
|
|
(fun path ->
|
2025-11-11 02:32:45 +01:00
|
|
|
match Fpath.segs path with
|
|
|
|
|
| [] -> assert false
|
|
|
|
|
| [ dir; _file ] -> dir
|
|
|
|
|
| _l ->
|
|
|
|
|
Fmt.failwith "invalid folder structure, file `%s` is misplaced"
|
2026-02-12 07:09:37 +01:00
|
|
|
(Fpath.to_string Fpath.(prefix // path)))
|
|
|
|
|
path_l
|
2025-09-23 20:58:34 +02:00
|
|
|
in
|
2026-02-12 07:09:37 +01:00
|
|
|
let lang_l = List.sort_uniq String.compare lang_l in
|
2026-02-12 11:25:57 +01:00
|
|
|
let etag = (etag t).value in
|
2026-02-12 07:09:37 +01:00
|
|
|
List.iter
|
|
|
|
|
(fun path ->
|
|
|
|
|
let etag' = Fpath.to_string (Fpath.rem_ext (Fpath.base path)) in
|
|
|
|
|
if not @@ String.equal etag etag' then
|
|
|
|
|
Fmt.failwith
|
|
|
|
|
"filename of file `%s` does not match configuration ETAG value `%s`"
|
|
|
|
|
(Fpath.to_string Fpath.(prefix // path))
|
|
|
|
|
etag)
|
|
|
|
|
path_l;
|
|
|
|
|
if List.is_empty lang_l then Fmt.failwith "no language supported";
|
|
|
|
|
if List.is_empty ext_l then Fmt.failwith "no mimetype supported";
|
|
|
|
|
if not @@ List.mem Assets_config.default_lang lang_l then
|
|
|
|
|
Fmt.failwith "default language `%s` files not found"
|
|
|
|
|
Assets_config.default_lang;
|
|
|
|
|
if not @@ List.mem ".txt" ext_l then
|
|
|
|
|
Fmt.failwith "plain text file not found";
|
|
|
|
|
if not @@ List.mem ".md" ext_l then Fmt.failwith "markdown file not found";
|
|
|
|
|
List.iter
|
|
|
|
|
(fun dir ->
|
|
|
|
|
if String.length dir <> 2 then
|
|
|
|
|
Fmt.failwith "language directory with invalid name: `%s`" dir)
|
|
|
|
|
lang_l;
|
|
|
|
|
if List.length path_l <> List.length ext_l * List.length lang_l then
|
|
|
|
|
Fmt.failwith
|
|
|
|
|
"invalid folder structure, all supported language must provide the \
|
|
|
|
|
same set of file mimetype"
|
|
|
|
|
else (lang_l, ext_l)
|
2025-09-23 20:58:34 +02:00
|
|
|
in
|
|
|
|
|
let lang_l, ext_l = aux Terms in
|
|
|
|
|
let lang_l', ext_l' = aux Privacy in
|
2026-02-12 07:09:37 +01:00
|
|
|
match
|
|
|
|
|
List.equal String.equal lang_l lang_l'
|
|
|
|
|
&& List.equal String.equal ext_l ext_l'
|
|
|
|
|
with
|
|
|
|
|
| false ->
|
2025-09-23 20:58:34 +02:00
|
|
|
Fmt.failwith
|
|
|
|
|
"invalid folder structure, /terms and /privacy must support the same \
|
2026-02-12 07:09:37 +01:00
|
|
|
set of languages and mimetypes"
|
|
|
|
|
| true -> (Array.of_list lang_l, Array.of_list ext_l)
|
2025-09-23 20:58:34 +02:00
|
|
|
|
2026-02-10 21:49:42 +01:00
|
|
|
module Mimetype = struct
|
|
|
|
|
type t = string * string
|
|
|
|
|
|
|
|
|
|
let pp fmt mime = Fmt.pf fmt "%s/%s" (fst mime) (snd mime)
|
|
|
|
|
|
|
|
|
|
let assoc =
|
|
|
|
|
List.filter
|
|
|
|
|
(fun (_mime, ext) -> Array.mem ext supported_ext_arr)
|
|
|
|
|
[
|
|
|
|
|
(("text", "plain"), ".txt");
|
|
|
|
|
(("text", "markdown"), ".md");
|
|
|
|
|
(("text", "html"), ".html");
|
|
|
|
|
(("text", "html"), ".htm");
|
|
|
|
|
(("application", "pdf"), ".pdf");
|
|
|
|
|
(("image", "jpeg"), ".jpg");
|
|
|
|
|
(("image", "jpeg"), ".jpeg");
|
|
|
|
|
(("image", "png"), ".png");
|
|
|
|
|
(("image", "gif"), ".gif");
|
|
|
|
|
]
|
|
|
|
|
|
2026-02-12 07:09:37 +01:00
|
|
|
let arr =
|
|
|
|
|
let all_supported, all_supported_ext = List.split assoc in
|
|
|
|
|
match
|
|
|
|
|
Array.find_opt
|
|
|
|
|
(fun ext -> not @@ List.exists (( = ) ext) all_supported_ext)
|
|
|
|
|
supported_ext_arr
|
|
|
|
|
with
|
|
|
|
|
| Some ext -> Fmt.failwith "extension `%s` unsupported" ext
|
|
|
|
|
| None -> Array.of_list all_supported
|
|
|
|
|
|
|
|
|
|
let default =
|
|
|
|
|
match
|
|
|
|
|
List.mem
|
|
|
|
|
(Assets_config.default_mimetype, Assets_config.default_extension)
|
|
|
|
|
assoc
|
|
|
|
|
with
|
|
|
|
|
| false ->
|
|
|
|
|
Fmt.failwith "default content type `%a` not supported" pp
|
|
|
|
|
Assets_config.default_mimetype
|
|
|
|
|
| true -> Assets_config.default_mimetype
|
|
|
|
|
|
|
|
|
|
let of_cohttp = function
|
2026-02-10 21:49:42 +01:00
|
|
|
| Cohttp.Accept.MediaType (m, m_sub) ->
|
2026-02-12 07:09:37 +01:00
|
|
|
Array.find_opt (( = ) (m, m_sub)) arr
|
|
|
|
|
| AnyMediaSubtype m -> Array.find_opt (fun (m', _) -> String.equal m m') arr
|
|
|
|
|
| AnyMedia -> Some default
|
2026-02-10 21:49:42 +01:00
|
|
|
|
2026-02-12 07:09:37 +01:00
|
|
|
let to_extension_exn t =
|
2026-02-10 21:49:42 +01:00
|
|
|
match List.assoc_opt t assoc with
|
2026-02-12 07:09:37 +01:00
|
|
|
| None -> Fmt.failwith "Mimetype.to_extension failure: `%a` unknown" pp t
|
2026-02-10 21:49:42 +01:00
|
|
|
| Some ext -> ext
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
module Language = struct
|
|
|
|
|
type t = string
|
|
|
|
|
|
2026-02-12 07:09:37 +01:00
|
|
|
let arr = supported_lang_arr
|
|
|
|
|
let default = Assets_config.default_lang
|
|
|
|
|
|
|
|
|
|
let of_cohttp = function
|
|
|
|
|
| Cohttp.Accept.AnyLanguage -> Some default
|
2026-02-10 21:49:42 +01:00
|
|
|
| Language language_range -> (
|
|
|
|
|
(* ignore language subtags (e.g. "en-US" -> "en") *)
|
|
|
|
|
match language_range with
|
|
|
|
|
| [] -> assert false
|
|
|
|
|
| lang :: _ when Array.mem lang supported_lang_arr -> Some lang
|
|
|
|
|
| _ -> None)
|
|
|
|
|
end
|
2025-09-23 20:58:34 +02:00
|
|
|
|
2025-09-28 19:37:49 +02:00
|
|
|
(* ! lang and mime must be supported *)
|
|
|
|
|
let get_content ~lang ~mime t =
|
2026-02-12 07:09:37 +01:00
|
|
|
let ext = Mimetype.to_extension_exn mime in
|
|
|
|
|
let path =
|
2026-02-12 11:25:57 +01:00
|
|
|
Fpath.to_string
|
|
|
|
|
Fpath.((v (Assets_config.base_dir t) / lang / (etag t).value) + ext)
|
2026-02-12 07:09:37 +01:00
|
|
|
in
|
2025-09-27 18:49:16 +02:00
|
|
|
match Assets_crunch.read path with
|
2025-09-28 19:37:49 +02:00
|
|
|
| None -> Fmt.failwith "static file not found: `%s`" path
|
|
|
|
|
| Some data -> data
|