Skip to content

Commit e090dc9

Browse files
authored
Merge pull request #25 from apivideo/feat/custom_video_name
Feat/custom video name
2 parents 0abdd60 + a0f927b commit e090dc9

File tree

8 files changed

+64
-13
lines changed

8 files changed

+64
-13
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# Changelog
22
All changes to this project will be documented in this file.
33

4+
## [1.1.0] - 2022-07-06
5+
- Video upload & Progressive upload: allow user to set a customized video name.
6+
47
## [1.0.11] - 2022-07-06
58
- Add origin headers
6-
-
9+
710
## [1.0.10] - 2022-06-29
811
- Retry even if the server is not responding.
912
- Add possibility to define a custom retry policy.

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,14 @@ Using delegated upload tokens for authentication is best options when uploading
178178
#### Common options
179179

180180

181-
| Option name | Mandatory | Type | Description |
182-
| ------------: | --------- | --------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------- |
183-
| file | **yes** | File | the file you want to upload |
184-
| chunkSize | no | number | number of bytes of each upload chunk (default: 50MB, min: 5MB, max: 128MB) |
185-
| apiHost | no | string | api.video host (default: ws.api.video) |
186-
| retries | no | number | number of retries when an API call fails (default: 5) |
187-
| retryStrategy | no | (retryCount: number, error: VideoUploadError) => number \| null | function that returns the number of ms to wait before retrying a failed upload. Returns null to stop retrying |
181+
| Option name | Mandatory | Type | Description |
182+
| ------------: | --------- | --------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
183+
| file | **yes** | File | the file you want to upload |
184+
| videoName | no | string | the name of your video (overrides the original file name for regular uploads, overrides the default "file" name for progressive uploads) |
185+
| chunkSize | no | number | number of bytes of each upload chunk (default: 50MB, min: 5MB, max: 128MB) |
186+
| apiHost | no | string | api.video host (default: ws.api.video) |
187+
| retries | no | number | number of retries when an API call fails (default: 5) |
188+
| retryStrategy | no | (retryCount: number, error: VideoUploadError) => number \| null | function that returns the number of ms to wait before retrying a failed upload. Returns null to stop retrying |
188189

189190

190191
### Example

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@api.video/video-uploader",
3-
"version": "1.0.11",
3+
"version": "1.1.0",
44
"description": "api.video video uploader",
55
"repository": {
66
"type": "git",
@@ -20,7 +20,8 @@
2020
"tslint": "tslint --project .",
2121
"build": "npm run tslint && webpack --mode production",
2222
"prepublishOnly": "npm run build",
23-
"test": "npm run build && mocha -r ts-node/register -r jsdom-global/register 'test/**/*.ts'"
23+
"test": "npm run build && mocha -r ts-node/register -r jsdom-global/register 'test/**/*.ts'",
24+
"watch": "npx webpack --watch --mode=development"
2425
},
2526
"devDependencies": {
2627
"@types/chai": "^4.3.3",

sample/index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88

99
<form>
1010
<input type="file" id="input" onchange="uploadFile(this.files)">
11+
<input type="text" id="VideoName" placeholder="Video name">
1112
</form>
1213
<script type="text/javascript">
1314
function uploadFile(files) {
15+
const videoName = document.getElementById('VideoName').value
1416
const uploader = new VideoUploader({
1517
file: files[0],
16-
uploadToken: "to7EcLLzRSsqhkzxyIhavEHA" // ecosystem sandbox upload token
18+
uploadToken: "to7EcLLzRSsqhkzxyIhavEHA", // ecosystem sandbox upload token
19+
videoName: videoName
1720
});
1821

1922
uploader.upload()

src/abstract-uploader.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ interface Origin {
4141
export interface CommonOptions {
4242
apiHost?: string;
4343
retries?: number;
44+
videoName?: string;
4445
retryStrategy?: RetryStrategy;
4546
origin?: {
4647
application?: Origin;

src/progressive-video-uploader.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ export class ProgressiveUploader extends AbstractUploader<ProgressiveProgressEve
2525
private currentPartBlobsSize = 0;
2626
private queue = new PromiseQueue();
2727
private preventEmptyParts: boolean;
28+
private fileName: string;
2829

2930
constructor(options: ProgressiveUploaderOptionsWithAccessToken | ProgressiveUploaderOptionsWithUploadToken | ProgressiveUploaderOptionsWithApiKey) {
3031
super(options);
3132
this.preventEmptyParts = options.preventEmptyParts || false;
33+
this.fileName = options.videoName || 'file';
3234
}
3335

3436
public uploadPart(file: Blob): Promise<void> {
@@ -71,7 +73,7 @@ export class ProgressiveUploader extends AbstractUploader<ProgressiveProgressEve
7173
private async upload(file: Blob, isLast: boolean = false): Promise<VideoUploadResponse> {
7274
const fileSize = file.size;
7375
return this.xhrWithRetrier({
74-
body: this.createFormData(file, "file"),
76+
body: this.createFormData(file, this.fileName),
7577
parts: {
7678
currentPart: this.currentPartNum,
7779
totalParts: isLast ? this.currentPartNum : '*'

src/video-uploader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class VideoUploader extends AbstractUploader<UploadProgressEvent> {
3939
this.chunkSize = options.chunkSize || DEFAULT_CHUNK_SIZE;
4040
this.file = options.file;
4141
this.fileSize = this.file.size;
42-
this.fileName = this.file.name;
42+
this.fileName = options.videoName || this.file.name;
4343

4444
this.chunksCount = Math.ceil(this.fileSize / this.chunkSize);
4545
}

test/video-uploader.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,46 @@ describe('Delegated upload', () => {
197197

198198
uploader.upload().then(() => done());
199199
});
200+
201+
it('video name is file name', (done) => {
202+
const uploadToken = "the-upload-token";
203+
const videoId = "9876";
204+
const fileName = "filename"
205+
206+
const uploader = new VideoUploader({
207+
file: new File([new ArrayBuffer(10)], fileName),
208+
uploadToken,
209+
videoId
210+
});
211+
212+
mock.post(`https://ws.api.video/upload?token=${uploadToken}`, (req, res) => {
213+
expect(req.body().get("file").name).to.be.eql(fileName);
214+
return res.status(201).body("{}");
215+
});
216+
217+
uploader.upload().then(() => done());
218+
})
219+
220+
it('video name is customized', (done) => {
221+
const uploadToken = "the-upload-token";
222+
const videoId = "9876";
223+
const fileName = "filename"
224+
const videoName = "video name"
225+
226+
const uploader = new VideoUploader({
227+
file: new File([new ArrayBuffer(10)], fileName),
228+
uploadToken,
229+
videoId,
230+
videoName
231+
});
232+
233+
mock.post(`https://ws.api.video/upload?token=${uploadToken}`, (req, res) => {
234+
expect(req.body().get("file").name).to.be.eql(videoName);
235+
return res.status(201).body("{}");
236+
});
237+
238+
uploader.upload().then(() => done());
239+
})
200240
});
201241

202242
describe('Progress listener', () => {

0 commit comments

Comments
 (0)