-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
49 lines (46 loc) · 1.27 KB
/
utils.js
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
const urlToBlob = async (hash) => {
let res = await fetch(`/api/torrent/${hash}`)
let reader = res.body.getReader()
let strm = new ReadableStream({
async start(controller) {
while (true) {
const { done, value } = await reader.read()
if (done) {
break
}
controller.enqueue(value)
}
controller.close()
reader.releaseLock()
}
})
let respone = new Response(strm)
let blob = await respone.blob()
let url = await URL.createObjectURL(blob)
return url
}
const imgToBlob = async (slug) => {
let res = await fetch(`/api/image/${slug}`)
let reader = res.body.getReader()
let strm = new ReadableStream({
async start(controller) {
while (true) {
const { done, value } = await reader.read()
if (done) {
break
}
controller.enqueue(value)
}
controller.close()
reader.releaseLock()
}
})
let respone = new Response(strm)
let blob = await respone.blob()
let url = await URL.createObjectURL(blob)
return url
}
module.exports = {
urlToBlob,
imgToBlob
}