From f0beb283894c0844a2d9becb46ebf4a8dc0c05c8 Mon Sep 17 00:00:00 2001 From: Joel Griffith Date: Tue, 12 Feb 2019 13:57:47 -0800 Subject: [PATCH 1/6] Using content-disposition for non-US font titles in reporting --- package.json | 3 +- ...ent_payload.js => get_document_payload.ts} | 34 ++++++++++--------- yarn.lock | 7 ++++ 3 files changed, 27 insertions(+), 17 deletions(-) rename x-pack/plugins/reporting/server/routes/lib/{get_document_payload.js => get_document_payload.ts} (63%) diff --git a/package.json b/package.json index a9f486a3e48be..e5135146f60bc 100644 --- a/package.json +++ b/package.json @@ -132,6 +132,7 @@ "color": "1.0.3", "commander": "2.8.1", "compare-versions": "3.1.0", + "content-disposition": "^0.5.3", "css-loader": "1.0.0", "custom-event-polyfill": "^0.3.0", "d3": "3.5.6", @@ -274,7 +275,6 @@ "@types/eslint": "^4.16.2", "@types/execa": "^0.9.0", "@types/fetch-mock": "7.2.1", - "@types/json5": "^0.0.30", "@types/getopts": "^2.0.0", "@types/glob": "^5.0.35", "@types/globby": "^8.0.0", @@ -287,6 +287,7 @@ "@types/joi": "^13.4.2", "@types/jquery": "^3.3.6", "@types/js-yaml": "^3.11.1", + "@types/json5": "^0.0.30", "@types/listr": "^0.13.0", "@types/lodash": "^3.10.1", "@types/minimatch": "^2.0.29", diff --git a/x-pack/plugins/reporting/server/routes/lib/get_document_payload.js b/x-pack/plugins/reporting/server/routes/lib/get_document_payload.ts similarity index 63% rename from x-pack/plugins/reporting/server/routes/lib/get_document_payload.js rename to x-pack/plugins/reporting/server/routes/lib/get_document_payload.ts index 87f8254a343b9..c29404e9b5a23 100644 --- a/x-pack/plugins/reporting/server/routes/lib/get_document_payload.js +++ b/x-pack/plugins/reporting/server/routes/lib/get_document_payload.ts @@ -4,12 +4,15 @@ * you may not use this file except in compliance with the Elastic License. */ -import { oncePerServer } from '../../lib/once_per_server'; +// @ts-ignore +import contentDisposition from 'content-disposition'; +// @ts-ignore +import { oncePerServer } from './once_per_server'; -function getDocumentPayloadFn(server) { +function getDocumentPayloadFn(server: any) { const exportTypesRegistry = server.plugins.reporting.exportTypesRegistry; - function encodeContent(content, exportType) { + function encodeContent(content: string, exportType: any) { switch (exportType.jobContentEncoding) { case 'base64': return Buffer.from(content, 'base64'); @@ -18,42 +21,42 @@ function getDocumentPayloadFn(server) { } } - function getCompleted(output, jobType, title) { - const exportType = exportTypesRegistry.get(item => item.jobType === jobType); + function getCompleted(output: any, jobType: string, title: any) { + const exportType = exportTypesRegistry.get((item: any) => item.jobType === jobType); return { statusCode: 200, content: encodeContent(output.content, exportType), contentType: output.content_type, headers: { - 'Content-Disposition': `inline; filename="${title || 'report'}.${exportType.jobContentExtension}"` - } + 'Content-Disposition': contentDisposition(title || 'report', { type: 'inline' }), + }, }; } - function getFailure(output) { + function getFailure(output: any) { return { statusCode: 500, content: { message: 'Reporting generation failed', - reason: output.content + reason: output.content, }, - contentType: 'application/json' + contentType: 'application/json', }; } - function getIncomplete(status) { + function getIncomplete(status: any) { return { statusCode: 503, content: status, contentType: 'application/json', headers: { - 'retry-after': 30 - } + 'retry-after': 30, + }, }; } - return function getDocumentPayload(doc) { - const { status, output, jobtype: jobType, payload: { title } = {} } = doc._source; + return function getDocumentPayload(doc: any) { + const { status, output, jobtype: jobType, payload: { title } = { title: '' } } = doc._source; if (status === 'completed') { return getCompleted(output, jobType, title); @@ -69,4 +72,3 @@ function getDocumentPayloadFn(server) { } export const getDocumentPayloadFactory = oncePerServer(getDocumentPayloadFn); - diff --git a/yarn.lock b/yarn.lock index 6e06c95e0da85..5e5edec6ed804 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7044,6 +7044,13 @@ content-disposition@0.5.2: resolved "https://registry.yarnpkg.com/content-disposition/-/content-disposition-0.5.2.tgz#0cf68bb9ddf5f2be7961c3a85178cb85dba78cb4" integrity sha1-DPaLud318r55YcOoUXjLhdunjLQ= +content-disposition@^0.5.3: + version "0.5.3" + resolved "https://registry.yarnpkg.com/content-disposition/-/content-disposition-0.5.3.tgz#e130caf7e7279087c5616c2007d0485698984fbd" + integrity sha512-ExO0774ikEObIAEV9kDo50o+79VCUdEB6n6lzKgGwupcVeRlhrj3qGAfwq8G6uBJjkqLrhT0qEYFcWng8z1z0g== + dependencies: + safe-buffer "5.1.2" + content-type-parser@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/content-type-parser/-/content-type-parser-1.0.2.tgz#caabe80623e63638b2502fd4c7f12ff4ce2352e7" From 8b44cb658c79ab51741b5e07f31dbc6a1c9b720e Mon Sep 17 00:00:00 2001 From: Joel Griffith Date: Wed, 13 Feb 2019 10:57:40 -0800 Subject: [PATCH 2/6] Fixing test failures (actual regression) --- .../reporting/server/routes/lib/get_document_payload.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/reporting/server/routes/lib/get_document_payload.ts b/x-pack/plugins/reporting/server/routes/lib/get_document_payload.ts index c29404e9b5a23..402ce71a70740 100644 --- a/x-pack/plugins/reporting/server/routes/lib/get_document_payload.ts +++ b/x-pack/plugins/reporting/server/routes/lib/get_document_payload.ts @@ -23,12 +23,14 @@ function getDocumentPayloadFn(server: any) { function getCompleted(output: any, jobType: string, title: any) { const exportType = exportTypesRegistry.get((item: any) => item.jobType === jobType); + const filename = `${title || 'report'}.${exportType.jobContentExtension}`; + return { statusCode: 200, content: encodeContent(output.content, exportType), contentType: output.content_type, headers: { - 'Content-Disposition': contentDisposition(title || 'report', { type: 'inline' }), + 'Content-Disposition': contentDisposition(filename, { type: 'inline' }), }, }; } From d90ed8aa8890eb7d82604eef2b899b286f50827a Mon Sep 17 00:00:00 2001 From: Joel Griffith Date: Thu, 14 Feb 2019 12:46:04 -0800 Subject: [PATCH 3/6] Fixing lib import --- .../plugins/reporting/server/routes/lib/get_document_payload.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/reporting/server/routes/lib/get_document_payload.ts b/x-pack/plugins/reporting/server/routes/lib/get_document_payload.ts index 402ce71a70740..8b27048115990 100644 --- a/x-pack/plugins/reporting/server/routes/lib/get_document_payload.ts +++ b/x-pack/plugins/reporting/server/routes/lib/get_document_payload.ts @@ -7,7 +7,7 @@ // @ts-ignore import contentDisposition from 'content-disposition'; // @ts-ignore -import { oncePerServer } from './once_per_server'; +import { oncePerServer } from '../../lib/once_per_server'; function getDocumentPayloadFn(server: any) { const exportTypesRegistry = server.plugins.reporting.exportTypesRegistry; From 3dd9f77dcd1904e0804b19855605e0fb2e7ed57d Mon Sep 17 00:00:00 2001 From: Joel Griffith Date: Thu, 14 Feb 2019 13:57:37 -0800 Subject: [PATCH 4/6] Moving content-disposition over to x-pack package.json --- package.json | 1 - x-pack/package.json | 1 + yarn.lock | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index e5135146f60bc..e5c1c4b778416 100644 --- a/package.json +++ b/package.json @@ -132,7 +132,6 @@ "color": "1.0.3", "commander": "2.8.1", "compare-versions": "3.1.0", - "content-disposition": "^0.5.3", "css-loader": "1.0.0", "custom-event-polyfill": "^0.3.0", "d3": "3.5.6", diff --git a/x-pack/package.json b/x-pack/package.json index 57056b40bb79e..f8952da1fdcea 100644 --- a/x-pack/package.json +++ b/x-pack/package.json @@ -81,6 +81,7 @@ "chance": "1.0.10", "checksum": "0.1.1", "commander": "2.12.2", + "content-disposition": "0.5.3", "copy-webpack-plugin": "^4.5.2", "del": "^3.0.0", "dotenv": "2.0.0", diff --git a/yarn.lock b/yarn.lock index 5e5edec6ed804..96681255f13ac 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7044,7 +7044,7 @@ content-disposition@0.5.2: resolved "https://registry.yarnpkg.com/content-disposition/-/content-disposition-0.5.2.tgz#0cf68bb9ddf5f2be7961c3a85178cb85dba78cb4" integrity sha1-DPaLud318r55YcOoUXjLhdunjLQ= -content-disposition@^0.5.3: +content-disposition@0.5.3: version "0.5.3" resolved "https://registry.yarnpkg.com/content-disposition/-/content-disposition-0.5.3.tgz#e130caf7e7279087c5616c2007d0485698984fbd" integrity sha512-ExO0774ikEObIAEV9kDo50o+79VCUdEB6n6lzKgGwupcVeRlhrj3qGAfwq8G6uBJjkqLrhT0qEYFcWng8z1z0g== From 05007e01641f6085dfeeeb0c4f8d61e8b89c054d Mon Sep 17 00:00:00 2001 From: Joel Griffith Date: Thu, 14 Feb 2019 15:14:29 -0800 Subject: [PATCH 5/6] deps not dev deps... --- x-pack/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/package.json b/x-pack/package.json index f8952da1fdcea..64b54e2c1fa6f 100644 --- a/x-pack/package.json +++ b/x-pack/package.json @@ -81,7 +81,6 @@ "chance": "1.0.10", "checksum": "0.1.1", "commander": "2.12.2", - "content-disposition": "0.5.3", "copy-webpack-plugin": "^4.5.2", "del": "^3.0.0", "dotenv": "2.0.0", @@ -175,6 +174,7 @@ "classnames": "2.2.5", "concat-stream": "1.5.1", "constate": "^0.9.0", + "content-disposition": "0.5.3", "copy-to-clipboard": "^3.0.8", "cronstrue": "^1.51.0", "d3": "3.5.6", From bcc49ee920e8c06dad9038ffa7c27ab59780a24b Mon Sep 17 00:00:00 2001 From: Joel Griffith Date: Fri, 15 Feb 2019 14:59:50 -0800 Subject: [PATCH 6/6] Small util for generating report name --- .../reporting/server/routes/lib/get_document_payload.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/reporting/server/routes/lib/get_document_payload.ts b/x-pack/plugins/reporting/server/routes/lib/get_document_payload.ts index 8b27048115990..f99f0fb0ce434 100644 --- a/x-pack/plugins/reporting/server/routes/lib/get_document_payload.ts +++ b/x-pack/plugins/reporting/server/routes/lib/get_document_payload.ts @@ -9,6 +9,11 @@ import contentDisposition from 'content-disposition'; // @ts-ignore import { oncePerServer } from '../../lib/once_per_server'; +const DEFAULT_TITLE = 'report'; + +const getTitle = (exportType: any, title?: string): string => + `${title || DEFAULT_TITLE}.${exportType.jobContentExtension}`; + function getDocumentPayloadFn(server: any) { const exportTypesRegistry = server.plugins.reporting.exportTypesRegistry; @@ -23,7 +28,7 @@ function getDocumentPayloadFn(server: any) { function getCompleted(output: any, jobType: string, title: any) { const exportType = exportTypesRegistry.get((item: any) => item.jobType === jobType); - const filename = `${title || 'report'}.${exportType.jobContentExtension}`; + const filename = getTitle(exportType, title); return { statusCode: 200,