From b3ba90d5c38a43a837a3ca242a30cb7a480b068e Mon Sep 17 00:00:00 2001 From: Josias Montag Date: Mon, 26 Sep 2022 10:28:02 +0200 Subject: [PATCH] feat: add `jobs.getExportUrls` utility --- README.md | 5 +- lib/JobsResource.ts | 8 +- tests/integration/JobsResourceTest.js | 6 +- tests/unit/JobsResourceTest.js | 31 +++++++ tests/unit/responses/job_finished.json | 118 +++++++++++++++++++++++++ 5 files changed, 158 insertions(+), 10 deletions(-) create mode 100644 tests/unit/responses/job_finished.json diff --git a/README.md b/README.md index 3a95102..ead1566 100644 --- a/README.md +++ b/README.md @@ -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); diff --git a/lib/JobsResource.ts b/lib/JobsResource.ts index f5da9d4..d8b1d6b 100644 --- a/lib/JobsResource.ts +++ b/lib/JobsResource.ts @@ -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 ?? []); } } diff --git a/tests/integration/JobsResourceTest.js b/tests/integration/JobsResourceTest.js index 636cb30..7dcd87f 100644 --- a/tests/integration/JobsResourceTest.js +++ b/tests/integration/JobsResourceTest.js @@ -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'); diff --git a/tests/unit/JobsResourceTest.js b/tests/unit/JobsResourceTest.js index 68ab6d9..e1b37bd 100644 --- a/tests/unit/JobsResourceTest.js +++ b/tests/unit/JobsResourceTest.js @@ -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/') + ); + }); + }); }); diff --git a/tests/unit/responses/job_finished.json b/tests/unit/responses/job_finished.json new file mode 100644 index 0000000..1e7201f --- /dev/null +++ b/tests/unit/responses/job_finished.json @@ -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" + } + } +} \ No newline at end of file