Skip to content

Commit b3ba90d

Browse files
committed
feat: add jobs.getExportUrls utility
1 parent 13dee13 commit b3ba90d

5 files changed

Lines changed: 158 additions & 10 deletions

File tree

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,7 @@ CloudConvert can generate public URLs for using `export/url` tasks. You can use
5858
```js
5959
job = await cloudConvert.jobs.wait(job.id); // Wait for job completion
6060

61-
const exportTask = job.tasks.filter(
62-
task => task.operation === 'export/url' && task.status === 'finished'
63-
)[0];
64-
const file = exportTask.result.files[0];
61+
const file = this.cloudConvert.jobs.getExportUrls(job)[0];
6562

6663
const writeStream = fs.createWriteStream('./out/' + file.filename);
6764

lib/JobsResource.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,13 @@ export default class JobsResource {
102102
}
103103

104104
getExportUrls(job: Job): FileResult[] {
105-
return job.tasks.flatMap(task => task.result?.files ?? []);
105+
return job.tasks
106+
.filter(
107+
task =>
108+
task.operation === 'export/url' &&
109+
task.status === 'finished'
110+
)
111+
.flatMap(task => task.result?.files ?? []);
106112
}
107113
}
108114

tests/integration/JobsResourceTest.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ describe('JobsResource', () => {
4444
assert.equal(job.status, 'finished');
4545

4646
// download export file
47-
48-
const exportTask = job.tasks.filter(
49-
task => task.name === 'export-it'
50-
)[0];
51-
const file = exportTask.result.files[0];
47+
const file = this.cloudConvert.jobs.getExportUrls(job)[0];
5248

5349
assert.equal(file.filename, 'input.png');
5450

tests/unit/JobsResourceTest.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,35 @@ describe('JobsResource', () => {
7777
);
7878
});
7979
});
80+
81+
describe('getExportUrls()', () => {
82+
it('should extract the export URLs', async () => {
83+
nock('https://api.cloudconvert.com')
84+
.get('/v2/jobs/b2e4eb2b-a744-4da2-97cd-776d393532a8')
85+
.query(true)
86+
.replyWithFile(
87+
200,
88+
__dirname + '/responses/job_finished.json',
89+
{
90+
'Content-Type': 'application/json'
91+
}
92+
);
93+
94+
const job = await this.cloudConvert.jobs.get(
95+
'b2e4eb2b-a744-4da2-97cd-776d393532a8',
96+
{ include: 'tasks' }
97+
);
98+
99+
const exportUrls = this.cloudConvert.jobs.getExportUrls(job);
100+
101+
assert.isArray(exportUrls);
102+
assert.lengthOf(exportUrls, 1);
103+
104+
assert.equal(exportUrls[0].filename, 'original.png');
105+
assert.match(
106+
exportUrls[0].url,
107+
new RegExp('^https://storage.cloudconvert.com/')
108+
);
109+
});
110+
});
80111
});
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
{
2+
"data": {
3+
"id": "b2e4eb2b-a744-4da2-97cd-776d393532a8",
4+
"tag": "jobbuilder",
5+
"status": "finished",
6+
"created_at": "2022-09-26T08:14:11+00:00",
7+
"started_at": "2022-09-26T08:14:11+00:00",
8+
"ended_at": "2022-09-26T08:14:12+00:00",
9+
"tasks": [
10+
{
11+
"id": "1b7ccc22-e26c-43dc-85e7-c1ff8fbfd9b4",
12+
"name": "export-1",
13+
"job_id": "b2e4eb2b-a744-4da2-97cd-776d393532a8",
14+
"status": "finished",
15+
"credits": 0,
16+
"code": null,
17+
"message": null,
18+
"percent": 100,
19+
"operation": "export\/url",
20+
"result": {
21+
"files": [
22+
{
23+
"filename": "original.png",
24+
"size": 1040507,
25+
"url": "https:\/\/storage.cloudconvert.com\/tasks\/1b7ccc22-e26c-43dc-85e7-c1ff8fbfd9b4\/original.png?AWSAccessKeyId=cloudconvert-production&Expires=1664266452&Signature=TDScvmTUzCv4nk5EWkz2Aakxn%2Bs%3D&response-content-disposition=attachment%3B%20filename%3D%22original.png%22&response-content-type=image%2Fpng"
26+
}
27+
]
28+
},
29+
"created_at": "2022-09-26T08:14:11+00:00",
30+
"started_at": "2022-09-26T08:14:12+00:00",
31+
"ended_at": "2022-09-26T08:14:12+00:00",
32+
"retry_of_task_id": null,
33+
"copy_of_task_id": null,
34+
"user_id": 1,
35+
"priority": 50,
36+
"host_name": "dulce",
37+
"storage": "ceph-fra",
38+
"depends_on_task_ids": [
39+
"69534510-3eef-46c2-b7a6-71b3f1aed916"
40+
],
41+
"links": {
42+
"self": "https:\/\/api.cloudconvert.com\/v2\/tasks\/1b7ccc22-e26c-43dc-85e7-c1ff8fbfd9b4"
43+
}
44+
},
45+
{
46+
"id": "0a6fd2af-d795-4988-a104-e7c890608bc9",
47+
"name": "import-1",
48+
"job_id": "b2e4eb2b-a744-4da2-97cd-776d393532a8",
49+
"status": "finished",
50+
"credits": 0,
51+
"code": null,
52+
"message": null,
53+
"percent": 100,
54+
"operation": "import\/url",
55+
"result": {
56+
"files": [
57+
{
58+
"filename": "original.jpg",
59+
"size": 213864
60+
}
61+
]
62+
},
63+
"created_at": "2022-09-26T08:14:11+00:00",
64+
"started_at": "2022-09-26T08:14:11+00:00",
65+
"ended_at": "2022-09-26T08:14:11+00:00",
66+
"retry_of_task_id": null,
67+
"copy_of_task_id": null,
68+
"user_id": 1,
69+
"priority": 50,
70+
"host_name": "dulce",
71+
"storage": null,
72+
"depends_on_task_ids": [],
73+
"links": {
74+
"self": "https:\/\/api.cloudconvert.com\/v2\/tasks\/0a6fd2af-d795-4988-a104-e7c890608bc9"
75+
}
76+
},
77+
{
78+
"id": "69534510-3eef-46c2-b7a6-71b3f1aed916",
79+
"name": "task-1",
80+
"job_id": "b2e4eb2b-a744-4da2-97cd-776d393532a8",
81+
"status": "finished",
82+
"credits": 1,
83+
"code": null,
84+
"message": null,
85+
"percent": 100,
86+
"operation": "convert",
87+
"engine": "imagemagick",
88+
"engine_version": "7.1.0",
89+
"result": {
90+
"files": [
91+
{
92+
"filename": "original.png",
93+
"size": 1040507
94+
}
95+
]
96+
},
97+
"created_at": "2022-09-26T08:14:11+00:00",
98+
"started_at": "2022-09-26T08:14:11+00:00",
99+
"ended_at": "2022-09-26T08:14:11+00:00",
100+
"retry_of_task_id": null,
101+
"copy_of_task_id": null,
102+
"user_id": 1,
103+
"priority": 50,
104+
"host_name": "dulce",
105+
"storage": null,
106+
"depends_on_task_ids": [
107+
"0a6fd2af-d795-4988-a104-e7c890608bc9"
108+
],
109+
"links": {
110+
"self": "https:\/\/api.cloudconvert.com\/v2\/tasks\/69534510-3eef-46c2-b7a6-71b3f1aed916"
111+
}
112+
}
113+
],
114+
"links": {
115+
"self": "https:\/\/api.cloudconvert.com\/v2\/jobs\/b2e4eb2b-a744-4da2-97cd-776d393532a8"
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)