-
Notifications
You must be signed in to change notification settings - Fork 23
/
tus-ext.ts
38 lines (37 loc) · 1.18 KB
/
tus-ext.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
import { b64, resolveUrl, Tus } from 'ngx-uploadx';
/**
* Implements tus resumable upload protocol
* {@link https://github.com/tus/tus-resumable-upload-protocol/blob/master/protocol.md `Creation With Upload` extension}
* @example
* options: UploadxOptions = {
* allowedTypes: 'image/*,video/*',
* endpoint: `https://master.tus.io/files/`,
* uploaderClass: TusExt
* };
*/
export class TusExt extends Tus {
async getFileUrl(): Promise<string> {
this.offset = 0;
this.chunkSize = this.file.size;
const encodedMetaData = b64.serialize(this.metadata);
const { body } = this.getChunk();
const headers = {
'Upload-Length': `${this.size}`,
'Upload-Metadata': `${encodedMetaData}`,
'Content-Type': 'application/offset+octet-stream',
'Upload-Offset': `${this.offset}`
};
await this.request({
method: 'POST',
url: this.endpoint,
headers,
body
});
const location = this.getValueFromResponse('location');
if (!location) {
throw new Error('Invalid or missing Location header');
}
this.offset = this.getOffsetFromResponse() || this.size;
return resolveUrl(location, this.endpoint);
}
}