]>
Commit | Line | Data |
---|---|---|
f6d6e7f8 | 1 | import { objectToFormData } from '@app/helpers' |
2 | import { resolveUrl, UploaderX } from 'ngx-uploadx' | |
3 | ||
4 | /** | |
5 | * multipart/form-data uploader extending the UploaderX implementation of Google Resumable | |
6 | * for use with multer | |
7 | * | |
8 | * @see https://github.com/kukhariev/ngx-uploadx/blob/637e258fe366b8095203f387a6101a230ee4f8e6/src/uploadx/lib/uploaderx.ts | |
9 | * @example | |
10 | * | |
11 | * options: UploadxOptions = { | |
12 | * uploaderClass: UploaderXFormData | |
13 | * }; | |
14 | */ | |
15 | export class UploaderXFormData extends UploaderX { | |
16 | ||
17 | async getFileUrl (): Promise<string> { | |
18 | const headers = { | |
19 | 'X-Upload-Content-Length': this.size.toString(), | |
20 | 'X-Upload-Content-Type': this.file.type || 'application/octet-stream' | |
21 | } | |
22 | ||
23 | const previewfile = this.metadata.previewfile as any as File | |
24 | delete this.metadata.previewfile | |
25 | ||
26 | const data = objectToFormData(this.metadata) | |
27 | if (previewfile !== undefined) { | |
28 | data.append('previewfile', previewfile, previewfile.name) | |
29 | data.append('thumbnailfile', previewfile, previewfile.name) | |
30 | } | |
31 | ||
32 | await this.request({ | |
33 | method: 'POST', | |
34 | body: data, | |
35 | url: this.endpoint, | |
36 | headers | |
37 | }) | |
38 | ||
39 | const location = this.getValueFromResponse('location') | |
40 | if (!location) { | |
41 | throw new Error('Invalid or missing Location header') | |
42 | } | |
43 | ||
44 | this.offset = this.responseStatus === 201 ? 0 : undefined | |
45 | ||
46 | return resolveUrl(location, this.endpoint) | |
47 | } | |
48 | } |