2026-02-12 07:09:37 +01:00
|
|
|
(* https://docs.taler.net/design-documents/003-tos-rendering.html
|
|
|
|
|
must support `text/plain` and `text/markdown` *)
|
2025-09-13 15:56:19 +02:00
|
|
|
|
2026-03-26 06:23:42 +01:00
|
|
|
let failure fmt =
|
|
|
|
|
Fmt.kstr
|
|
|
|
|
(fun s ->
|
|
|
|
|
Fmt.epr "Assets failure: %s.@." s;
|
|
|
|
|
exit 1)
|
|
|
|
|
fmt
|
|
|
|
|
|
2025-09-13 15:56:19 +02:00
|
|
|
type t =
|
|
|
|
|
| Terms
|
|
|
|
|
| Privacy
|
|
|
|
|
|
2026-03-20 01:13:21 +01:00
|
|
|
module Cfg = struct
|
2026-02-12 07:09:37 +01:00
|
|
|
(* 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"
|
2025-09-13 15:56:19 +02:00
|
|
|
|
2026-02-12 07:09:37 +01:00
|
|
|
(* TODO this should be in the config like terms_etag *)
|
|
|
|
|
let terms_legal_version = "0"
|
|
|
|
|
end
|
2025-09-13 15:56:19 +02:00
|
|
|
|
2026-02-12 07:09:37 +01:00
|
|
|
let etag k =
|
2026-02-12 11:25:57 +01:00
|
|
|
match k with Terms -> Config.terms_etag | Privacy -> Config.privacy_etag
|
2025-09-13 15:56:19 +02:00
|
|
|
|
|
|
|
|
let supported_lang_arr, supported_ext_arr =
|
|
|
|
|
let aux t =
|
2026-03-20 01:13:21 +01:00
|
|
|
let prefix = Fpath.v (Cfg.base_dir t) in
|
2026-02-12 07:09:37 +01:00
|
|
|
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-13 15:56:19 +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-09-13 15:56:19 +02:00
|
|
|
match Fpath.segs path with
|
|
|
|
|
| [] -> assert false
|
|
|
|
|
| [ dir; _file ] -> dir
|
|
|
|
|
| _l ->
|
2026-03-26 06:23:42 +01:00
|
|
|
failure "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-13 15:56:19 +02:00
|
|
|
in
|
2026-02-12 07:09:37 +01:00
|
|
|
let lang_l = List.sort_uniq String.compare lang_l in
|
2026-03-20 01:13:21 +01:00
|
|
|
let etag = etag t 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
|
2026-03-26 06:23:42 +01:00
|
|
|
failure "filename `%s` does not match configuration ETAG value `%s`"
|
2026-02-12 07:09:37 +01:00
|
|
|
(Fpath.to_string Fpath.(prefix // path))
|
|
|
|
|
etag)
|
|
|
|
|
path_l;
|
2026-03-26 06:23:42 +01:00
|
|
|
if List.is_empty lang_l then failure "no language supported";
|
|
|
|
|
if List.is_empty ext_l then failure "no mimetype supported";
|
2026-03-20 01:13:21 +01:00
|
|
|
if not @@ List.mem Cfg.default_lang lang_l then
|
2026-03-26 06:23:42 +01:00
|
|
|
failure "default language `%s` files not found" Cfg.default_lang;
|
|
|
|
|
if not @@ List.mem ".txt" ext_l then failure "plain text file not found";
|
|
|
|
|
if not @@ List.mem ".md" ext_l then failure "markdown file not found";
|
2026-02-12 07:09:37 +01:00
|
|
|
List.iter
|
|
|
|
|
(fun dir ->
|
|
|
|
|
if String.length dir <> 2 then
|
2026-03-26 06:23:42 +01:00
|
|
|
failure "language directory with invalid name: `%s`" dir)
|
2026-02-12 07:09:37 +01:00
|
|
|
lang_l;
|
|
|
|
|
if List.length path_l <> List.length ext_l * List.length lang_l then
|
2026-03-26 06:23:42 +01:00
|
|
|
failure
|
2026-02-12 07:09:37 +01:00
|
|
|
"invalid folder structure, all supported language must provide the \
|
|
|
|
|
same set of file mimetype"
|
|
|
|
|
else (lang_l, ext_l)
|
2025-09-13 15:56:19 +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 ->
|
2026-03-26 06:23:42 +01:00
|
|
|
failure
|
2025-09-13 15:56:19 +02:00
|
|
|
"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-13 15:56:19 +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-03-20 01:13:21 +01:00
|
|
|
let default = Cfg.default_mimetype
|
|
|
|
|
|
|
|
|
|
let () =
|
|
|
|
|
if not @@ List.mem (Cfg.default_mimetype, Cfg.default_extension) assoc then
|
2026-03-26 06:23:42 +01:00
|
|
|
failure "default content type `%a` not supported" pp Cfg.default_mimetype
|
2026-03-20 01:13:21 +01:00
|
|
|
|
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
|
2026-03-26 06:23:42 +01:00
|
|
|
| Some ext -> failure "extension `%s` unsupported" ext
|
2026-02-12 07:09:37 +01:00
|
|
|
| None -> Array.of_list all_supported
|
|
|
|
|
|
|
|
|
|
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
|
2026-03-20 01:13:21 +01:00
|
|
|
let default = Cfg.default_lang
|
|
|
|
|
|
|
|
|
|
let () =
|
|
|
|
|
if not @@ Array.mem default arr then
|
2026-03-26 06:23:42 +01:00
|
|
|
failure "default language `%s` not supported" Cfg.default_lang
|
2026-02-12 07:09:37 +01:00
|
|
|
|
|
|
|
|
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-13 15:56:19 +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-03-20 01:13:21 +01:00
|
|
|
Fpath.to_string Fpath.((v (Cfg.base_dir t) / lang / etag t) + ext)
|
2026-02-12 07:09:37 +01:00
|
|
|
in
|
2025-09-13 15:56:19 +02:00
|
|
|
match Assets_crunch.read path with
|
|
|
|
|
| None -> Fmt.failwith "static file not found: `%s`" path
|
|
|
|
|
| Some data -> data
|