-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodels.ts
76 lines (65 loc) · 2.42 KB
/
models.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Implement for XHR and managedFile arr member.
export type IXhrRequestState = 'PENDING' | 'READY' | 'COMPLETE' | 'ABORTED' | 'ERROR';
export class Policy {
s3Signature: string;
signatureKey: string;
base64Policy: string;
accessKeyId: string;
xAmzCredential: string;
xAmzMetaUuid: string;
xAmzDate: string;
xAmzAcl: string;
xAmzBucket: string;
xAmzAlgorithm: string;
xAmzServerSideEncryption: string;
folder: string;
constructor(private policy) {
this.s3Signature = policy.s3Signature;
this.accessKeyId = policy.accessKeyId;
this.signatureKey = policy.signatureKey;
this.base64Policy = policy.base64Policy;
this.folder = policy.folder;
this.xAmzAcl = policy.xAmzAcl;
this.xAmzDate = policy.xAmzDate;
this.xAmzBucket = policy.xAmzBucket;
this.xAmzMetaUuid = policy.xAmzMetaUuid;
this.xAmzAlgorithm = policy.xAmzAlgorithm;
this.xAmzCredential = policy.xAmzCredential;
this.xAmzServerSideEncryption = policy.xAmzServerSideEncryption;
}
}
export class ManagedUpload {
status: IXhrRequestState;
file: File;
formData: FormData;
xhr: XMLHttpRequest;
path: string;
progress: number;
total: number;
constructor(file, folder, path, policy: Policy) {
this.file = file;
this.formData = this._createFormData(file, folder, path, policy);
this.status = 'READY';
this.path = path;
this.progress = 0;
this.total = 0;
}
private _createFormData(file, folder, path, policy) {
let formData = new FormData;
if (!policy)
throw new Error('No valid policy in Managed upload creator');
formData.append('acl', policy.xAmzAcl);
formData.append('Content-Type', file.type);
formData.append('X-Amz-Date', policy.xAmzDate);
formData.append('x-amz-server-side-encryption', policy.xAmzServerSideEncryption);
formData.append('x-amz-meta-uuid', policy.xAmzMetaUuid);
formData.append('X-Amz-Algorithm', policy.xAmzAlgorithm);
formData.append('X-Amz-Credential', policy.xAmzCredential);
formData.append('X-Amz-Signature', policy.s3Signature);
formData.append('Policy', policy.base64Policy);
formData.append('key', folder + '/' + file.name);
// File field must come last!
formData.append('file', file);
return formData;
}
}