-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.v
92 lines (82 loc) · 1.82 KB
/
stream.v
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
module cdv
import io
import encoding.base64
import x.json2 as json
@[params]
pub struct IOStreamParams {
pub:
writer ?io.Writer
reader ?io.Reader
offset ?int
size ?int
}
pub fn (mut page Page) handle_stream_opt(handle string, opts IOStreamParams) ! {
for {
res := page.io_read(handle, offset: opts.offset, size: opts.size)
mut buf := []u8{}
if res.base64_encoded {
buf = base64.decode(res.data)
} else {
buf = res.data.bytes()
}
if mut writer := opts.writer {
writer.write(buf)!
}
if mut reader := opts.reader {
reader.read(mut buf)!
}
if res.eof {
page.io_close(handle)
break
}
}
}
pub fn (mut page Page) handle_stream(handle string, opts IOStreamParams) {
page.handle_stream_opt(handle, opts) or { page.noop(err) }
}
@[params]
pub struct IOReadParams {
pub:
offset ?int
size ?int
}
pub struct IORead {
pub:
base64_encoded bool
data string
eof bool
}
pub fn (mut page Page) io_read_opt(handle string, opts IOReadParams) !IORead {
mut params := map[string]json.Any{}
params['handle'] = handle
if offset := opts.offset {
params['offset'] = offset
}
if size := opts.size {
params['size'] = size
}
res := page.send_or_noop('IO.read', params: params).result
return IORead{
base64_encoded: res['base64Encoded'] or { false }.bool()
data: res['data']!.str()
eof: res['eof']!.bool()
}
}
pub fn (mut page Page) io_read(handle string, opts IOReadParams) IORead {
return page.io_read_opt(handle, opts) or { page.noop(err) }
}
pub fn (mut page Page) io_close(handle string) {
page.send_or_noop('IO.close',
params: {
'handle': handle
}
)
}
pub fn (mut page Page) resolve_blob(object_id string) string {
uuid := page.send_or_noop('IO.resolveBlob',
params: {
'objectId': object_id
}
).result['uuid'] or { '' }.str()
return uuid
}