From bab832593cec203bf1c4d026fff01eb92cc9c4c3 Mon Sep 17 00:00:00 2001 From: Joel Griffith Date: Thu, 21 Feb 2019 10:18:25 -0800 Subject: [PATCH] Using `content-disposition` pkg for non-US font titles in reporting (#30895) (#31713) * Using content-disposition for non-US font titles in reporting --- x-pack/package.json | 1 + .../server/routes/lib/get_document_payload.ts | 81 +++++++++++++++++++ yarn.lock | 7 ++ 3 files changed, 89 insertions(+) create mode 100644 x-pack/plugins/reporting/server/routes/lib/get_document_payload.ts diff --git a/x-pack/package.json b/x-pack/package.json index 4562a7963c4f..0ff96bec730b 100644 --- a/x-pack/package.json +++ b/x-pack/package.json @@ -158,6 +158,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", 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 new file mode 100644 index 000000000000..f99f0fb0ce43 --- /dev/null +++ b/x-pack/plugins/reporting/server/routes/lib/get_document_payload.ts @@ -0,0 +1,81 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +// @ts-ignore +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; + + function encodeContent(content: string, exportType: any) { + switch (exportType.jobContentEncoding) { + case 'base64': + return Buffer.from(content, 'base64'); + default: + return content; + } + } + + function getCompleted(output: any, jobType: string, title: any) { + const exportType = exportTypesRegistry.get((item: any) => item.jobType === jobType); + const filename = getTitle(exportType, title); + + return { + statusCode: 200, + content: encodeContent(output.content, exportType), + contentType: output.content_type, + headers: { + 'Content-Disposition': contentDisposition(filename, { type: 'inline' }), + }, + }; + } + + function getFailure(output: any) { + return { + statusCode: 500, + content: { + message: 'Reporting generation failed', + reason: output.content, + }, + contentType: 'application/json', + }; + } + + function getIncomplete(status: any) { + return { + statusCode: 503, + content: status, + contentType: 'application/json', + headers: { + 'retry-after': 30, + }, + }; + } + + return function getDocumentPayload(doc: any) { + const { status, output, jobtype: jobType, payload: { title } = { title: '' } } = doc._source; + + if (status === 'completed') { + return getCompleted(output, jobType, title); + } + + if (status === 'failed') { + return getFailure(output); + } + + // send a 503 indicating that the report isn't completed yet + return getIncomplete(status); + }; +} + +export const getDocumentPayloadFactory = oncePerServer(getDocumentPayloadFn); diff --git a/yarn.lock b/yarn.lock index 466c75da9d25..7775291c1626 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5973,6 +5973,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"