-
-
Notifications
You must be signed in to change notification settings - Fork 393
/
Files.js
37 lines (29 loc) · 1.16 KB
/
Files.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
export async function uploadFile(fileService, buffer, name, ext, chunkSize, vault, options = {}) {
const byteLength = options.byteLengthOverwrite || buffer.byteLength
const operation = await fileService.beginNewFileUpload(byteLength, vault)
if (isClientDisplayableError(operation)) {
return operation
}
let chunkId = 1
for (let i = 0; i < buffer.length; i += chunkSize) {
const readUntil = i + chunkSize > buffer.length ? buffer.length : i + chunkSize
const chunk = buffer.slice(i, readUntil)
const isFinalChunk = readUntil === buffer.length
const error = await fileService.pushBytesForUpload(operation, chunk, chunkId++, isFinalChunk)
if (error) {
throw new Error('Could not upload file chunk')
}
}
const file = await fileService.finishUpload(operation, name, ext)
return file
}
export async function downloadFile(fileService, file) {
let receivedBytes = new Uint8Array()
const error = await fileService.downloadFile(file, (decryptedBytes) => {
receivedBytes = new Uint8Array([...receivedBytes, ...decryptedBytes])
})
if (error) {
throw new Error('Could not download file', error.text)
}
return receivedBytes
}