Skip to content

Commit 8a8e5e7

Browse files
Merge pull request #19 from apivideo/abstract-uploader
Abstract uploader
2 parents ccd26c8 + 77c5578 commit 8a8e5e7

21 files changed

+665
-372
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Changelog
22
All changes to this project will be documented in this file.
33

4+
## [1.0.8] - 2022-04-27
5+
- Create a AbstractUploader
6+
- Add origin header
7+
- Add the possibility to provide a refresh token
8+
49
## [1.0.7] - 2022-04-26
510
- Don't retry on 401 errors
611
- Mutualize some code

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,12 @@ Using delegated upload tokens for authentication is best options when uploading
155155
**Warning**: be aware that exposing your access token client-side can lead to huge security issues. Use this method only if you know what you're doing :).
156156

157157

158-
| Option name | Mandatory | Type | Description |
159-
| ----------------------------: | --------- | ------ | ----------------------- |
160-
| accessToken | **yes** | string | your access token |
161-
| videoId | **yes** | string | id of an existing video |
162-
| _common options (see bellow)_ | | | |
158+
| Option name | Mandatory | Type | Description |
159+
| ----------------------------: | --------- | ------ | ----------------------------------------------------------------------------------------------------------------------------------------------- |
160+
| accessToken | **yes** | string | your access token |
161+
| refreshToken | **no** | string | your refresh token (please not that if you don't provide a refresh token, your upload may fails due to the access token lifetime of 60 minutes) |
162+
| videoId | **yes** | string | id of an existing video |
163+
| _common options (see bellow)_ | | | |
163164

164165

165166
#### Using an API key (**strongly** discouraged):

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/abstract-uploader.d.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
export declare const MIN_CHUNK_SIZE: number;
2+
export declare const DEFAULT_CHUNK_SIZE: number;
3+
export declare const MAX_CHUNK_SIZE: number;
4+
export declare const DEFAULT_RETRIES = 5;
5+
export declare const DEFAULT_API_HOST = "ws.api.video";
6+
export declare type VideoUploadResponse = {
7+
readonly videoId: string;
8+
readonly title?: string;
9+
readonly description?: string;
10+
readonly _public?: boolean;
11+
readonly panoramic?: boolean;
12+
readonly mp4Support?: boolean;
13+
readonly publishedAt?: Date;
14+
readonly createdAt?: Date;
15+
readonly updatedAt?: Date;
16+
readonly tags?: string[];
17+
readonly metadata?: {
18+
readonly key?: string;
19+
readonly value?: string;
20+
}[];
21+
readonly source?: {
22+
readonly type?: string;
23+
readonly uri?: string;
24+
};
25+
readonly assets?: {
26+
readonly iframe?: string;
27+
readonly player?: string;
28+
readonly hls?: string;
29+
readonly thumbnail?: string;
30+
};
31+
};
32+
export interface CommonOptions {
33+
apiHost?: string;
34+
retries?: number;
35+
}
36+
export interface WithUploadToken {
37+
uploadToken: string;
38+
videoId?: string;
39+
}
40+
export interface WithAccessToken {
41+
accessToken: string;
42+
refreshToken?: string;
43+
videoId: string;
44+
}
45+
export interface WithApiKey {
46+
apiKey: string;
47+
videoId: string;
48+
}
49+
export declare type VideoUploadError = {
50+
status: number;
51+
type?: string;
52+
title?: string;
53+
reason?: string;
54+
raw: string;
55+
};
56+
declare type HXRRequestParams = {
57+
parts?: {
58+
currentPart: number;
59+
totalParts: number | "*";
60+
};
61+
onProgress?: (e: ProgressEvent) => void;
62+
body: Document | XMLHttpRequestBodyInit | null;
63+
};
64+
export declare abstract class AbstractUploader<T> {
65+
protected uploadEndpoint: string;
66+
protected videoId?: string;
67+
protected retries: number;
68+
protected headers: {
69+
[name: string]: string;
70+
};
71+
protected onProgressCallbacks: ((e: T) => void)[];
72+
protected refreshToken?: string;
73+
protected apiHost: string;
74+
constructor(options: CommonOptions & (WithAccessToken | WithUploadToken | WithApiKey));
75+
onProgress(cb: (e: T) => void): void;
76+
protected parseErrorResponse(xhr: XMLHttpRequest): VideoUploadError;
77+
protected apiResponseToVideoUploadResponse(response: any): VideoUploadResponse;
78+
protected sleep(duration: number): Promise<void>;
79+
protected xhrWithRetrier(params: HXRRequestParams): Promise<VideoUploadResponse>;
80+
protected createFormData(file: Blob, fileName: string, startByte?: number, endByte?: number): FormData;
81+
doRefreshToken(): Promise<void>;
82+
private createXhrPromise;
83+
private withRetrier;
84+
}
85+
export {};

dist/src/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export { UploadProgressEvent, VideoUploader, VideoUploaderOptionsWithAccessToken, VideoUploaderOptionsWithUploadToken } from "./video-uploader";
22
export { ProgressiveUploadProgressEvent, ProgressiveUploader, ProgressiveUploaderOptionsWithAccessToken, ProgressiveUploaderOptionsWithUploadToken } from './progressive-video-uploader';
3-
export { VideoUploadResponse, MIN_CHUNK_SIZE, MAX_CHUNK_SIZE } from './common';
3+
export { VideoUploadResponse, MIN_CHUNK_SIZE, MAX_CHUNK_SIZE } from './abstract-uploader';
Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
1-
import { VideoUploadResponse } from "./common";
2-
export interface ProgressiveUploaderOptionsWithUploadToken extends Options {
3-
uploadToken: string;
4-
videoId?: string;
1+
import { AbstractUploader, CommonOptions, VideoUploadResponse, WithAccessToken, WithApiKey, WithUploadToken } from "./abstract-uploader";
2+
export interface ProgressiveUploaderOptionsWithUploadToken extends CommonOptions, WithUploadToken {
53
}
6-
export interface ProgressiveUploaderOptionsWithAccessToken extends Options {
7-
accessToken: string;
8-
videoId: string;
4+
export interface ProgressiveUploaderOptionsWithAccessToken extends CommonOptions, WithAccessToken {
95
}
10-
export interface ProgressiveUploaderOptionsWithApiKey extends Options {
11-
apiKey: string;
12-
videoId: string;
13-
}
14-
interface Options {
15-
apiHost?: string;
16-
retries?: number;
6+
export interface ProgressiveUploaderOptionsWithApiKey extends CommonOptions, WithApiKey {
177
}
188
export interface ProgressiveUploadProgressEvent {
199
uploadedBytes: number;
@@ -23,22 +13,13 @@ export interface ProgressiveProgressEvent {
2313
uploadedBytes: number;
2414
totalBytes: number;
2515
}
26-
export declare class ProgressiveUploader {
27-
private uploadEndpoint;
28-
private videoId?;
29-
private retries;
30-
private onProgressCallbacks;
31-
private headers;
16+
export declare class ProgressiveUploader extends AbstractUploader<ProgressiveProgressEvent> {
3217
private currentPartNum;
3318
private currentPartBlobs;
3419
private currentPartBlobsSize;
3520
private queue;
3621
constructor(options: ProgressiveUploaderOptionsWithAccessToken | ProgressiveUploaderOptionsWithUploadToken | ProgressiveUploaderOptionsWithApiKey);
37-
onProgress(cb: (e: ProgressiveProgressEvent) => void): void;
3822
uploadPart(file: Blob): Promise<void>;
3923
uploadLastPart(file: Blob): Promise<VideoUploadResponse>;
40-
private createFormData;
41-
private sleep;
4224
private upload;
4325
}
44-
export {};

dist/src/video-uploader.d.ts

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
1-
import { VideoUploadResponse } from "./common";
2-
export interface VideoUploaderOptionsWithUploadToken extends Options {
3-
uploadToken: string;
4-
videoId?: string;
1+
import { AbstractUploader, CommonOptions, VideoUploadResponse, WithAccessToken, WithApiKey, WithUploadToken } from "./abstract-uploader";
2+
interface UploadOptions {
3+
file: File;
4+
chunkSize?: number;
55
}
6-
export interface VideoUploaderOptionsWithAccessToken extends Options {
7-
accessToken: string;
8-
videoId: string;
6+
export interface VideoUploaderOptionsWithUploadToken extends CommonOptions, UploadOptions, WithUploadToken {
97
}
10-
export interface VideoUploaderOptionsWithApiKey extends Options {
11-
apiKey: string;
12-
videoId: string;
8+
export interface VideoUploaderOptionsWithAccessToken extends CommonOptions, UploadOptions, WithAccessToken {
139
}
14-
interface Options {
15-
file: File;
16-
chunkSize?: number;
17-
apiHost?: string;
18-
retries?: number;
10+
export interface VideoUploaderOptionsWithApiKey extends CommonOptions, UploadOptions, WithApiKey {
1911
}
2012
export interface UploadProgressEvent {
2113
uploadedBytes: number;
@@ -25,24 +17,14 @@ export interface UploadProgressEvent {
2517
currentChunk: number;
2618
currentChunkUploadedBytes: number;
2719
}
28-
export declare class VideoUploader {
20+
export declare class VideoUploader extends AbstractUploader<UploadProgressEvent> {
2921
private file;
3022
private chunkSize;
31-
private uploadEndpoint;
32-
private currentChunk;
3323
private chunksCount;
3424
private fileSize;
3525
private fileName;
36-
private videoId?;
37-
private retries;
38-
private onProgressCallbacks;
39-
private headers;
40-
private queue;
4126
constructor(options: VideoUploaderOptionsWithAccessToken | VideoUploaderOptionsWithUploadToken | VideoUploaderOptionsWithApiKey);
42-
onProgress(cb: (e: UploadProgressEvent) => void): void;
4327
upload(): Promise<VideoUploadResponse>;
44-
private sleep;
45-
private createFormData;
4628
private uploadCurrentChunk;
4729
}
4830
export {};

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@api.video/video-uploader",
3-
"version": "1.0.7",
3+
"version": "1.0.8",
44
"description": "api.video video uploader",
55
"repository": {
66
"type": "git",

sample/index.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<html>
2+
<head>
3+
<script src="../dist/index.js"></script>
4+
</head>
5+
<body>
6+
7+
<form>
8+
<input type="file" id="input" onchange="uploadFile(this.files)">
9+
</form>
10+
<script type="text/javascript">
11+
function uploadFile(files) {
12+
const uploader = new VideoUploader({
13+
file: files[0],
14+
videoId: "vi7Wub9QiEDdkMd5CZmnrD1Y",
15+
accessToken: "1ZeyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpYXQiOjE2NTMzMTAwMzIuMzE4MzgyLCJuYmYiOjE2NTMzMTAwMzIuMzE4MzgyLCJleHAiOjE2NTMzMTM2MzIuMzE4MzgyLCJhcGlLZXlWYWx1ZSI6IjNYbUwxTXE0NkxQUmNnazVDTW1lMmZCc0liWU1LWGJRSVg1RHJUa0NPamcifQ.MCGVn1CrdEnKL_b-3MGuNTdb3y-IoUCwoikzIQEzWIyJgsz7338UJ_yYF0njGXJFjjhOWz3M_ywyVkr5E1Vx0K8RExq5gUEvehmBERkoOtPjYLxvIsKCms7mml_cPBWuh21-ofxNKH1W58AhP8uvvNTqSSKgeh01abOr8gpyaR8cgqAEcFjkcko0LULAEb44mCmiBMBwJAAfh4iuAyyqLQO1TC63ev92xAhWyKJY23npHfAkzpE_F8HpCp6VbAQwZdZ6Tmsg2lj4kMFgvZi2EiaUZuW5RS5rkq4uBTmuOBNNTqwtuSl0bYcp-NBQ4W2Ubc_Kw04_2KcIkn4O1NEwbw",
16+
refreshToken: "1eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpYXQiOjE2NTMzMTE3MDMuMzE5NTcyLCJuYmYiOjE2NTMzMTE3MDMuMzE5NTcyLCJleHAiOjE2NTU5OTAxMDMuMzE5NTcyLCJyZWZyZXNoVG9rZW4iOnRydWUsImFwaUtleVZhbHVlIjoiM1htTDFNcTQ2TFBSY2drNUNNbWUyZkJzSWJZTUtYYlFJWDVEclRrQ09qZyJ9.soMScSuENRqqrIIOXdDnevDc1iNbl9Qcow71D6bOn4f9corRQhHgRwhOpKNoVAYX-SbbAgd1rbjiG95YgFLB9avDvCxqtW-7m_YVylywpGXOoaoz7wJSS2isB794_NYq6vAlivX22MB9aeA84JzwEZyajIOcwCJ-eOG-JbrW6kAtjW2Fl2I6rGIc6qQidYFSidZYI16p88aV-_-fHk8wV70B2Wb7CNOjrFeR51aIgtqeA-oheluRUwb0_3z5fgSTOy03CtOriloNSf9kGexvACnu1sIJ10D2cbHJshPv7djlQn8doGxhzoMpN7qy9MifO7KY5VFer_hQD0FuNFr5YQ"
17+
});
18+
19+
uploader.upload().catch(e => console.log(e));
20+
}
21+
</script>
22+
</body>
23+
</html>

0 commit comments

Comments
 (0)