put all image stuff in image.ml, make thumbnail for avatars

This commit is contained in:
Swrup 2022-04-04 14:39:25 +02:00
parent dbc2139511
commit 9ca17a8840
7 changed files with 240 additions and 186 deletions

View file

@ -11,8 +11,6 @@ let db = Filename.concat db_root "permap.db"
let db_uri = Format.sprintf "sqlite3://%s" db
let random_state = Random.State.make_self_init ()
module Db =
(val Caqti_blocking.connect (Uri.of_string db_uri) |> Caqti_blocking.or_fail)
@ -34,39 +32,3 @@ let () =
| Error _e ->
Format.eprintf "db error@\n";
exit 1
let mime =
let database = Conan.Process.database ~tree:Conan_light.tree in
fun content ->
match Conan_string.run ~database content with
| Ok m -> Conan.Metadata.mime m
| Error _ -> None
let clean_image image =
let max_name = 1000 in
let max_alt = 3000 in
let max_content = 4200000 in
let name, alt, content = image in
let name =
match name with
| Some name -> Dream.html_escape name
| None ->
(* make up random name if no name was given *)
Uuidm.to_string (Uuidm.v4_gen random_state ())
in
let alt = if String.trim alt = "" then name else alt in
if String.length name > max_name then
Error (Format.sprintf "Image name too long: More than %dB" max_name)
else if String.length alt > max_alt then
Error (Format.sprintf "Image description too long: More than %dB" max_alt)
else if String.length content > max_content then
Error (Format.sprintf "Image size too big: More than %dB" max_content)
else
match mime content with
| None -> Error "invalid image type"
| Some mime -> (
match mime with
| "image/jpeg" | "image/png" | "image/webp" -> Ok (name, alt, content)
| _unsupported_mime_type ->
Error (Format.sprintf "unsupported image type: %s" mime) )