-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
highlevel.browser.ts
162 lines (152 loc) · 5.66 KB
/
highlevel.browser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { generateUuid } from "@azure/ms-rest-js";
import { Aborter } from "./Aborter";
import { BlockBlobURL } from "./BlockBlobURL";
import { BlobUploadCommonResponse, IUploadToBlockBlobOptions } from "./highlevel.common";
import { Batch } from "./utils/Batch";
import {
BLOCK_BLOB_MAX_BLOCKS,
BLOCK_BLOB_MAX_STAGE_BLOCK_BYTES,
BLOCK_BLOB_MAX_UPLOAD_BLOB_BYTES,
DEFAULT_BLOB_DOWNLOAD_BLOCK_BYTES
} from "./utils/constants";
import { generateBlockID } from "./utils/utils.common";
/**
* ONLY AVAILABLE IN BROWSERS.
*
* Uploads a browser Blob/File/ArrayBuffer/ArrayBufferView object to block blob.
*
* When buffer length <= 256MB, this method will use 1 upload call to finish the upload.
* Otherwise, this method will call stageBlock to upload blocks, and finally call commitBlockList
* to commit the block list.
*
* @export
* @param {Aborter} aborter Create a new Aborter instance with Aborter.none or Aborter.timeout(),
* goto documents of Aborter for more examples about request cancellation
* @param {Blob | ArrayBuffer | ArrayBufferView} browserData Blob, File, ArrayBuffer or ArrayBufferView
* @param {BlockBlobURL} blockBlobURL
* @param {IUploadToBlockBlobOptions} [options]
* @returns {Promise<BlobUploadCommonResponse>}
*/
export async function uploadBrowserDataToBlockBlob(
aborter: Aborter,
browserData: Blob | ArrayBuffer | ArrayBufferView,
blockBlobURL: BlockBlobURL,
options?: IUploadToBlockBlobOptions
): Promise<BlobUploadCommonResponse> {
const browserBlob = new Blob([browserData]);
return UploadSeekableBlobToBlockBlob(
aborter,
(offset: number, size: number): Blob => {
return browserBlob.slice(offset, offset + size);
},
browserBlob.size,
blockBlobURL,
options
);
}
/**
* ONLY AVAILABLE IN BROWSERS.
*
* Uploads a browser Blob object to block blob. Requires a blobFactory as the data source,
* which need to return a Blob object with the offset and size provided.
*
* When buffer length <= 256MB, this method will use 1 upload call to finish the upload.
* Otherwise, this method will call stageBlock to upload blocks, and finally call commitBlockList
* to commit the block list.
*
* @param {Aborter} aborter Create a new Aborter instance with Aborter.none or Aborter.timeout(),
* goto documents of Aborter for more examples about request cancellation
* @param {(offset: number, size: number) => Blob} blobFactory
* @param {number} size
* @param {BlockBlobURL} blockBlobURL
* @param {IUploadToBlockBlobOptions} [options]
* @returns {Promise<BlobUploadCommonResponse>}
*/
async function UploadSeekableBlobToBlockBlob(
aborter: Aborter,
blobFactory: (offset: number, size: number) => Blob,
size: number,
blockBlobURL: BlockBlobURL,
options: IUploadToBlockBlobOptions = {}
): Promise<BlobUploadCommonResponse> {
if (!options.blockSize) {
options.blockSize = 0;
}
if (options.blockSize < 0 || options.blockSize > BLOCK_BLOB_MAX_STAGE_BLOCK_BYTES) {
throw new RangeError(
`blockSize option must be >= 0 and <= ${BLOCK_BLOB_MAX_STAGE_BLOCK_BYTES}`
);
}
if (options.maxSingleShotSize !== 0 && !options.maxSingleShotSize) {
options.maxSingleShotSize = BLOCK_BLOB_MAX_UPLOAD_BLOB_BYTES;
}
if (
options.maxSingleShotSize < 0 ||
options.maxSingleShotSize > BLOCK_BLOB_MAX_UPLOAD_BLOB_BYTES
) {
throw new RangeError(
`maxSingleShotSize option must be >= 0 and <= ${BLOCK_BLOB_MAX_UPLOAD_BLOB_BYTES}`
);
}
if (options.blockSize === 0) {
if (size > BLOCK_BLOB_MAX_STAGE_BLOCK_BYTES * BLOCK_BLOB_MAX_BLOCKS) {
throw new RangeError(`${size} is too larger to upload to a block blob.`);
}
if (size > options.maxSingleShotSize) {
options.blockSize = Math.ceil(size / BLOCK_BLOB_MAX_BLOCKS);
if (options.blockSize < DEFAULT_BLOB_DOWNLOAD_BLOCK_BYTES) {
options.blockSize = DEFAULT_BLOB_DOWNLOAD_BLOCK_BYTES;
}
}
}
if (!options.blobHTTPHeaders) {
options.blobHTTPHeaders = {};
}
if (!options.blobAccessConditions) {
options.blobAccessConditions = {};
}
if (size <= options.maxSingleShotSize) {
return blockBlobURL.upload(aborter, blobFactory(0, size), size, options);
}
const numBlocks: number = Math.floor((size - 1) / options.blockSize) + 1;
if (numBlocks > BLOCK_BLOB_MAX_BLOCKS) {
throw new RangeError(
`The buffer's size is too big or the BlockSize is too small;` +
`the number of blocks must be <= ${BLOCK_BLOB_MAX_BLOCKS}`
);
}
const blockList: string[] = [];
const blockIDPrefix = generateUuid();
let transferProgress: number = 0;
const batch = new Batch(options.parallelism);
for (let i = 0; i < numBlocks; i++) {
batch.addOperation(
async (): Promise<any> => {
const blockID = generateBlockID(blockIDPrefix, i);
const start = options.blockSize! * i;
const end = i === numBlocks - 1 ? size : start + options.blockSize!;
const contentLength = end - start;
blockList.push(blockID);
await blockBlobURL.stageBlock(
aborter,
blockID,
blobFactory(start, contentLength),
contentLength,
{
leaseAccessConditions: options.blobAccessConditions!.leaseAccessConditions
}
);
// Update progress after block is successfully uploaded to server, in case of block trying
// TODO: Hook with convenience layer progress event in finer level
transferProgress += contentLength;
if (options.progress) {
options.progress!({
loadedBytes: transferProgress
});
}
}
);
}
await batch.do();
return blockBlobURL.commitBlockList(aborter, blockList, options);
}