Skip to content

Commit

Permalink
Feature: Add upload callback and Add Blob to AddFileItem.data types (#29
Browse files Browse the repository at this point in the history
)
  • Loading branch information
perfectmak authored Jan 29, 2021
1 parent 5de484a commit cf1fd46
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
3 changes: 2 additions & 1 deletion etc/sdk.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import { UserAuth } from '@textile/hub';
// @public (undocumented)
export interface AddItemFile {
// (undocumented)
data: ReadableStream<Uint8Array> | ArrayBuffer | string;
data: ReadableStream<Uint8Array> | ArrayBuffer | string | Blob;
mimeType: string;
path: string;
progress?: (bytesRead?: number) => void;
}

// @public (undocumented)
Expand Down
8 changes: 7 additions & 1 deletion packages/storage/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,13 @@ export interface AddItemFile {
*
*/
mimeType: string;
data: ReadableStream<Uint8Array> | ArrayBuffer | string;
data: ReadableStream<Uint8Array> | 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 {
Expand Down
4 changes: 2 additions & 2 deletions packages/storage/src/userStorage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PushPathResult>(),
});

Expand Down Expand Up @@ -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[],
Expand Down
2 changes: 1 addition & 1 deletion packages/storage/src/userStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,14 +483,14 @@ export class UserStorage {
};

try {
await client.pushPath(rootKey, path, file.data);
const metadata = await metadataStore.upsertFileMetadata({
uuid: v4(),
mimeType: file.mimeType,
bucketSlug: bucket.slug,
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(
Expand Down
4 changes: 4 additions & 0 deletions packages/storage/src/utils/pathUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cf1fd46

Please sign in to comment.