package sihl

  1. Overview
  2. Docs

Source file contract_storage.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
type file =
  { id : string
  ; filename : string
  ; filesize : int
  ; mime : string
  }

type stored =
  { file : file
  ; blob : string
  }

let name = "storage"

exception Exception of string

module type Sig = sig
  (** Get the meta data of a complete file.

      This will not download the content, use [get_data_base64] for that. *)
  val find_opt : id:string -> stored option Lwt.t

  val find : id:string -> stored Lwt.t
  val delete : id:string -> unit Lwt.t

  (** Upload base64 string as data content for [file]. *)
  val upload_base64 : file -> base64:string -> stored Lwt.t

  (** Upload and overwrite base64 strong content of [file]. *)
  val update_base64 : stored -> base64:string -> stored Lwt.t

  (** Download actual file content for [file]. *)
  val download_data_base64_opt : stored -> string option Lwt.t

  val download_data_base64 : stored -> string Lwt.t
  val register : unit -> Core_container.Service.t

  include Core_container.Service.Sig
end

(* Common *)

let file_to_sexp { id; filename; filesize; mime } =
  let open Sexplib0.Sexp_conv in
  let open Sexplib0.Sexp in
  List
    [ List [ Atom "id"; sexp_of_string id ]
    ; List [ Atom "filename"; sexp_of_string filename ]
    ; List [ Atom "filesize"; sexp_of_int filesize ]
    ; List [ Atom "mime"; sexp_of_string mime ]
    ]
;;

let pp_file fmt t = Sexplib0.Sexp.pp_hum fmt (file_to_sexp t)
let set_mime mime file = { file with mime }
let set_filesize filesize file = { file with filesize }
let set_filename filename file = { file with filename }

let set_mime_stored mime stored_file =
  { stored_file with file = set_mime mime stored_file.file }
;;

let set_filesize_stored size stored_file =
  { stored_file with file = set_filesize size stored_file.file }
;;

let set_filename_stored name stored_file =
  { stored_file with file = set_filename name stored_file.file }
;;

let stored_to_sexp { file; _ } =
  let open Sexplib0.Sexp_conv in
  let open Sexplib0.Sexp in
  List
    [ List [ Atom "file"; file_to_sexp file ]
    ; List [ Atom "blob"; sexp_of_string "<binary>" ]
    ]
;;

let pp_stored fmt t = Sexplib0.Sexp.pp_hum fmt (stored_to_sexp t)
OCaml

Innovation. Community. Security.