-
Notifications
You must be signed in to change notification settings - Fork 996
/
Copy pathimage_server.cljs
300 lines (275 loc) · 10.3 KB
/
image_server.cljs
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
(ns utils.image-server
(:require
[quo.foundations.colors :as colors]
[react-native.fs :as utils.fs]
[react-native.platform :as platform]
[schema.core :as schema]
[utils.datetime :as datetime]))
(def ^:const image-server-uri-prefix "https://localhost:")
(def ^:const account-images-action "/accountImages")
(def ^:const account-initials-action "/accountInitials")
(def ^:const contact-images-action "/contactImages")
(def ^:const generate-qr-action "/GenerateQRCode")
(defn get-font-file-ready
"setup font file and get the absolute path to it
this font file is passed to status-go later to render the initials avatar
for ios, it's located at main-bundle-path
for android, it's located in the assets dir which can not be accessed by status-go
so we copy one to the cache directory"
[font-file-name callback]
(if platform/android?
(let [cache-dir (utils.fs/cache-dir)
font-file-name font-file-name
src (str "fonts/" font-file-name)
dest (str cache-dir "/" font-file-name)
copy #(utils.fs/copy-assets src dest)
cb #(callback dest)]
(.then (utils.fs/file-exists? dest)
(fn [file?]
(if file?
(cb)
(.then (copy) cb)))))
(callback (str (utils.fs/main-bundle-path)
"/"
font-file-name))))
(defn timestamp [] (datetime/timestamp))
(defn current-theme-index
[theme]
(case theme
:light 1
:dark 2))
(defn correction-level->index
[level]
(case (keyword level)
:low 1
:medium 2
:quart 3
:highest 4
4))
(defn get-account-image-uri
"fn to get the avatar uri when multiaccount has custom image set
not directly called, check `get-account-image-uri-fn`
color formats (for all color options):
#RRGGBB
#RRGGBBAA
rgb(255,255,255)
rgba(255,255,255,0.1) note alpha is 0-1
non-placeholder-avatar: requires at least one of `public-key` or `key-uid`
placeholder-avatar: pass image file path as `image-name`
`indicator-size` is outer indicator radius
`indicator-size` - `indicator-border` is inner indicator radius"
[{:keys [port public-key image-name key-uid size theme indicator-size
indicator-border indicator-center-to-edge indicator-color ring?
ring-width ratio]}]
(str
image-server-uri-prefix
port
account-images-action
"?publicKey="
public-key
"&keyUid="
key-uid
"&imageName="
image-name
"&size="
(Math/round (* size ratio))
"&theme="
(current-theme-index theme)
"&clock="
(timestamp)
"&indicatorColor="
(js/encodeURIComponent indicator-color)
"&indicatorSize="
(* indicator-size ratio)
"&indicatorBorder="
(* indicator-border ratio)
"&indicatorCenterToEdge="
(* indicator-center-to-edge ratio)
"&addRing="
(if ring? 1 0)
"&ringWidth="
(* ring-width ratio)))
(defn get-account-image-uri-fn
"pass the result fn to user-avatar component as `:profile-picture`
use this fn in subs to set multiaccount `:images` as [{:fn ...}]
pass the image to user-avatar
user-avatar can fill the rest style related options
set `override-ring?` to a non-nil value to override `ring?`, mainly used to
hide ring for account with ens name
check `get-account-image-uri` for color formats"
[{:keys [port public-key key-uid image-name theme override-ring? ratio]}]
(fn [{:keys [size indicator-size indicator-border indicator-center-to-edge
indicator-color ring? ring-width override-theme]}]
(get-account-image-uri
{:port port
:image-name image-name
:size size
:public-key public-key
:ratio ratio
:key-uid key-uid
:theme (if (nil? override-theme) theme override-theme)
:indicator-size indicator-size
:indicator-border indicator-border
:indicator-center-to-edge indicator-center-to-edge
:indicator-color indicator-color
:ring? (if (nil? override-ring?) ring? override-ring?)
:ring-width ring-width})))
(defn get-initials-avatar-uri
"fn to get the avatar uri when account/contact/placeholder has no custom pic set
not directly called, check `get-account-initials-uri-fn`
multiaccount: at least one of `key-uid`, `public-key` is required to render the ring
contact: `public-key` is required to render the ring
check `get-account-image-uri` for color formats
check `get-font-file-ready` for `font-file`
`uppercase-ratio` is the uppercase-height/line-height for `font-file`"
[{:keys [port public-key key-uid theme ring? length size customization-color
color font-size font-file uppercase-ratio indicator-size
indicator-border indicator-center-to-edge indicator-color full-name
ring-width ratio]}]
(str
image-server-uri-prefix
port
account-initials-action
"?publicKey="
public-key
"&keyUid="
key-uid
"&length="
length
"&size="
(Math/round (* size ratio))
"&bgColor="
(js/encodeURIComponent (colors/resolve-color customization-color theme))
"&color="
(js/encodeURIComponent color)
"&fontSize="
(* font-size ratio)
"&fontFile="
(js/encodeURIComponent font-file)
"&uppercaseRatio="
uppercase-ratio
"&theme="
(current-theme-index theme)
"&clock="
"&name="
(js/encodeURIComponent full-name)
(timestamp)
"&indicatorColor="
(js/encodeURIComponent indicator-color)
"&indicatorSize="
(* indicator-size ratio)
"&indicatorBorder="
(* indicator-border ratio)
"&indicatorCenterToEdge="
(* indicator-center-to-edge ratio)
"&addRing="
(if ring? 1 0)
"&ringWidth="
(* ring-width ratio)))
(schema/=> get-initials-avatar-uri
[:=>
[:cat
[:map
[:color string?]
[:theme :schema.common/theme]
[:size number?]
[:ratio float?]
[:uppercase-ratio number?]
[:customization-color :schema.common/customization-color]
[:font-size number?]
[:font-file string?]]]
[:string]])
(defn get-initials-avatar-uri-fn
"return a fn that calls `get-account-initials-uri`
pass the fn to user-avatar component to fill the style related options
check `get-account-image-uri` for color formats
check `get-font-file-ready` for `font-file`
check `get-account-image-uri-fn` for `override-ring?`"
[{:keys [port public-key key-uid theme override-ring? font-file ratio
uppercase-ratio customization-color]}]
(fn [{:keys [full-name length size font-size color indicator-size indicator-border
indicator-color indicator-center-to-edge ring? ring-width
override-theme]}]
(get-initials-avatar-uri
{:port port
:public-key public-key
:ratio ratio
:key-uid key-uid
:full-name full-name
:length length
:size size
:customization-color customization-color
:theme (if (nil? override-theme) theme override-theme)
:ring? (if (nil? override-ring?) ring? override-ring?)
:ring-width ring-width
:font-size font-size
:color color
:font-file font-file
:uppercase-ratio uppercase-ratio
:indicator-size indicator-size
:indicator-border indicator-border
:indicator-center-to-edge indicator-center-to-edge
:indicator-color indicator-color})))
(defn get-contact-image-uri
[{:keys [port public-key image-name clock theme indicator-size indicator-border
indicator-center-to-edge indicator-color size ring? ring-width ratio]}]
(str
image-server-uri-prefix
port
contact-images-action
"?publicKey="
public-key
"&imageName="
image-name
"&size="
(Math/round (* size ratio))
"&theme="
(current-theme-index theme)
"&clock="
clock
"&indicatorColor="
(js/encodeURIComponent indicator-color)
"&indicatorSize="
(* indicator-size ratio)
"&indicatorBorder="
(* indicator-border ratio)
"&indicatorCenterToEdge="
(* indicator-center-to-edge ratio)
"&addRing="
(if ring? 1 0)
"&ringWidth="
(* ring-width ratio)))
(defn get-contact-image-uri-fn
[{:keys [port public-key image-name theme override-ring? clock ratio]}]
(fn [{:keys [size indicator-size indicator-border indicator-center-to-edge
indicator-color ring? ring-width override-theme]}]
(get-contact-image-uri {:port port
:ratio ratio
:image-name image-name
:public-key public-key
:size size
:theme (if (nil? override-theme) theme override-theme)
:clock clock
:indicator-size indicator-size
:indicator-border indicator-border
:indicator-center-to-edge indicator-center-to-edge
:indicator-color indicator-color
:ring? (if (nil? override-ring?) ring? override-ring?)
:ring-width ring-width})))
(defn get-qr-image-uri-for-any-url
[{:keys [url port qr-size error-level]}]
(let [qr-url-base64 (js/btoa url)
error-correction-level (correction-level->index error-level)
superimpose-profile? false
media-server-url (str image-server-uri-prefix
port
generate-qr-action
"?level="
error-correction-level
"&url="
qr-url-base64
"&allowProfileImage="
superimpose-profile?
"&size="
qr-size)]
media-server-url))