Skip to content

Commit

Permalink
feat: add jobs.getExportUrls utility
Browse files Browse the repository at this point in the history
  • Loading branch information
josiasmontag committed Sep 26, 2022
1 parent 13dee13 commit b3ba90d
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 10 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ CloudConvert can generate public URLs for using `export/url` tasks. You can use
```js
job = await cloudConvert.jobs.wait(job.id); // Wait for job completion

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

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

Expand Down
8 changes: 7 additions & 1 deletion lib/JobsResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,13 @@ export default class JobsResource {
}

getExportUrls(job: Job): FileResult[] {
return job.tasks.flatMap(task => task.result?.files ?? []);
return job.tasks
.filter(
task =>
task.operation === 'export/url' &&
task.status === 'finished'
)
.flatMap(task => task.result?.files ?? []);
}
}

Expand Down
6 changes: 1 addition & 5 deletions tests/integration/JobsResourceTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ describe('JobsResource', () => {
assert.equal(job.status, 'finished');

// download export file

const exportTask = job.tasks.filter(
task => task.name === 'export-it'
)[0];
const file = exportTask.result.files[0];
const file = this.cloudConvert.jobs.getExportUrls(job)[0];

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

Expand Down
31 changes: 31 additions & 0 deletions tests/unit/JobsResourceTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,35 @@ describe('JobsResource', () => {
);
});
});

describe('getExportUrls()', () => {
it('should extract the export URLs', async () => {
nock('https://api.cloudconvert.com')
.get('/v2/jobs/b2e4eb2b-a744-4da2-97cd-776d393532a8')
.query(true)
.replyWithFile(
200,
__dirname + '/responses/job_finished.json',
{
'Content-Type': 'application/json'
}
);

const job = await this.cloudConvert.jobs.get(
'b2e4eb2b-a744-4da2-97cd-776d393532a8',
{ include: 'tasks' }
);

const exportUrls = this.cloudConvert.jobs.getExportUrls(job);

assert.isArray(exportUrls);
assert.lengthOf(exportUrls, 1);

assert.equal(exportUrls[0].filename, 'original.png');
assert.match(
exportUrls[0].url,
new RegExp('^https://storage.cloudconvert.com/')
);
});
});
});
118 changes: 118 additions & 0 deletions tests/unit/responses/job_finished.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
{
"data": {
"id": "b2e4eb2b-a744-4da2-97cd-776d393532a8",
"tag": "jobbuilder",
"status": "finished",
"created_at": "2022-09-26T08:14:11+00:00",
"started_at": "2022-09-26T08:14:11+00:00",
"ended_at": "2022-09-26T08:14:12+00:00",
"tasks": [
{
"id": "1b7ccc22-e26c-43dc-85e7-c1ff8fbfd9b4",
"name": "export-1",
"job_id": "b2e4eb2b-a744-4da2-97cd-776d393532a8",
"status": "finished",
"credits": 0,
"code": null,
"message": null,
"percent": 100,
"operation": "export\/url",
"result": {
"files": [
{
"filename": "original.png",
"size": 1040507,
"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"
}
]
},
"created_at": "2022-09-26T08:14:11+00:00",
"started_at": "2022-09-26T08:14:12+00:00",
"ended_at": "2022-09-26T08:14:12+00:00",
"retry_of_task_id": null,
"copy_of_task_id": null,
"user_id": 1,
"priority": 50,
"host_name": "dulce",
"storage": "ceph-fra",
"depends_on_task_ids": [
"69534510-3eef-46c2-b7a6-71b3f1aed916"
],
"links": {
"self": "https:\/\/api.cloudconvert.com\/v2\/tasks\/1b7ccc22-e26c-43dc-85e7-c1ff8fbfd9b4"
}
},
{
"id": "0a6fd2af-d795-4988-a104-e7c890608bc9",
"name": "import-1",
"job_id": "b2e4eb2b-a744-4da2-97cd-776d393532a8",
"status": "finished",
"credits": 0,
"code": null,
"message": null,
"percent": 100,
"operation": "import\/url",
"result": {
"files": [
{
"filename": "original.jpg",
"size": 213864
}
]
},
"created_at": "2022-09-26T08:14:11+00:00",
"started_at": "2022-09-26T08:14:11+00:00",
"ended_at": "2022-09-26T08:14:11+00:00",
"retry_of_task_id": null,
"copy_of_task_id": null,
"user_id": 1,
"priority": 50,
"host_name": "dulce",
"storage": null,
"depends_on_task_ids": [],
"links": {
"self": "https:\/\/api.cloudconvert.com\/v2\/tasks\/0a6fd2af-d795-4988-a104-e7c890608bc9"
}
},
{
"id": "69534510-3eef-46c2-b7a6-71b3f1aed916",
"name": "task-1",
"job_id": "b2e4eb2b-a744-4da2-97cd-776d393532a8",
"status": "finished",
"credits": 1,
"code": null,
"message": null,
"percent": 100,
"operation": "convert",
"engine": "imagemagick",
"engine_version": "7.1.0",
"result": {
"files": [
{
"filename": "original.png",
"size": 1040507
}
]
},
"created_at": "2022-09-26T08:14:11+00:00",
"started_at": "2022-09-26T08:14:11+00:00",
"ended_at": "2022-09-26T08:14:11+00:00",
"retry_of_task_id": null,
"copy_of_task_id": null,
"user_id": 1,
"priority": 50,
"host_name": "dulce",
"storage": null,
"depends_on_task_ids": [
"0a6fd2af-d795-4988-a104-e7c890608bc9"
],
"links": {
"self": "https:\/\/api.cloudconvert.com\/v2\/tasks\/69534510-3eef-46c2-b7a6-71b3f1aed916"
}
}
],
"links": {
"self": "https:\/\/api.cloudconvert.com\/v2\/jobs\/b2e4eb2b-a744-4da2-97cd-776d393532a8"
}
}
}

0 comments on commit b3ba90d

Please sign in to comment.