Skip to content

Commit

Permalink
fix(sdk/upload-service): fix Blob for browser
Browse files Browse the repository at this point in the history
Signed-off-by: kacper-koza-arianelabs <[email protected]>
  • Loading branch information
kacper-koza-arianelabs committed May 27, 2024
1 parent 731aacc commit 00155ab
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 44 deletions.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@
"./src/local-validation/index.js": "./src/local-validation/browser.js",
"./lib/esm/local-validation/index.js": "./lib/esm/local-validation/browser.js",
"./lib/cjs/local-validation/index.js": "./lib/cjs/local-validation/browser.js",
"./src/validator/validators/schema.js": "./src/validator/validators/schema.browser.js",
"./lib/esm/validator/validators/schema.js": "./lib/esm/validator/validators/schema.browser.js",
"./lib/cjs/validator/validators/schema.js": "./lib/cjs/validator/validators/schema.browser.js",
"./src/services/upload-service.js": "./src/services/upload-service.browser.js",
"./lib/esm/services/upload-service.js": "./lib/esm/services/upload-service.browser.js",
"./lib/cjs/services/upload-service.js": "./lib/cjs/services/upload-service.browser.js",
Expand Down
1 change: 0 additions & 1 deletion src/services/file-storages/aws/aws-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* limitations under the License.
*
*/
import type { Blob } from 'buffer';
import { dictionary } from '../../../utils/constants/dictionary';
import { filetypename } from 'magic-bytes.js';
import { errorToMessage } from '../../../helpers/error-to-message';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* limitations under the License.
*
*/
import type { Blob } from 'buffer';
import { dictionary } from '../../../utils/constants/dictionary';
import { FileStorageUploadUrl, FileStorageURL } from '../../upload-service';
import { FileStorage } from '../../../types/file-storage-service';
Expand Down
2 changes: 0 additions & 2 deletions src/services/file-storages/pinata/pinata-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* limitations under the License.
*
*/
import type { Blob } from 'buffer';
import { dictionary } from '../../../utils/constants/dictionary';
import { v4 } from 'uuid';
import axios, { type AxiosInstance } from 'axios';
Expand Down Expand Up @@ -48,7 +47,6 @@ export class PinataService implements FileStorage {

public async uploadFile(file: Blob): Promise<string> {
const formData = new FormData();
// @ts-expect-error Argument of type 'Blob' is assignable to parameter of type 'import("buffer").Blob
formData.append('file', file);

formData.append(
Expand Down
32 changes: 23 additions & 9 deletions src/services/upload-service.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import filter from 'lodash/filter';
import isNull from 'lodash/isNull';
import map from 'lodash/map';
import { BufferFile } from '../types/buffer-file';
import { dictionary } from '../utils/constants/dictionary';
import { errorToMessage } from '../helpers/error-to-message';
import { NFTMetadata } from '../types/nft-metadata';
Expand All @@ -29,10 +28,12 @@ import { FileStorage } from '../types/file-storage-service';
export type FileStorageURL = `https://${string}/`;
export type FileStorageUploadUrl = string;
type UploadServiceReturn = {
content: Blob | BufferFile;
content: Blob;
url: string;
};

const nonEmptyFiles = (file: Blob) => file.size > 0;

export class UploadService {
private service: FileStorage;

Expand All @@ -48,12 +49,26 @@ export class UploadService {
throw new Error('Not supported in browser.');
}

/**
* Function below is not browser supported
* @browserUnsupported
*/
public async uploadBlobFiles(_: (Blob | BufferFile)[]): Promise<UploadServiceReturn[]> {
throw new Error('Not supported in browser.');
public async uploadBlobFiles(files: Blob[]): Promise<UploadServiceReturn[]> {
if (files.length < 0) {
throw new Error(dictionary.errors.uploadService.noFiles);
}

try {
return await Promise.all(
map(filter(files, nonEmptyFiles), async (file) => {
const url = await this.service.uploadFile(file);
return {
content: file,
url,
};
})
);
} catch (e) {
const errorMessage = errorToMessage(e);

throw new Error(errorMessage);
}
}

public async handleBlobUpload(metadata: Partial<NFTMetadata> | NFTMetadata): Promise<UploadServiceReturn | null> {
Expand All @@ -63,7 +78,6 @@ export class UploadService {

try {
const file = new Blob([JSON.stringify(metadata)], { type: 'application/json' });
// @ts-expect-error Argument of type 'Blob' is assignable to parameter of type 'import("buffer").Blob
const url = await this.service.uploadFile(file);

return {
Expand Down
28 changes: 4 additions & 24 deletions src/services/upload-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ type UploadServiceReturn = {
url: string;
};

const nonEmptyFiles = (file: Blob | BufferFile): boolean => {
return file instanceof Blob ? file.size > 0 : !file.isFileEmpty;
};
const nonEmptyFiles = (file: Blob) => file.size > 0;

export class UploadService {
private service: FileStorage;
Expand All @@ -47,7 +45,7 @@ export class UploadService {

/**
* Function below is not browser supported
* @browserUnsupported
* @browserUnsupported
*/
public async uploadFilesFromPath(paths: string[]): Promise<UploadServiceReturn[]> {
const result = await Promise.all(
Expand All @@ -72,7 +70,6 @@ export class UploadService {
async (file) => {
const fileContent = fs.readFileSync(file);
const blob = new Blob([fileContent]);
// @ts-expect-error Argument of type 'Blob' is assignable to parameter of type 'import("buffer").Blob
const url = await this.service.uploadFile(blob);

return {
Expand All @@ -91,31 +88,15 @@ export class UploadService {
return result.flat();
}

/**
* Function below is not browser supported
* @browserUnsupported
*/
public async uploadBlobFiles(files: (Blob | BufferFile)[]): Promise<UploadServiceReturn[]> {
public async uploadBlobFiles(files: Blob[]): Promise<UploadServiceReturn[]> {
if (files.length < 0) {
throw new Error(dictionary.errors.uploadService.noFiles);
}

try {
return await Promise.all(
map(filter(files, nonEmptyFiles), async (file) => {
let fileToUpload: Blob | null = null;

if (file instanceof Blob) {
fileToUpload = file;
} else {
const fileContent = fs.readFileSync(file.filePath);

fileToUpload = new Blob([fileContent]);
}

// @ts-expect-error Argument of type 'Blob' is assignable to parameter of type 'import("buffer").Blob
const url = await this.service.uploadFile(fileToUpload);

const url = await this.service.uploadFile(file);
return {
content: file,
url,
Expand All @@ -136,7 +117,6 @@ export class UploadService {

try {
const file = new Blob([JSON.stringify(metadata)], { type: 'application/json' });
// @ts-expect-error Argument of type 'Blob' is assignable to parameter of type 'import("buffer").Blob
const url = await this.service.uploadFile(file);

return {
Expand Down
1 change: 0 additions & 1 deletion src/test/unit/storage-providers/aws-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { AWSService } from '../../../services/file-storages/aws/aws-service';
import { S3Client } from '@aws-sdk/client-s3';
import { Upload } from '@aws-sdk/lib-storage';
import { AWS_RES_DONE } from '../../__mocks__/aws';
import { Blob } from 'buffer';

jest.mock('@aws-sdk/client-s3');
jest.mock('@aws-sdk/lib-storage');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* limitations under the License.
*
*/
import { Blob } from 'buffer';
import axios from 'axios';
import { NftStorageService } from '../../../services/file-storages/nft-storage/nft-storage-service';
import { dictionary } from '../../../utils/constants/dictionary';
Expand Down
1 change: 0 additions & 1 deletion src/test/unit/storage-providers/pinata-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*
*/
import { PinataService } from '../../../services/file-storages/pinata/pinata-service';
import { Blob } from 'buffer';
import axios from 'axios';
import { dictionary } from '../../../utils/constants/dictionary';

Expand Down
1 change: 0 additions & 1 deletion src/types/file-storage-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* limitations under the License.
*
*/
import type { Blob } from 'buffer';

export interface FileStorage {
uploadFile(file: Blob): Promise<string>;
Expand Down

0 comments on commit 00155ab

Please sign in to comment.