-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathresult.ts
More file actions
94 lines (84 loc) · 2.81 KB
/
Copy pathresult.ts
File metadata and controls
94 lines (84 loc) · 2.81 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import {FileValue} from "./file-param.js";
import {FilesValue} from "./files-param.js";
export interface IResultFileDto {
FileName: string,
FileExt: string,
FileSize: number,
FileId: string,
Url: string
}
export interface IResultDto {
ConversionCost: number,
Files: IResultFileDto[]
}
export interface IResultErrorDto {
Code: number,
Message: string
}
export default class Result {
/**
* Conversion result class. Can be used to get information about converted file(s) as well as passing result to other chained conversion `Parameters`.
*
* @param dto
*/
constructor(
private readonly dto: IResultDto
) {}
/**
* Returns conversion duration seconds
*/
public get duration(): number {
return this.dto.ConversionCost
}
/**
* Returns conversion result files.
*/
public get files(): IResultFileDto[] {
return this.dto.Files
}
/**
* Returns file parameter value. Value can be used to add file parameter to the chained conversion `Parameters` object.
* ```ts
* params.add('file', result.toParamFile())
* ```
* @param idx - If conversion result contains more than one file, then provided file index will be used.
*/
public toParamFile(idx: number=0): FileValue {
return new FileValue(this.dto.Files[idx].FileName, this.dto.Files[idx].FileId)
}
/**
* Returns multiple files parameter value. Value can be used to add files parameter to the chained conversion `Parameters` object.
* ```ts
* params.add('files', result.toParamFiles())
* ```
* Can only be used with the conversions that accept multiple input files, such as `pdf` -> 'merge' conversion.
*/
public toParamFiles(): FilesValue {
return new FilesValue(this.dto.Files)
}
/**
* Uploads file to AWS S3
* @param region - AWS Region
* @param bucket - S3 Bucket name
* @param accessKeyId - AWS Access Key ID
* @param secretAccessKey - AWS Access Key Secret
* @returns - Array of promises that resolves to S3 PUT request responses
*/
public uploadToS3(region: string, bucket: string, accessKeyId: string, secretAccessKey: string): Promise<Response>[] {
return this.dto.Files.map(f => {
let dto = {
// TODO: fileServerUrl: params.get('fileServerUrl'),
region: region,
bucket: bucket,
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
fileId: f.FileId
}
return fetch(`https://integration.convertapi.com/s3/upload`, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify(dto)
})
})
}
}