@uploadcare/upload-client 6.0.1-alpha.7 → 6.0.1-alpha.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.browser.js +179 -143
- package/dist/index.d.ts +62 -55
- package/dist/index.node.js +180 -140
- package/dist/index.react-native.js +211 -165
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -70,6 +70,42 @@ export declare type ContentInfo = {
|
|
|
70
70
|
image?: ImageInfo;
|
|
71
71
|
video?: VideoInfo;
|
|
72
72
|
};
|
|
73
|
+
export interface DefaultSettings {
|
|
74
|
+
baseCDN: string;
|
|
75
|
+
baseURL: string;
|
|
76
|
+
maxContentLength: number;
|
|
77
|
+
retryThrottledRequestMaxTimes: number;
|
|
78
|
+
retryNetworkErrorMaxTimes: number;
|
|
79
|
+
multipartMinFileSize: number;
|
|
80
|
+
multipartChunkSize: number;
|
|
81
|
+
multipartMinLastPartSize: number;
|
|
82
|
+
maxConcurrentRequests: number;
|
|
83
|
+
pollingTimeoutMilliseconds: number;
|
|
84
|
+
pusherKey: string;
|
|
85
|
+
}
|
|
86
|
+
export interface Settings extends Partial<DefaultSettings> {
|
|
87
|
+
publicKey: string;
|
|
88
|
+
fileName?: string;
|
|
89
|
+
contentType?: string;
|
|
90
|
+
store?: boolean;
|
|
91
|
+
secureSignature?: string;
|
|
92
|
+
secureExpire?: string;
|
|
93
|
+
integration?: string;
|
|
94
|
+
userAgent?: CustomUserAgent;
|
|
95
|
+
checkForUrlDuplicates?: boolean;
|
|
96
|
+
saveUrlForRecurrentUploads?: boolean;
|
|
97
|
+
source?: string;
|
|
98
|
+
jsonpCallback?: string;
|
|
99
|
+
}
|
|
100
|
+
export declare type BrowserFile = Blob | File;
|
|
101
|
+
export declare type NodeFile = Buffer;
|
|
102
|
+
export declare type ReactNativeAsset = {
|
|
103
|
+
type: string;
|
|
104
|
+
uri: string;
|
|
105
|
+
name?: string;
|
|
106
|
+
};
|
|
107
|
+
export declare type ReactNativeFile = ReactNativeAsset | Blob;
|
|
108
|
+
export declare type AnyFile = BrowserFile | NodeFile | ReactNativeFile;
|
|
73
109
|
export declare type FileInfo = {
|
|
74
110
|
size: number;
|
|
75
111
|
done: number;
|
|
@@ -112,18 +148,6 @@ export declare type ProgressCallback<T = ComputableProgressInfo | UnknownProgres
|
|
|
112
148
|
export declare type Metadata = {
|
|
113
149
|
[key: string]: string;
|
|
114
150
|
};
|
|
115
|
-
export declare type Headers = {
|
|
116
|
-
[key: string]: string | string[] | undefined;
|
|
117
|
-
};
|
|
118
|
-
export declare type ErrorRequestInfo = {
|
|
119
|
-
method?: string;
|
|
120
|
-
url: string;
|
|
121
|
-
query?: string;
|
|
122
|
-
data?: NodeFormData | FormData | BrowserFile | NodeFile;
|
|
123
|
-
headers?: Headers;
|
|
124
|
-
};
|
|
125
|
-
export declare type BrowserFile = Blob | File;
|
|
126
|
-
export declare type NodeFile = Buffer;
|
|
127
151
|
export declare type BaseResponse = {
|
|
128
152
|
file: Uuid;
|
|
129
153
|
};
|
|
@@ -148,7 +172,7 @@ export declare type BaseOptions = {
|
|
|
148
172
|
* Performs file uploading request to Uploadcare Upload API.
|
|
149
173
|
* Can be canceled and has progress.
|
|
150
174
|
*/
|
|
151
|
-
export function base(file:
|
|
175
|
+
export function base(file: AnyFile, { publicKey, fileName, contentType, baseURL, secureSignature, secureExpire, store, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, retryNetworkErrorMaxTimes, metadata }: BaseOptions): Promise<BaseResponse>;
|
|
152
176
|
declare enum TypeEnum {
|
|
153
177
|
Token = "token",
|
|
154
178
|
FileInfo = "file_info"
|
|
@@ -309,7 +333,7 @@ export declare type MultipartUploadResponse = {
|
|
|
309
333
|
/**
|
|
310
334
|
* Complete multipart uploading.
|
|
311
335
|
*/
|
|
312
|
-
export function multipartUpload(part:
|
|
336
|
+
export function multipartUpload(part: AnyFile, url: MultipartPart, { signal, onProgress, retryThrottledRequestMaxTimes, retryNetworkErrorMaxTimes }: MultipartUploadOptions): Promise<MultipartUploadResponse>;
|
|
313
337
|
export declare type MultipartCompleteOptions = {
|
|
314
338
|
publicKey: string;
|
|
315
339
|
baseURL?: string;
|
|
@@ -362,13 +386,7 @@ export declare type DirectOptions = {
|
|
|
362
386
|
baseCDN?: string;
|
|
363
387
|
metadata?: Metadata;
|
|
364
388
|
};
|
|
365
|
-
export declare const uploadDirect: (file:
|
|
366
|
-
export declare type UploadFromUrlOptions = {
|
|
367
|
-
baseCDN?: string;
|
|
368
|
-
onProgress?: ProgressCallback;
|
|
369
|
-
pusherKey?: string;
|
|
370
|
-
} & FromUrlOptions;
|
|
371
|
-
export declare const uploadFromUrl: (sourceUrl: string, { publicKey, fileName, baseURL, baseCDN, checkForUrlDuplicates, saveUrlForRecurrentUploads, secureSignature, secureExpire, store, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, pusherKey, metadata }: UploadFromUrlOptions) => Promise<UploadcareFile>;
|
|
389
|
+
export declare const uploadDirect: (file: AnyFile, { publicKey, fileName, baseURL, secureSignature, secureExpire, store, contentType, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, retryNetworkErrorMaxTimes, baseCDN, metadata }: DirectOptions) => Promise<UploadcareFile>;
|
|
372
390
|
export declare type FromUploadedOptions = {
|
|
373
391
|
publicKey: string;
|
|
374
392
|
fileName?: string;
|
|
@@ -383,6 +401,12 @@ export declare type FromUploadedOptions = {
|
|
|
383
401
|
baseCDN?: string;
|
|
384
402
|
};
|
|
385
403
|
export declare const uploadFromUploaded: (uuid: Uuid, { publicKey, fileName, baseURL, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, retryNetworkErrorMaxTimes, baseCDN }: FromUploadedOptions) => Promise<UploadcareFile>;
|
|
404
|
+
export declare type UploadFromUrlOptions = {
|
|
405
|
+
baseCDN?: string;
|
|
406
|
+
onProgress?: ProgressCallback;
|
|
407
|
+
pusherKey?: string;
|
|
408
|
+
} & FromUrlOptions;
|
|
409
|
+
export declare const uploadFromUrl: (sourceUrl: string, { publicKey, fileName, baseURL, baseCDN, checkForUrlDuplicates, saveUrlForRecurrentUploads, secureSignature, secureExpire, store, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, pusherKey, metadata }: UploadFromUrlOptions) => Promise<UploadcareFile>;
|
|
386
410
|
export declare type MultipartOptions = {
|
|
387
411
|
publicKey: string;
|
|
388
412
|
contentType?: string;
|
|
@@ -404,7 +428,7 @@ export declare type MultipartOptions = {
|
|
|
404
428
|
baseCDN?: string;
|
|
405
429
|
metadata?: Metadata;
|
|
406
430
|
};
|
|
407
|
-
export declare const uploadMultipart: (file:
|
|
431
|
+
export declare const uploadMultipart: (file: AnyFile, { publicKey, fileName, fileSize, baseURL, secureSignature, secureExpire, store, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, retryNetworkErrorMaxTimes, contentType, multipartChunkSize, maxConcurrentRequests, baseCDN, metadata }: MultipartOptions) => Promise<UploadcareFile>;
|
|
408
432
|
export declare type FileFromOptions = {
|
|
409
433
|
publicKey: string;
|
|
410
434
|
fileName?: string;
|
|
@@ -432,7 +456,7 @@ export declare type FileFromOptions = {
|
|
|
432
456
|
/**
|
|
433
457
|
* Uploads file from provided data.
|
|
434
458
|
*/
|
|
435
|
-
export declare function uploadFile(data:
|
|
459
|
+
export declare function uploadFile(data: AnyFile | Url | Uuid, { publicKey, fileName, baseURL, secureSignature, secureExpire, store, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, retryNetworkErrorMaxTimes, contentType, multipartMinFileSize, multipartChunkSize, maxConcurrentRequests, baseCDN, checkForUrlDuplicates, saveUrlForRecurrentUploads, pusherKey, metadata }: FileFromOptions): Promise<UploadcareFile>;
|
|
436
460
|
export declare class UploadcareGroup {
|
|
437
461
|
readonly uuid: GroupId;
|
|
438
462
|
readonly filesCount: string;
|
|
@@ -448,51 +472,34 @@ export declare class UploadcareGroup {
|
|
|
448
472
|
export declare type GroupFromOptions = {
|
|
449
473
|
jsonpCallback?: string;
|
|
450
474
|
};
|
|
451
|
-
export function uploadFileGroup(data:
|
|
452
|
-
export interface DefaultSettings {
|
|
453
|
-
baseCDN: string;
|
|
454
|
-
baseURL: string;
|
|
455
|
-
maxContentLength: number;
|
|
456
|
-
retryThrottledRequestMaxTimes: number;
|
|
457
|
-
retryNetworkErrorMaxTimes: number;
|
|
458
|
-
multipartMinFileSize: number;
|
|
459
|
-
multipartChunkSize: number;
|
|
460
|
-
multipartMinLastPartSize: number;
|
|
461
|
-
maxConcurrentRequests: number;
|
|
462
|
-
pollingTimeoutMilliseconds: number;
|
|
463
|
-
pusherKey: string;
|
|
464
|
-
}
|
|
465
|
-
export interface Settings extends Partial<DefaultSettings> {
|
|
466
|
-
publicKey: string;
|
|
467
|
-
fileName?: string;
|
|
468
|
-
contentType?: string;
|
|
469
|
-
store?: boolean;
|
|
470
|
-
secureSignature?: string;
|
|
471
|
-
secureExpire?: string;
|
|
472
|
-
integration?: string;
|
|
473
|
-
userAgent?: CustomUserAgent;
|
|
474
|
-
checkForUrlDuplicates?: boolean;
|
|
475
|
-
saveUrlForRecurrentUploads?: boolean;
|
|
476
|
-
source?: string;
|
|
477
|
-
jsonpCallback?: string;
|
|
478
|
-
}
|
|
475
|
+
export function uploadFileGroup(data: AnyFile[] | Url[] | Uuid[], { publicKey, fileName, baseURL, secureSignature, secureExpire, store, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, retryNetworkErrorMaxTimes, contentType, multipartChunkSize, baseCDN, jsonpCallback }: FileFromOptions & GroupFromOptions): Promise<UploadcareGroup>;
|
|
479
476
|
export declare class UploadClient {
|
|
480
477
|
private settings;
|
|
481
478
|
constructor(settings: Settings);
|
|
482
479
|
updateSettings(newSettings: Settings): void;
|
|
483
480
|
getSettings(): Settings;
|
|
484
|
-
base(file:
|
|
481
|
+
base(file: AnyFile, options?: Partial<BaseOptions>): Promise<BaseResponse>;
|
|
485
482
|
info(uuid: Uuid, options?: Partial<InfoOptions>): Promise<FileInfo>;
|
|
486
483
|
fromUrl(sourceUrl: Url, options?: Partial<FromUrlOptions>): Promise<FromUrlResponse>;
|
|
487
484
|
fromUrlStatus(token: Token, options?: Partial<FromUrlStatusOptions>): Promise<FromUrlStatusResponse>;
|
|
488
485
|
group(uuids: Uuid[], options?: Partial<GroupOptions>): Promise<GroupInfo>;
|
|
489
486
|
groupInfo(id: GroupId, options?: Partial<GroupInfoOptions>): Promise<GroupInfo>;
|
|
490
487
|
multipartStart(size: number, options?: Partial<MultipartStartOptions>): Promise<MultipartStartResponse>;
|
|
491
|
-
multipartUpload(part:
|
|
488
|
+
multipartUpload(part: BrowserFile | NodeFile, url: MultipartPart, options?: Partial<MultipartUploadOptions>): Promise<MultipartUploadResponse>;
|
|
492
489
|
multipartComplete(uuid: Uuid, options?: Partial<MultipartCompleteOptions>): Promise<FileInfo>;
|
|
493
|
-
uploadFile(data:
|
|
494
|
-
uploadFileGroup(data: (
|
|
490
|
+
uploadFile(data: AnyFile | Url | Uuid, options?: Partial<FileFromOptions>): Promise<UploadcareFile>;
|
|
491
|
+
uploadFileGroup(data: (AnyFile)[] | Url[] | Uuid[], options?: Partial<FileFromOptions & GroupFromOptions>): Promise<UploadcareGroup>;
|
|
495
492
|
}
|
|
493
|
+
export declare type Headers = {
|
|
494
|
+
[key: string]: string | string[] | undefined;
|
|
495
|
+
};
|
|
496
|
+
export declare type ErrorRequestInfo = {
|
|
497
|
+
method?: string;
|
|
498
|
+
url: string;
|
|
499
|
+
query?: string;
|
|
500
|
+
data?: NodeFormData | FormData | AnyFile;
|
|
501
|
+
headers?: Headers;
|
|
502
|
+
};
|
|
496
503
|
export declare type ErrorResponseInfo = {
|
|
497
504
|
error?: {
|
|
498
505
|
statusCode: number;
|
package/dist/index.node.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import http from 'http';
|
|
2
2
|
import https from 'https';
|
|
3
|
-
import { parse } from 'url';
|
|
4
3
|
import { Transform, Readable } from 'stream';
|
|
4
|
+
import { parse } from 'url';
|
|
5
5
|
import NodeFormData from 'form-data';
|
|
6
6
|
import WebSocket from 'ws';
|
|
7
7
|
|
|
@@ -164,6 +164,26 @@ const poll = ({ check, interval = DEFAULT_INTERVAL, timeout, signal }) => new Pr
|
|
|
164
164
|
tickTimeoutId = setTimeout(tick, 0);
|
|
165
165
|
});
|
|
166
166
|
|
|
167
|
+
/*
|
|
168
|
+
Settings for future support:
|
|
169
|
+
parallelDirectUploads: 10,
|
|
170
|
+
*/
|
|
171
|
+
const defaultSettings = {
|
|
172
|
+
baseCDN: 'https://ucarecdn.com',
|
|
173
|
+
baseURL: 'https://upload.uploadcare.com',
|
|
174
|
+
maxContentLength: 50 * 1024 * 1024,
|
|
175
|
+
retryThrottledRequestMaxTimes: 1,
|
|
176
|
+
retryNetworkErrorMaxTimes: 3,
|
|
177
|
+
multipartMinFileSize: 25 * 1024 * 1024,
|
|
178
|
+
multipartChunkSize: 5 * 1024 * 1024,
|
|
179
|
+
multipartMinLastPartSize: 1024 * 1024,
|
|
180
|
+
maxConcurrentRequests: 4,
|
|
181
|
+
pollingTimeoutMilliseconds: 10000,
|
|
182
|
+
pusherKey: '79ae88bd931ea68464d9'
|
|
183
|
+
};
|
|
184
|
+
const defaultContentType = 'application/octet-stream';
|
|
185
|
+
const defaultFilename = 'original';
|
|
186
|
+
|
|
167
187
|
// ProgressEmitter is a simple PassThrough-style transform stream which keeps
|
|
168
188
|
// track of the number of bytes which have been piped through it and will
|
|
169
189
|
// invoke the `onprogress` function whenever new number are available.
|
|
@@ -285,45 +305,24 @@ const getFileOptions = ({ name, contentType }) => [
|
|
|
285
305
|
const transformFile = identity;
|
|
286
306
|
var getFormData = () => new NodeFormData();
|
|
287
307
|
|
|
288
|
-
const
|
|
289
|
-
return
|
|
308
|
+
const isBlob = (data) => {
|
|
309
|
+
return typeof Blob !== 'undefined' && data instanceof Blob;
|
|
290
310
|
};
|
|
291
|
-
const
|
|
292
|
-
return
|
|
293
|
-
!!asset &&
|
|
294
|
-
'uri' in asset &&
|
|
295
|
-
typeof asset.uri === 'string' &&
|
|
296
|
-
isReactNativeUri(asset.uri));
|
|
311
|
+
const isFile = (data) => {
|
|
312
|
+
return typeof File !== 'undefined' && data instanceof File;
|
|
297
313
|
};
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
* FileData type guard.
|
|
301
|
-
*/
|
|
302
|
-
const isFileData = (data) => {
|
|
303
|
-
return (data !== undefined &&
|
|
304
|
-
((typeof Blob !== 'undefined' && data instanceof Blob) ||
|
|
305
|
-
(typeof File !== 'undefined' && data instanceof File) ||
|
|
306
|
-
(typeof Buffer !== 'undefined' && data instanceof Buffer) ||
|
|
307
|
-
(typeof data === 'string' && isReactNativeUri(data)) ||
|
|
308
|
-
(typeof data === 'object' && isReactNativeAsset(data))));
|
|
314
|
+
const isBuffer = (data) => {
|
|
315
|
+
return typeof Buffer !== 'undefined' && data instanceof Buffer;
|
|
309
316
|
};
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
return !isFileData(data) && regExp.test(data);
|
|
317
|
+
const isReactNativeAsset = (data) => {
|
|
318
|
+
return (!!data &&
|
|
319
|
+
typeof data === 'object' &&
|
|
320
|
+
!Array.isArray(data) &&
|
|
321
|
+
'uri' in data &&
|
|
322
|
+
typeof data.uri === 'string');
|
|
317
323
|
};
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
*
|
|
321
|
-
* @param {NodeFile | BrowserFile | Url | Uuid} data
|
|
322
|
-
*/
|
|
323
|
-
const isUrl = (data) => {
|
|
324
|
-
const URL_REGEX = '^(?:\\w+:)?\\/\\/([^\\s\\.]+\\.\\S{2}|localhost[\\:?\\d]*)\\S*$';
|
|
325
|
-
const regExp = new RegExp(URL_REGEX);
|
|
326
|
-
return !isFileData(data) && regExp.test(data);
|
|
324
|
+
const isFileData = (data) => {
|
|
325
|
+
return (isBlob(data) || isFile(data) || isBuffer(data) || isReactNativeAsset(data));
|
|
327
326
|
};
|
|
328
327
|
|
|
329
328
|
const isSimpleValue = (value) => {
|
|
@@ -364,11 +363,9 @@ function getFormDataParams(options) {
|
|
|
364
363
|
return params;
|
|
365
364
|
}
|
|
366
365
|
function buildFormData(options) {
|
|
367
|
-
console.log('buildFormData', options);
|
|
368
366
|
const formData = getFormData();
|
|
369
367
|
const paramsList = getFormDataParams(options);
|
|
370
368
|
for (const params of paramsList) {
|
|
371
|
-
console.log('params', params);
|
|
372
369
|
const [key, value, ...rest] = params;
|
|
373
370
|
// node form-data has another signature for append
|
|
374
371
|
formData.append(key, value, ...rest);
|
|
@@ -376,6 +373,19 @@ function buildFormData(options) {
|
|
|
376
373
|
return formData;
|
|
377
374
|
}
|
|
378
375
|
|
|
376
|
+
class UploadClientError extends Error {
|
|
377
|
+
constructor(message, code, request, response, headers) {
|
|
378
|
+
super();
|
|
379
|
+
this.name = 'UploadClientError';
|
|
380
|
+
this.message = message;
|
|
381
|
+
this.code = code;
|
|
382
|
+
this.request = request;
|
|
383
|
+
this.response = response;
|
|
384
|
+
this.headers = headers;
|
|
385
|
+
Object.setPrototypeOf(this, UploadClientError.prototype);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
379
389
|
const buildSearchParams = (query) => {
|
|
380
390
|
const searchParams = new URLSearchParams();
|
|
381
391
|
for (const [key, value] of Object.entries(query)) {
|
|
@@ -407,26 +417,6 @@ const getUrl = (base, path, query) => {
|
|
|
407
417
|
return url.toString();
|
|
408
418
|
};
|
|
409
419
|
|
|
410
|
-
/*
|
|
411
|
-
Settings for future support:
|
|
412
|
-
parallelDirectUploads: 10,
|
|
413
|
-
*/
|
|
414
|
-
const defaultSettings = {
|
|
415
|
-
baseCDN: 'https://ucarecdn.com',
|
|
416
|
-
baseURL: 'https://upload.uploadcare.com',
|
|
417
|
-
maxContentLength: 50 * 1024 * 1024,
|
|
418
|
-
retryThrottledRequestMaxTimes: 1,
|
|
419
|
-
retryNetworkErrorMaxTimes: 3,
|
|
420
|
-
multipartMinFileSize: 25 * 1024 * 1024,
|
|
421
|
-
multipartChunkSize: 5 * 1024 * 1024,
|
|
422
|
-
multipartMinLastPartSize: 1024 * 1024,
|
|
423
|
-
maxConcurrentRequests: 4,
|
|
424
|
-
pollingTimeoutMilliseconds: 10000,
|
|
425
|
-
pusherKey: '79ae88bd931ea68464d9'
|
|
426
|
-
};
|
|
427
|
-
const defaultContentType = 'application/octet-stream';
|
|
428
|
-
const defaultFilename = 'original';
|
|
429
|
-
|
|
430
420
|
var version = '6.0.0';
|
|
431
421
|
|
|
432
422
|
const LIBRARY_NAME = 'UploadcareUploadClient';
|
|
@@ -439,19 +429,6 @@ function getUserAgent(options) {
|
|
|
439
429
|
});
|
|
440
430
|
}
|
|
441
431
|
|
|
442
|
-
class UploadClientError extends Error {
|
|
443
|
-
constructor(message, code, request, response, headers) {
|
|
444
|
-
super();
|
|
445
|
-
this.name = 'UploadClientError';
|
|
446
|
-
this.message = message;
|
|
447
|
-
this.code = code;
|
|
448
|
-
this.request = request;
|
|
449
|
-
this.response = response;
|
|
450
|
-
this.headers = headers;
|
|
451
|
-
Object.setPrototypeOf(this, UploadClientError.prototype);
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
|
|
455
432
|
const REQUEST_WAS_THROTTLED_CODE = 'RequestThrottledError';
|
|
456
433
|
const DEFAULT_RETRY_AFTER_TIMEOUT = 15000;
|
|
457
434
|
const DEFAULT_NETWORK_ERROR_TIMEOUT = 1000;
|
|
@@ -482,6 +459,31 @@ function retryIfFailed(fn, options) {
|
|
|
482
459
|
}));
|
|
483
460
|
}
|
|
484
461
|
|
|
462
|
+
const getContentType = (file) => {
|
|
463
|
+
if (isBlob(file) || isFile(file) || isReactNativeAsset(file)) {
|
|
464
|
+
return file.type || defaultContentType;
|
|
465
|
+
}
|
|
466
|
+
if (isBuffer(file)) {
|
|
467
|
+
return defaultFilename;
|
|
468
|
+
}
|
|
469
|
+
console.warn(`Unknown filename: ${file}. Using default filename: ${defaultFilename}`);
|
|
470
|
+
return defaultFilename;
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
const getFilename = (file) => {
|
|
474
|
+
if (isBlob(file) || isBuffer(file)) {
|
|
475
|
+
return defaultFilename;
|
|
476
|
+
}
|
|
477
|
+
if (isFile(file)) {
|
|
478
|
+
return file.name || defaultFilename;
|
|
479
|
+
}
|
|
480
|
+
if (isReactNativeAsset(file)) {
|
|
481
|
+
return file.name || defaultFilename;
|
|
482
|
+
}
|
|
483
|
+
console.warn(`Unknown file type: ${file}. Using default filename: ${defaultFilename}`);
|
|
484
|
+
return defaultFilename;
|
|
485
|
+
};
|
|
486
|
+
|
|
485
487
|
function getStoreValue(store) {
|
|
486
488
|
return typeof store === 'undefined' ? 'auto' : store ? '1' : '0';
|
|
487
489
|
}
|
|
@@ -497,13 +499,13 @@ function base(file, { publicKey, fileName, contentType, baseURL = defaultSetting
|
|
|
497
499
|
jsonerrors: 1
|
|
498
500
|
}),
|
|
499
501
|
headers: {
|
|
500
|
-
'X-UC-User-Agent': getUserAgent({ publicKey, integration, userAgent })
|
|
502
|
+
'X-UC-User-Agent': getUserAgent({ publicKey, integration, userAgent })
|
|
501
503
|
},
|
|
502
504
|
data: buildFormData({
|
|
503
505
|
file: {
|
|
504
506
|
data: file,
|
|
505
|
-
name: fileName
|
|
506
|
-
contentType
|
|
507
|
+
name: fileName || getFilename(file),
|
|
508
|
+
contentType: contentType || getContentType(file)
|
|
507
509
|
},
|
|
508
510
|
UPLOADCARE_PUB_KEY: publicKey,
|
|
509
511
|
UPLOADCARE_STORE: getStoreValue(store),
|
|
@@ -701,9 +703,9 @@ function multipartStart(size, { publicKey, contentType, fileName, multipartChunk
|
|
|
701
703
|
'X-UC-User-Agent': getUserAgent({ publicKey, integration, userAgent })
|
|
702
704
|
},
|
|
703
705
|
data: buildFormData({
|
|
704
|
-
filename: fileName
|
|
706
|
+
filename: fileName || defaultFilename,
|
|
705
707
|
size: size,
|
|
706
|
-
content_type: contentType
|
|
708
|
+
content_type: contentType || defaultContentType,
|
|
707
709
|
part_size: multipartChunkSize,
|
|
708
710
|
UPLOADCARE_STORE: getStoreValue(store),
|
|
709
711
|
UPLOADCARE_PUB_KEY: publicKey,
|
|
@@ -780,6 +782,28 @@ function multipartComplete(uuid, { publicKey, baseURL = defaultSettings.baseURL,
|
|
|
780
782
|
}), { retryThrottledRequestMaxTimes, retryNetworkErrorMaxTimes });
|
|
781
783
|
}
|
|
782
784
|
|
|
785
|
+
function isReadyPoll({ file, publicKey, baseURL, source, integration, userAgent, retryThrottledRequestMaxTimes, retryNetworkErrorMaxTimes, signal, onProgress }) {
|
|
786
|
+
return poll({
|
|
787
|
+
check: (signal) => info(file, {
|
|
788
|
+
publicKey,
|
|
789
|
+
baseURL,
|
|
790
|
+
signal,
|
|
791
|
+
source,
|
|
792
|
+
integration,
|
|
793
|
+
userAgent,
|
|
794
|
+
retryThrottledRequestMaxTimes,
|
|
795
|
+
retryNetworkErrorMaxTimes
|
|
796
|
+
}).then((response) => {
|
|
797
|
+
if (response.isReady) {
|
|
798
|
+
return response;
|
|
799
|
+
}
|
|
800
|
+
onProgress && onProgress({ isComputable: true, value: 1 });
|
|
801
|
+
return false;
|
|
802
|
+
}),
|
|
803
|
+
signal
|
|
804
|
+
});
|
|
805
|
+
}
|
|
806
|
+
|
|
783
807
|
class UploadcareFile {
|
|
784
808
|
constructor(fileInfo, { baseCDN, fileName }) {
|
|
785
809
|
this.name = null;
|
|
@@ -817,28 +841,6 @@ class UploadcareFile {
|
|
|
817
841
|
}
|
|
818
842
|
}
|
|
819
843
|
|
|
820
|
-
function isReadyPoll({ file, publicKey, baseURL, source, integration, userAgent, retryThrottledRequestMaxTimes, retryNetworkErrorMaxTimes, signal, onProgress }) {
|
|
821
|
-
return poll({
|
|
822
|
-
check: (signal) => info(file, {
|
|
823
|
-
publicKey,
|
|
824
|
-
baseURL,
|
|
825
|
-
signal,
|
|
826
|
-
source,
|
|
827
|
-
integration,
|
|
828
|
-
userAgent,
|
|
829
|
-
retryThrottledRequestMaxTimes,
|
|
830
|
-
retryNetworkErrorMaxTimes
|
|
831
|
-
}).then((response) => {
|
|
832
|
-
if (response.isReady) {
|
|
833
|
-
return response;
|
|
834
|
-
}
|
|
835
|
-
onProgress && onProgress({ isComputable: true, value: 1 });
|
|
836
|
-
return false;
|
|
837
|
-
}),
|
|
838
|
-
signal
|
|
839
|
-
});
|
|
840
|
-
}
|
|
841
|
-
|
|
842
844
|
const uploadDirect = (file, { publicKey, fileName, baseURL, secureSignature, secureExpire, store, contentType, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, retryNetworkErrorMaxTimes, baseCDN, metadata }) => {
|
|
843
845
|
return base(file, {
|
|
844
846
|
publicKey,
|
|
@@ -874,6 +876,29 @@ const uploadDirect = (file, { publicKey, fileName, baseURL, secureSignature, sec
|
|
|
874
876
|
.then((fileInfo) => new UploadcareFile(fileInfo, { baseCDN }));
|
|
875
877
|
};
|
|
876
878
|
|
|
879
|
+
const uploadFromUploaded = (uuid, { publicKey, fileName, baseURL, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, retryNetworkErrorMaxTimes, baseCDN }) => {
|
|
880
|
+
return info(uuid, {
|
|
881
|
+
publicKey,
|
|
882
|
+
baseURL,
|
|
883
|
+
signal,
|
|
884
|
+
source,
|
|
885
|
+
integration,
|
|
886
|
+
userAgent,
|
|
887
|
+
retryThrottledRequestMaxTimes,
|
|
888
|
+
retryNetworkErrorMaxTimes
|
|
889
|
+
})
|
|
890
|
+
.then((fileInfo) => new UploadcareFile(fileInfo, { baseCDN, fileName }))
|
|
891
|
+
.then((result) => {
|
|
892
|
+
// hack for node ¯\_(ツ)_/¯
|
|
893
|
+
if (onProgress)
|
|
894
|
+
onProgress({
|
|
895
|
+
isComputable: true,
|
|
896
|
+
value: 1
|
|
897
|
+
});
|
|
898
|
+
return result;
|
|
899
|
+
});
|
|
900
|
+
};
|
|
901
|
+
|
|
877
902
|
const race = (fns, { signal } = {}) => {
|
|
878
903
|
let lastError = null;
|
|
879
904
|
let winnerIndex = null;
|
|
@@ -1211,35 +1236,30 @@ const uploadFromUrl = (sourceUrl, { publicKey, fileName, baseURL, baseCDN, check
|
|
|
1211
1236
|
}))
|
|
1212
1237
|
.then((fileInfo) => new UploadcareFile(fileInfo, { baseCDN }));
|
|
1213
1238
|
|
|
1214
|
-
const
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
retryThrottledRequestMaxTimes,
|
|
1223
|
-
retryNetworkErrorMaxTimes
|
|
1224
|
-
})
|
|
1225
|
-
.then((fileInfo) => new UploadcareFile(fileInfo, { baseCDN, fileName }))
|
|
1226
|
-
.then((result) => {
|
|
1227
|
-
// hack for node ¯\_(ツ)_/¯
|
|
1228
|
-
if (onProgress)
|
|
1229
|
-
onProgress({
|
|
1230
|
-
isComputable: true,
|
|
1231
|
-
value: 1
|
|
1232
|
-
});
|
|
1233
|
-
return result;
|
|
1234
|
-
});
|
|
1239
|
+
const memo = new WeakMap();
|
|
1240
|
+
const getReactNativeBlob = async (asset) => {
|
|
1241
|
+
if (memo.has(asset)) {
|
|
1242
|
+
return memo.get(asset);
|
|
1243
|
+
}
|
|
1244
|
+
const blob = await fetch(asset.uri).then((res) => res.blob());
|
|
1245
|
+
memo.set(asset, blob);
|
|
1246
|
+
return blob;
|
|
1235
1247
|
};
|
|
1236
1248
|
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1249
|
+
const getFileSize = async (file) => {
|
|
1250
|
+
if (isBuffer(file)) {
|
|
1251
|
+
return file.length;
|
|
1252
|
+
}
|
|
1253
|
+
if (isFile(file) || isBlob(file)) {
|
|
1254
|
+
return file.size;
|
|
1255
|
+
}
|
|
1256
|
+
if (isReactNativeAsset(file)) {
|
|
1257
|
+
const blob = await getReactNativeBlob(file);
|
|
1258
|
+
return blob.size;
|
|
1259
|
+
}
|
|
1260
|
+
throw new Error(`Failed to get file size for file: ${file}`);
|
|
1242
1261
|
};
|
|
1262
|
+
|
|
1243
1263
|
/**
|
|
1244
1264
|
* Check if FileData is multipart data.
|
|
1245
1265
|
*/
|
|
@@ -1247,15 +1267,24 @@ const isMultipart = (fileSize, multipartMinFileSize = defaultSettings.multipartM
|
|
|
1247
1267
|
return fileSize >= multipartMinFileSize;
|
|
1248
1268
|
};
|
|
1249
1269
|
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1270
|
+
/**
|
|
1271
|
+
* Uuid type guard.
|
|
1272
|
+
*/
|
|
1273
|
+
const isUuid = (data) => {
|
|
1274
|
+
const UUID_REGEX = '[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}';
|
|
1275
|
+
const regExp = new RegExp(UUID_REGEX);
|
|
1276
|
+
return !isFileData(data) && regExp.test(data);
|
|
1277
|
+
};
|
|
1278
|
+
/**
|
|
1279
|
+
* Url type guard.
|
|
1280
|
+
*
|
|
1281
|
+
* @param {AnyFile | Url | Uuid} data
|
|
1282
|
+
*/
|
|
1283
|
+
const isUrl = (data) => {
|
|
1284
|
+
const URL_REGEX = '^(?:\\w+:)?\\/\\/([^\\s\\.]+\\.\\S{2}|localhost[\\:?\\d]*)\\S*$';
|
|
1285
|
+
const regExp = new RegExp(URL_REGEX);
|
|
1286
|
+
return !isFileData(data) && regExp.test(data);
|
|
1254
1287
|
};
|
|
1255
|
-
|
|
1256
|
-
function prepareChunks(file, fileSize, chunkSize) {
|
|
1257
|
-
return (index) => sliceChunk(file, index, fileSize, chunkSize);
|
|
1258
|
-
}
|
|
1259
1288
|
|
|
1260
1289
|
const runWithConcurrency = (concurrency, tasks) => {
|
|
1261
1290
|
return new Promise((resolve, reject) => {
|
|
@@ -1292,6 +1321,16 @@ const runWithConcurrency = (concurrency, tasks) => {
|
|
|
1292
1321
|
});
|
|
1293
1322
|
};
|
|
1294
1323
|
|
|
1324
|
+
const sliceChunk = (file, index, fileSize, chunkSize) => {
|
|
1325
|
+
const start = chunkSize * index;
|
|
1326
|
+
const end = Math.min(start + chunkSize, fileSize);
|
|
1327
|
+
return file.slice(start, end);
|
|
1328
|
+
};
|
|
1329
|
+
|
|
1330
|
+
const prepareChunks = async (file, fileSize, chunkSize) => {
|
|
1331
|
+
return (index) => sliceChunk(file, index, fileSize, chunkSize);
|
|
1332
|
+
};
|
|
1333
|
+
|
|
1295
1334
|
const uploadPart = (chunk, url, { publicKey, onProgress, signal, integration, retryThrottledRequestMaxTimes, retryNetworkErrorMaxTimes }) => multipartUpload(chunk, url, {
|
|
1296
1335
|
publicKey,
|
|
1297
1336
|
onProgress,
|
|
@@ -1300,8 +1339,8 @@ const uploadPart = (chunk, url, { publicKey, onProgress, signal, integration, re
|
|
|
1300
1339
|
retryThrottledRequestMaxTimes,
|
|
1301
1340
|
retryNetworkErrorMaxTimes
|
|
1302
1341
|
});
|
|
1303
|
-
const uploadMultipart = (file, { publicKey, fileName, fileSize, baseURL, secureSignature, secureExpire, store, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, retryNetworkErrorMaxTimes, contentType, multipartChunkSize = defaultSettings.multipartChunkSize, maxConcurrentRequests = defaultSettings.maxConcurrentRequests, baseCDN, metadata }) => {
|
|
1304
|
-
const size = fileSize || getFileSize(file);
|
|
1342
|
+
const uploadMultipart = async (file, { publicKey, fileName, fileSize, baseURL, secureSignature, secureExpire, store, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, retryNetworkErrorMaxTimes, contentType, multipartChunkSize = defaultSettings.multipartChunkSize, maxConcurrentRequests = defaultSettings.maxConcurrentRequests, baseCDN, metadata }) => {
|
|
1343
|
+
const size = fileSize || await getFileSize(file);
|
|
1305
1344
|
let progressValues;
|
|
1306
1345
|
const createProgressHandler = (totalChunks, chunkIdx) => {
|
|
1307
1346
|
if (!onProgress)
|
|
@@ -1323,8 +1362,8 @@ const uploadMultipart = (file, { publicKey, fileName, fileSize, baseURL, secureS
|
|
|
1323
1362
|
};
|
|
1324
1363
|
return multipartStart(size, {
|
|
1325
1364
|
publicKey,
|
|
1326
|
-
contentType,
|
|
1327
|
-
fileName: fileName
|
|
1365
|
+
contentType: contentType || getContentType(file),
|
|
1366
|
+
fileName: fileName || getFilename(file),
|
|
1328
1367
|
baseURL,
|
|
1329
1368
|
secureSignature,
|
|
1330
1369
|
secureExpire,
|
|
@@ -1337,8 +1376,8 @@ const uploadMultipart = (file, { publicKey, fileName, fileSize, baseURL, secureS
|
|
|
1337
1376
|
retryNetworkErrorMaxTimes,
|
|
1338
1377
|
metadata
|
|
1339
1378
|
})
|
|
1340
|
-
.then(({ uuid, parts }) => {
|
|
1341
|
-
const getChunk = prepareChunks(file, size, multipartChunkSize);
|
|
1379
|
+
.then(async ({ uuid, parts }) => {
|
|
1380
|
+
const getChunk = await prepareChunks(file, size, multipartChunkSize);
|
|
1342
1381
|
return Promise.all([
|
|
1343
1382
|
uuid,
|
|
1344
1383
|
runWithConcurrency(maxConcurrentRequests, parts.map((url, index) => () => uploadPart(getChunk(index), url, {
|
|
@@ -1385,14 +1424,15 @@ const uploadMultipart = (file, { publicKey, fileName, fileSize, baseURL, secureS
|
|
|
1385
1424
|
/**
|
|
1386
1425
|
* Uploads file from provided data.
|
|
1387
1426
|
*/
|
|
1388
|
-
function uploadFile(data, { publicKey, fileName, baseURL = defaultSettings.baseURL, secureSignature, secureExpire, store, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, retryNetworkErrorMaxTimes, contentType, multipartMinFileSize, multipartChunkSize, maxConcurrentRequests, baseCDN = defaultSettings.baseCDN, checkForUrlDuplicates, saveUrlForRecurrentUploads, pusherKey, metadata }) {
|
|
1427
|
+
async function uploadFile(data, { publicKey, fileName, baseURL = defaultSettings.baseURL, secureSignature, secureExpire, store, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, retryNetworkErrorMaxTimes, contentType, multipartMinFileSize, multipartChunkSize, maxConcurrentRequests, baseCDN = defaultSettings.baseCDN, checkForUrlDuplicates, saveUrlForRecurrentUploads, pusherKey, metadata }) {
|
|
1389
1428
|
if (isFileData(data)) {
|
|
1390
|
-
const fileSize = getFileSize(data);
|
|
1429
|
+
const fileSize = await getFileSize(data);
|
|
1391
1430
|
if (isMultipart(fileSize, multipartMinFileSize)) {
|
|
1392
1431
|
return uploadMultipart(data, {
|
|
1393
1432
|
publicKey,
|
|
1394
1433
|
contentType,
|
|
1395
1434
|
multipartChunkSize,
|
|
1435
|
+
fileSize,
|
|
1396
1436
|
fileName,
|
|
1397
1437
|
baseURL,
|
|
1398
1438
|
secureSignature,
|