-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBlobStore.jl
More file actions
381 lines (323 loc) · 10.5 KB
/
BlobStore.jl
File metadata and controls
381 lines (323 loc) · 10.5 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
const UPLOAD_CHUNK_SIZE_HASH = 5*1024*1024
#TODO we can also extend the blobstore
struct NavAbilityBlobStore <: DFG.AbstractBlobStore{Vector{UInt8}}
client::NavAbilityClient
label::Symbol
end
NavAbilityBlobStore(client::NavAbilityClient) = NavAbilityBlobStore(client, :default)
NavAbilityBlobStore(client::GQL.Client, userLabel::String, label = :default) =
error("Deprecated, use NavAbilityBlobStore(client::NavAbilityClient, label::Symbol)")
function Base.show(io::IO, ::MIME"text/plain", s::NavAbilityBlobStore)
summary(io, s)
print(io, "\n ")
show(io, MIME("text/plain"), s.client)
println(io, "\n label: ", s.label)
end
function NavAbilityBlobStore(fgclient::NavAbilityDFG, label::Symbol = :default)
NavAbilityBlobStore(fgclient.client, label)
end
function executeGql(store::NavAbilityBlobStore, query::AbstractString, variables, T::Type = Any; kwargs...)
executeGql(store.client, query, variables, T; kwargs...)
end
struct NavAbilityCachedBlobStore{T <: DFG.AbstractBlobStore} <:
DFG.AbstractBlobStore{Vector{UInt8}}
key::Symbol
localstore::T
remotestore::NavAbilityBlobStore
end
function NavAbilityCachedBlobStore(localstore::DFG.AbstractBlobStore, remotestore::NavAbilityBlobStore)
return NavAbilityCachedBlobStore(:default_nva_cached, localstore, remotestore)
end
"""
$(SIGNATURES)
Request URLs for data blob download.
Args:
store (NavAbilityBlobStore): The NavAbility blob store.
blobId (String): The unique file identifier of the data blob.
"""
function createDownload(store::NavAbilityBlobStore, blobId::UUID)
response = executeGql(
store.client,
MUTATION_CREATE_DOWNLOAD,
(blobId = string(blobId), label=store.label);
)
return response.data["createDownload"]
end
function DFG.getBlob(blobstore::NavAbilityBlobStore, blobId::UUID)
url = createDownload(blobstore, blobId)
io = PipeBuffer()
Downloads.download(url, io)
return io |> take!
end
function DFG.getBlob(blobstore::NavAbilityCachedBlobStore, blobId::UUID)
if hasBlob(blobstore.localstore, blobId)
blob = getBlob(blobstore.localstore, blobId)
else
@info "missed in cache, caching" blobId
blob = getBlob(blobstore.remotestore, blobId)
addBlob!(blobstore.localstore, blobId, blob)
end
return blob
end
function DFG.listBlobs(store::NavAbilityBlobStore)
response = executeGql(
store,
QUERY_LIST_BLOBS,
(label = store.label,),
Vector{String},
)
list = response.data["listBlobs"]
return UUID.(list)
end
function DFG.hasBlob(store::NavAbilityBlobStore, blobId::UUID)
response = executeGql(
store,
QUERY_HAS_BLOB,
(blobId = string(blobId), label = store.label),
Bool;
)
return response.data["hasBlob"]
end
## =========================================================================
## Upload
## =========================================================================
"""
$(SIGNATURES)
Request URLs for data blob upload.
Args:
navAbilityClient (NavAbilityClient): The NavAbility client.
blobId: The unique file identifier of the data blob.
parts (Int): Split upload into multiple blob parts, FIXME currently only supports parts=1.
"""
function createUpload(
nvastore::NavAbilityBlobStore,
blobId::UUID,
parts::Int = 1,
)
#
store = (label=nvastore.label, type="NVA_CLOUD")
response = executeGql(
nvastore.client,
GQL_CREATE_UPLOAD,
(blobId=blobId, parts=parts, store=store)
)
return response.data["createUpload"]
end
## Complete the upload
function completeUpload(
client::NavAbilityClient,
blobId::UUID,
uploadId::AbstractString,
eTags::AbstractVector{<:AbstractString},
)
# CompletedUploadPartInput
parts = Vector{Dict{String,Any}}()
for (pn,eTag) in enumerate(eTags)
push!(parts,
Dict{String,Any}(
"partNumber" => pn,
"eTag" => eTag,
)
)
end
# CompletedUploadInput
cui = Dict{String,Any}(
"uploadId" => uploadId,
"parts" => parts
)
response = executeGql(
client,
GQL_COMPLETEUPLOAD,
(blobId = blobId, completedUpload = cui)
)
return response.data["completeUpload"]
end
function completeUploadSingle(
client::NavAbilityClient,
blobId::UUID,
uploadId::AbstractString,
eTag::AbstractString,
)
response = executeGql(
client,
GQL_COMPLETEUPLOAD_SINGLE,
(blobId = blobId, uploadId = uploadId, eTag = eTag),
)
return response.data["completeUpload"]
end
##
function DFG.addBlob!(
store::NavAbilityBlobStore,
filepath::AbstractString,
blobId::UUID = uuid4();
chunkSize::Integer = UPLOAD_CHUNK_SIZE_HASH,
mimeType::String = "application/octet-stream",
)
# locate large file on fs, ready to read in chunks
fid = open(filepath,"r")
# calculate number or parts necessary
nparts = ceil(Int, filesize(filepath) / chunkSize)
# create the upload url destination
crUp = createUpload(store, blobId, nparts)
# recover uploadId for later completion
uploadId = crUp["uploadId"]
# custom header for pushing the file up
headers_ = [
"Content-Length" => filesize(filepath),
"Content-Type" => mimeType,
"Accept" => "application/json, text/plain, */*",
"Accept-Encoding" => "gzip, deflate, br",
"Sec-Fetch-Dest" => "empty",
"Sec-Fetch-Mode" => "cors",
"Sec-Fetch-Site" => "cross-site",
"Sec-GPC" => 1,
"Connection" => "keep-alive",
]
# recover all the eTags for later completion of upload
eTags = Vector{String}()
for (np,url_) in enumerate(crUp["parts"])
# recover nparts-many urls from API response
url = url_["url"]
# read chunk from file
chunk = Vector{UInt8}()
sz = readbytes!(fid,chunk,chunkSize)
# upload each chunk with header CONTENT_LENGTH
headers = vcat(
"Content-Length" => sz,
headers_
)
# recover eTag from each successful upload
resp = HTTP.put(url, headers, chunk)
# Extract eTag
eTag = match(r"[a-zA-Z0-9]+", resp["eTag"]).match
push!(eTags, eTag)
end
# close file
close(fid)
# close out the upload
res = completeUpload(
store.client,
blobId,
uploadId,
eTags
)
res == "Accepted" ? nothing : @error("Unable to upload blob, $res")
blobId
end
function getMimetype(io::IO)
getFormat(s::DFG.FileIO.Stream{T}) where T = T
stream = DFG.FileIO.query(io)
# not sure if we need restrict to only our mimetypes, but better than nothing
mime = findfirst(==(getFormat(stream)), DFG._MIMETypes)
if isnothing(mime)
return MIME("application/octet-stream")
else
return mime
end
end
function DFG.addBlob!(store::NavAbilityBlobStore, blobId::UUID, blob::Vector{UInt8})
client = store.client
mimeType = getMimetype(IOBuffer(blob))
filesize = length(blob)
# TODO: Use about a 50M file part here.
np = 1 # TODO: ceil(filesize / 50e6)
# create the upload url destination
d = createUpload(store, blobId, np)
url = d["parts"][1]["url"]
uploadId = d["uploadId"]
# custom header for pushing the file up
headers = [
"Content-Length" => filesize,
"Content-Type" => string(mimeType),
"Accept" => "application/json, text/plain, */*",
"Accept-Encoding" => "gzip, deflate, br",
"Sec-Fetch-Dest" => "empty",
"Sec-Fetch-Mode" => "cors",
"Sec-Fetch-Site" => "cross-site",
"Sec-GPC" => 1,
"Connection" => "keep-alive",
]
#
resp = HTTP.put(url, headers, blob)
# Extract eTag
eTag = match(r"[a-zA-Z0-9]+", resp["eTag"]).match
# close out the upload
res = completeUploadSingle(client, blobId, uploadId, eTag)
res == "Accepted" ? nothing : @error("Unable to upload blob, $res")
return UUID(blobId)
end
function DFG.addBlob!(
blobstore::NavAbilityCachedBlobStore,
blobId::UUID,
blob::Vector{UInt8},
)
addBlob!(blobstore.remotestore, blobId, blob)
addBlob!(blobstore.localstore, blobId, blob)
return blobId
end
function DFG.deleteBlob!(
blobstore::NavAbilityBlobStore,
blobId::UUID
)
response = executeGql(
blobstore.client,
MUTATION_DELETE_BLOB,
(blobId = string(blobId), label = string(blobstore.label));
)
return response.data["deleteBlob"]
end
##==========================================================================================
## NavAbility™ Blob Store Deployed on Premise
##==========================================================================================
struct NavAbilityOnPremBlobStore <: DFG.AbstractBlobStore{Vector{UInt8}}
client::NavAbilityClient
label::Symbol
end
function NavAbilityOnPremBlobStore(fgclient::NavAbilityDFG, label=:default)
NavAbilityOnPremBlobStore(fgclient.client, label)
end
function DFG.addBlob!(store::NavAbilityOnPremBlobStore, blobId::UUID, blob::Vector{UInt8})
b64blob = base64encode(blob)
response = NvaSDK.GQL.mutate(
store.client.client,
"addBlobFS",
Dict("storeLabel" => string(store.label), "blobId" => string(blobId), "input" => b64blob);
throw_on_execution_error = true,
)
blobId_str = response.data["addBlobFS"]
blobId = tryparse(UUID, blobId_str)
isnothing(blobId) && error(blobId_str)
return blobId
end
function DFG.getBlob(store::NavAbilityOnPremBlobStore, blobId::UUID)
response = executeGql(
store.client,
QUERY_GET_BLOB,
(id = string(blobId), storeLabel = string(store.label))
)
#FIXME Errors not working as expected
if startswith(response.data["getBlob"], "500 Internal Error") ||
startswith(response.data["getBlob"], "$blobId not found")
error(response.data["getBlob"])
end
return base64decode(response.data["getBlob"])
end
function DFG.hasBlob(store::NavAbilityOnPremBlobStore, blobId::UUID)
response = executeGql(
store.client,
QUERY_HAS_BLOB,
(blobId = string(blobId), label = store.label, type = "NVA_ON_PREM"),
Bool;
)
return response.data["hasBlob"]
end
function DFG.listBlobs(store::NavAbilityOnPremBlobStore)
response = executeGql(
store.client,
QUERY_LIST_BLOBS,
(label = store.label, type = "NVA_ON_PREM"),
Vector{String},
)
list = response.data["listBlobs"]
return UUID.(list)
end