Skip to content

Commit

Permalink
fix for client/server side js blob issue
Browse files Browse the repository at this point in the history
  • Loading branch information
boyko-ant committed Feb 18, 2020
1 parent 0ed0f6b commit ab8d379
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/service/BlobService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import GeneratorService from "./helper/GeneratorService";

import {BlobServiceClient, ContainerClient, StorageSharedKeyCredential} from "@azure/storage-blob";
import {Readable} from "stream";
import UtilService from "./helper/UtilService";

export default class BlobService {
public static async createContainerIfNotExistsAsync(): Promise<void> {
Expand Down Expand Up @@ -35,14 +36,22 @@ export default class BlobService {
const clientContainer = this.getClientContainer();
const clientBlob = clientContainer.getBlockBlobClient(uniqueId);

if (content instanceof ArrayBuffer) {
await clientBlob.upload(content, content.byteLength);
} else if (content instanceof Readable) {
await clientBlob.uploadStream(content);
} else if (content instanceof Blob) {
await clientBlob.upload(content, content.size);
if (UtilService.IsNode()) {
if (content instanceof ArrayBuffer) {
await clientBlob.upload(content, content.byteLength);
} else if (content instanceof Readable) {
await clientBlob.uploadStream(content);
} else {
throw new Error("Content should be either Readable or ArrayBuffer");
}
} else {
throw new Error("Content should be either Readable or Blob");
if (content instanceof ArrayBuffer) {
await clientBlob.upload(content, content.byteLength);
} else if (content instanceof Blob) {
await clientBlob.upload(content, content.size);
} else {
throw new Error("Content should be either ArrayBuffer or Blob");
}
}

return clientBlob.url;
Expand Down
5 changes: 5 additions & 0 deletions src/service/helper/UtilService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default class UtilService {
public static IsNode(): boolean {
return typeof window === 'undefined';
}
}

0 comments on commit ab8d379

Please sign in to comment.