package sihl-storage

  1. Overview
  2. Docs
A Sihl service for storing and retrieving large files

Install

Dune Dependency

Authors

Maintainers

Sources

sihl-queue-0.1.9.tbz
sha256=77f0813d75a88edd14b3396e8b848d94c31c28803299b4b1bd4b78b1de4a2e80
sha512=a8907bc35ea14b7c3a7d638979a2a274860202b2de58b84b5621a4908db001ace493d8aa2e5383f4c8b1847efd256938592f63ef75a41521284b3640d3a7442a

doc/src/sihl-storage/sihl_storage.ml.html

Source file sihl_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
open Lwt.Syntax

module Make (Repo : Sihl.Storage.Sig.REPO) : Sihl.Storage.Sig.SERVICE = struct
  let find_opt ~id = Repo.get_file ~id

  let find ~id =
    let* file = Repo.get_file ~id in
    match file with
    | None -> raise (Sihl.Storage.Exception ("File not found with id " ^ id))
    | Some file -> Lwt.return file
  ;;

  let delete ~id =
    let* file = find ~id in
    let blob_id = Sihl.Storage.StoredFile.blob file in
    let* () = Repo.delete_file ~id:file.file.id in
    Repo.delete_blob ~id:blob_id
  ;;

  let upload_base64 ~file ~base64 =
    let blob_id = Sihl.Database.Id.random () |> Sihl.Database.Id.to_string in
    let* blob =
      match Base64.decode base64 with
      | Error (`Msg msg) ->
        Logs.err (fun m ->
            m
              "STORAGE: Could not upload base64 content of file %a"
              Sihl.Storage.File.pp
              file);
        raise (Sihl.Storage.Exception msg)
      | Ok blob -> Lwt.return blob
    in
    let* () = Repo.insert_blob ~id:blob_id ~blob in
    let stored_file = Sihl.Storage.StoredFile.make ~file ~blob:blob_id in
    let* () = Repo.insert_file ~file:stored_file in
    Lwt.return stored_file
  ;;

  let update_base64 ~file ~base64 =
    let blob_id = Sihl.Storage.StoredFile.blob file in
    let* blob =
      match Base64.decode base64 with
      | Error (`Msg msg) ->
        Logs.err (fun m ->
            m
              "STORAGE: Could not upload base64 content of file %a"
              Sihl.Storage.StoredFile.pp
              file);
        raise (Sihl.Storage.Exception msg)
      | Ok blob -> Lwt.return blob
    in
    let* () = Repo.update_blob ~id:blob_id ~blob in
    let* () = Repo.update_file ~file in
    Lwt.return file
  ;;

  let download_data_base64_opt ~file =
    let blob_id = Sihl.Storage.StoredFile.blob file in
    let* blob = Repo.get_blob ~id:blob_id in
    match Option.map Base64.encode blob with
    | Some (Error (`Msg msg)) ->
      Logs.err (fun m ->
          m
            "STORAGE: Could not get base64 content of file %a"
            Sihl.Storage.StoredFile.pp
            file);
      raise (Sihl.Storage.Exception msg)
    | Some (Ok blob) -> Lwt.return @@ Some blob
    | None -> Lwt.return None
  ;;

  let download_data_base64 ~file =
    let blob_id = Sihl.Storage.StoredFile.blob file in
    let* blob = Repo.get_blob ~id:blob_id in
    match Option.map Base64.encode blob with
    | Some (Error (`Msg msg)) ->
      Logs.err (fun m ->
          m
            "STORAGE: Could not get base64 content of file %a"
            Sihl.Storage.StoredFile.pp
            file);
      raise (Sihl.Storage.Exception msg)
    | Some (Ok blob) -> Lwt.return blob
    | None ->
      raise
        (Sihl.Storage.Exception
           (Format.asprintf
              "File data not found for file %a"
              Sihl.Storage.StoredFile.pp
              file))
  ;;

  let start () = Lwt.return ()
  let stop () = Lwt.return ()
  let lifecycle = Sihl.Core.Container.Lifecycle.create "storage" ~start ~stop

  let register () =
    Repo.register_migration ();
    Repo.register_cleaner ();
    Sihl.Core.Container.Service.create lifecycle
  ;;
end

module Repo = Repo
OCaml

Innovation. Community. Security.