do not load array buffer into memory

This commit is contained in:
Lucy Cifferello
2023-06-14 15:47:41 -06:00
committed by Aiden McClelland
parent f29c7ba4f2
commit fd166c4433
4 changed files with 19 additions and 14 deletions

View File

@@ -95,8 +95,9 @@ export class SideloadPage {
manifest: this.toUpload.manifest!,
icon: this.toUpload.icon!,
})
const buffer = await blobToBuffer(this.toUpload.file!)
this.api.uploadPackage(guid, buffer).catch(e => console.error(e))
this.api
.uploadPackage(guid, this.toUpload.file!)
.catch(e => console.error(e))
this.navCtrl.navigateRoot('/services')
} catch (e: any) {
@@ -190,20 +191,24 @@ async function readBlobAsDataURL(
}
async function blobToDataURL(data: Blob | File): Promise<string> {
const res = await readBlobAsDataURL(data)
if (res instanceof ArrayBuffer)
if (res instanceof ArrayBuffer) {
throw new Error('readBlobAsDataURL response should not be an array buffer')
if (res == null)
}
if (res == null) {
throw new Error('readBlobAsDataURL response should not be null')
}
if (typeof res === 'string') return res
throw new Error('no possible blob to data url resolution found')
}
async function blobToBuffer(data: Blob | File): Promise<ArrayBuffer> {
const res = await readBlobToArrayBuffer(data)
if (res instanceof String)
if (res instanceof String) {
throw new Error('readBlobToArrayBuffer response should not be a string')
if (res == null)
}
if (res == null) {
throw new Error('readBlobToArrayBuffer response should not be null')
}
if (res instanceof ArrayBuffer) return res
throw new Error('no possible blob to array buffer resolution found')
}

View File

@@ -1,6 +1,6 @@
import { BehaviorSubject, Observable } from 'rxjs'
import { Update } from 'patch-db-client'
import { RR, Encrypted } from './api.types'
import { Encrypted, RR } from './api.types'
import { DataModel } from 'src/app/services/patch-db/data-model'
import { Log } from '@start9labs/shared'
import { WebSocketSubjectConfig } from 'rxjs/webSocket'
@@ -30,7 +30,7 @@ export abstract class ApiService {
abstract getStatic(url: string): Promise<string>
// for sideloading packages
abstract uploadPackage(guid: string, body: ArrayBuffer): Promise<string>
abstract uploadPackage(guid: string, body: Blob): Promise<string>
// db

View File

@@ -44,7 +44,7 @@ export class LiveApiService extends ApiService {
}
// for sideloading packages
async uploadPackage(guid: string, body: ArrayBuffer): Promise<string> {
async uploadPackage(guid: string, body: Blob): Promise<string> {
return this.httpRequest({
method: Method.POST,
body,

View File

@@ -1,12 +1,12 @@
import { Injectable } from '@angular/core'
import { pauseFor, Log } from '@start9labs/shared'
import { Log, pauseFor } from '@start9labs/shared'
import { ApiService } from './embassy-api.service'
import {
PatchOp,
Update,
Operation,
RemoveOperation,
PatchOp,
pathFromArray,
RemoveOperation,
Update,
} from 'patch-db-client'
import {
DataModel,
@@ -87,7 +87,7 @@ export class MockApiService extends ApiService {
return markdown
}
async uploadPackage(guid: string, body: ArrayBuffer): Promise<string> {
async uploadPackage(guid: string, body: Blob): Promise<string> {
await pauseFor(2000)
return 'success'
}