* chore: add quota usage e2e tests * chore: add moving from shared to shared vault quota e2e test * chore: fix test suite name * chore: fix awaiting for notifications to propagate * chore: fix awaiting for notifications processing
38 lines
1.2 KiB
JavaScript
38 lines
1.2 KiB
JavaScript
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
|
|
}
|