Skip to content

Commit

Permalink
feat(chunked): add polling for uploader state
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrvis committed Dec 5, 2023
1 parent a10dd35 commit 0c0ecb9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/api/akord-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ export default class AkordApi extends Api {
return resource;
};

public async getUploadState(id: string): Promise<{ resourceUri: string[] }> {
try {
const resource = await new ApiClient()
.env(this.config)
.resourceId(id)
.getUploadState()

return resource;
} catch (error) {
return null; // upload state not saved yet
}
};

public async downloadFile(id: string, options: FileGetOptions = {}): Promise<{ fileData: ArrayBuffer | ReadableStream<Uint8Array>, metadata: EncryptionMetadata }> {
const { response } = await new ApiClient()
.env(this.config)
Expand Down
10 changes: 10 additions & 0 deletions src/api/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,16 @@ export class ApiClient {
}
}

async getUploadState(): Promise<{ resourceUri: string[] }> {
if (!this._resourceId) {
throw new BadRequest("Missing resource id to download. Use ApiClient#resourceId() to add it");
}
const data = await this.get(`${this._gatewayurl}/internal/uploader/${this._resourceId}`);
return {
resourceUri: data.resourceUri
}
}

/**
*
* @requires:
Expand Down
2 changes: 2 additions & 0 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ abstract class Api {

abstract uploadFile(file: ArrayBuffer, tags: Tags, options?: FileUploadOptions): Promise<{ resourceUri: string[], resourceLocation: string }>

abstract getUploadState(id: string): Promise<{ resourceUri: string[] }>

abstract uploadData(items: { data: any, tags: Tags }[], cacheOnly?: boolean): Promise<Array<string>>

abstract getContractState(vaultId: string): Promise<ContractState>
Expand Down
13 changes: 13 additions & 0 deletions src/core/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const BYTES_IN_MB = 1000000;
const DEFAULT_CHUNK_SIZE_IN_BYTES = 10 * BYTES_IN_MB;
const MINIMAL_CHUNK_SIZE_IN_BYTES = 5 * BYTES_IN_MB;
const CHUNKS_CONCURRENCY = 25;
const UPLOADER_POLLING_RATE_IN_MILLISECONDS = 2500;


class FileService extends Service {
Expand Down Expand Up @@ -216,6 +217,18 @@ class FileService extends Service {
const fileTags = tags.concat(fileSignatureTags);
const resource = await this.uploadChunk(file, chunkSize, sourceOffset, { digestObject, encryptedKey, targetOffset, tags: fileTags, location: chunkedResource.resourceLocation });

// polling loop
if (!options.cacheOnly) {
const uri = chunkedResource.resourceLocation.split(":")[0];
while (true) {
await new Promise(resolve => setTimeout(resolve, UPLOADER_POLLING_RATE_IN_MILLISECONDS)); const state = await this.api.getUploadState(uri);
if (state && state.resourceUri) {
resource.resourceUri = state.resourceUri;
break;
}
}
}

return {
resourceUri: resource.resourceUri,
numberOfChunks: numberOfChunks,
Expand Down

0 comments on commit 0c0ecb9

Please sign in to comment.