From cf1fd463c84133233b7b42603877d833e44964f3 Mon Sep 17 00:00:00 2001 From: Perfect Makanju Date: Fri, 29 Jan 2021 20:09:49 +0100 Subject: [PATCH] Feature: Add upload callback and Add Blob to AddFileItem.data types (#29) --- etc/sdk.api.md | 3 ++- packages/storage/src/types.ts | 8 +++++++- packages/storage/src/userStorage.spec.ts | 4 ++-- packages/storage/src/userStorage.ts | 2 +- packages/storage/src/utils/pathUtils.spec.ts | 4 ++++ 5 files changed, 16 insertions(+), 5 deletions(-) diff --git a/etc/sdk.api.md b/etc/sdk.api.md index 0fe6b72..27ec933 100644 --- a/etc/sdk.api.md +++ b/etc/sdk.api.md @@ -12,9 +12,10 @@ import { UserAuth } from '@textile/hub'; // @public (undocumented) export interface AddItemFile { // (undocumented) - data: ReadableStream | ArrayBuffer | string; + data: ReadableStream | ArrayBuffer | string | Blob; mimeType: string; path: string; + progress?: (bytesRead?: number) => void; } // @public (undocumented) diff --git a/packages/storage/src/types.ts b/packages/storage/src/types.ts index e509ce7..8647f87 100644 --- a/packages/storage/src/types.ts +++ b/packages/storage/src/types.ts @@ -124,7 +124,13 @@ export interface AddItemFile { * */ mimeType: string; - data: ReadableStream | ArrayBuffer | string; + data: ReadableStream | ArrayBuffer | string | Blob; + /** + * progress callback if provided will be called with bytes written to + * remote while uploading the file. + * + */ + progress?: (bytesRead?: number) => void; } export interface AddItemsRequest { diff --git a/packages/storage/src/userStorage.spec.ts b/packages/storage/src/userStorage.spec.ts index 970fe9d..7c95fb0 100644 --- a/packages/storage/src/userStorage.spec.ts +++ b/packages/storage/src/userStorage.spec.ts @@ -271,7 +271,7 @@ describe('UserStorage', () => { it('should publish data, error and done events correctly', async () => { const { storage, mockBuckets } = initStubbedStorage(); const uploadError = new Error('update is non-fast-forward'); - when(mockBuckets.pushPath('myBucketKey', anyString(), anything())).thenResolve({ + when(mockBuckets.pushPath('myBucketKey', anyString(), anything(), anything())).thenResolve({ ...mock(), }); @@ -301,7 +301,7 @@ describe('UserStorage', () => { }); // fail upload of b.txt - when(mockBuckets.pushPath('myBucketKey', '/b.txt', anything())).thenReject(uploadError); + when(mockBuckets.pushPath('myBucketKey', '/b.txt', anything(), anything())).thenReject(uploadError); const callbackData = { data: [] as AddItemsEventData[], error: [] as AddItemsEventData[], diff --git a/packages/storage/src/userStorage.ts b/packages/storage/src/userStorage.ts index b7c3231..2f8bf9a 100644 --- a/packages/storage/src/userStorage.ts +++ b/packages/storage/src/userStorage.ts @@ -483,7 +483,6 @@ export class UserStorage { }; try { - await client.pushPath(rootKey, path, file.data); const metadata = await metadataStore.upsertFileMetadata({ uuid: v4(), mimeType: file.mimeType, @@ -491,6 +490,7 @@ export class UserStorage { dbId: bucket.dbId, path, }); + await client.pushPath(rootKey, path, file.data, { progress: file.progress }); // set file entry const existingFile = await client.listPath(rootKey, path); const [fileEntry] = UserStorage.parsePathItems( diff --git a/packages/storage/src/utils/pathUtils.spec.ts b/packages/storage/src/utils/pathUtils.spec.ts index b319542..f167131 100644 --- a/packages/storage/src/utils/pathUtils.spec.ts +++ b/packages/storage/src/utils/pathUtils.spec.ts @@ -90,6 +90,10 @@ describe('pathUtils', () => { input: '/ipfs/bafybeifyipelgeu75bzjnrw5l5xpp4nmllh3owzk5o7qci7gtatstgdoam/top.txt', output: '/top.txt', }, + { + input: '/ipfs/bafybeifyipelgeu75bzjnrw5l5xpp4nmllh3owzk5o7qci7gtatstgdoam/folder/top.txt', + output: '/folder/top.txt', + }, ]; // eslint-disable-next-line mocha/no-setup-in-describe