blob: 9fa6c4a65b9ee56a43d7b622fde7118dde27475b (
plain)
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
|
(ns aome.org-file
(:require [shadow.cljs.modern :as scmodern]
[reagent.core :as r]
["openpgp" :as pgp]))
(def current-file-name (r/atom ""))
(def current-file-extension (r/atom ".org"))
(def content (r/atom ""))
(defn- decrypt-string [bytes callback]
(scmodern/js-await [pgp-message (pgp/readMessage #js {"binaryMessage" bytes})]
(scmodern/js-await [decrypted (pgp/decrypt #js {"message" pgp-message
"passwords" (.prompt js/window "Passphrase:" "")})]
(callback (-> decrypted
js->clj
(get "data"))))))
(defn load-from-file-picker! []
(scmodern/js-await [fileHandles (.showOpenFilePicker js/window (clj->js opts))]
(let [fileHandle (first fileHandles)]
(scmodern/js-await [file (.getFile fileHandle)]
(if-let [split-name (->> (.-name file)
(re-seq #"([A-z0-9-_]*)((\.[A-z]*){1,2})$")
first)]
(do (reset! current-file-name (nth split-name 1))
(reset! current-file-extension (nth split-name 2))))
(if (= @current-file-extension ".org.gpg")
(scmodern/js-await [file-content-bytes (.bytes file)]
(decrypt-string file-content-bytes #(reset! content %)))
(scmodern/js-await [file-content (.text file)]
(reset! content file-content)))))))
(defn- encrypt-string [s callback]
(scmodern/js-await [pgp-message (pgp/createMessage #js {"text" s})]
(scmodern/js-await [encrypted (pgp/encrypt #js {"message" pgp-message
"passwords" (.prompt js/window "Passphrase:" "")
"format" "binary"})]
(callback encrypted))))
(defn save-to-file-picker! []
(scmodern/js-await [fileHandle (.showSaveFilePicker js/window #js {"suggestedName" (str @current-file-name @current-file-extension)})]
(scmodern/js-await [writable (.createWritable fileHandle)]
(if (= @current-file-extension ".org.gpg")
(encrypt-string @content
#(scmodern/js-await [_ (.write writable %)]
(scmodern/js-await [_ (.close writable)])))
(scmodern/js-await [_ (.write writable @content)]
(scmodern/js-await [_ (.close writable)]))))))
|