-
Notifications
You must be signed in to change notification settings - Fork 23
/
multipart-form-data.ts
40 lines (36 loc) · 1.11 KB
/
multipart-form-data.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
import { AuthorizeRequest, resolveUrl, Uploader } from 'ngx-uploadx';
/**
* Multipart/form-data extended uploader for use with node-uploadx
* @example
* options: UploadxOptions = {
* allowedTypes: 'image/*',
* uploaderClass: MultiPartFormData,
* token: getToken
* };
*/
export class MultiPartFormData extends Uploader {
async getFileUrl(): Promise<string> {
this.offset = 0;
const formData: FormData = new FormData();
formData.set('metadata', JSON.stringify(this.metadata));
formData.append('file', this.file, this.file.name);
await this.request({
method: 'POST',
body: formData,
url: this.endpoint
});
this.offset = this.size;
const location = this.getValueFromResponse('location');
return location ? resolveUrl(location, this.endpoint) : '';
}
async sendFileContent(): Promise<number | undefined> {
return this.size;
}
async getOffset(): Promise<number | undefined> {
return 0;
}
protected override _authorize: AuthorizeRequest = (req, token) => {
token && (req.headers['Authorization'] = `Basic ${token}`);
return req;
};
}