From 6c224a6800b935e81cebf2ff12799b080d5267fd Mon Sep 17 00:00:00 2001 From: Vinicius Goulart Date: Thu, 19 Dec 2024 13:52:32 +0100 Subject: [PATCH] feat: Inject content hash into document preview URL --- .../test/spec/Playground.spec.js | 3 ++ .../components/form-fields/DocumentPreview.js | 30 ++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/packages/form-js-playground/test/spec/Playground.spec.js b/packages/form-js-playground/test/spec/Playground.spec.js index 691af3dab..30550ff8d 100644 --- a/packages/form-js-playground/test/spec/Playground.spec.js +++ b/packages/form-js-playground/test/spec/Playground.spec.js @@ -88,6 +88,9 @@ describe('playground', function () { metadata: { fileName: 'My document.pdf', contentType: 'application/pdf', + customProperties: { + contentHash: '1234567890', + }, }, }, { diff --git a/packages/form-js-viewer/src/render/components/form-fields/DocumentPreview.js b/packages/form-js-viewer/src/render/components/form-fields/DocumentPreview.js index 4f345a2dd..90603fa1b 100644 --- a/packages/form-js-viewer/src/render/components/form-fields/DocumentPreview.js +++ b/packages/form-js-viewer/src/render/components/form-fields/DocumentPreview.js @@ -15,6 +15,8 @@ const type = 'documentPreview'; * @property {Object} metadata * @property {string|undefined} [metadata.contentType] * @property {string} metadata.fileName + * @property {Object} [metadata.customProperties] + * @property {string|undefined} [metadata.customProperties.contentHash] * * @typedef Field * @property {string} id @@ -170,7 +172,11 @@ function DocumentRenderer(props) { const [hasError, setHasError] = useState(false); const ref = useRef(null); const isInViewport = useInViewport(ref); - const fullUrl = endpoint.replace(DOCUMENT_ID_PLACEHOLDER, documentMetadata.documentId); + const fullUrl = buildUrl({ + baseUrl: endpoint, + documentId: documentMetadata.documentId, + customProperties: metadata.customProperties, + }); const singleDocumentContainerClassName = `fjs-${type}-single-document-container`; const errorMessageId = `${domId}-error-message`; const errorMessage = 'Unable to download document'; @@ -303,3 +309,25 @@ function useInViewport(ref) { return isInViewport; } + +/** + * This solution should be a temporary fix, we should try to remove it via: https://github.com/bpmn-io/form-js/issues/1341 + * + * @param {Object} options + * @param {string} options.baseUrl + * @param {string} options.documentId + * @param {Object} [options.customProperties] + * @param {string|undefined} [options.customProperties.contentHash] + * + * @returns {string} + */ +function buildUrl(options) { + const { baseUrl, documentId, customProperties } = options; + const finalUrl = new URL(baseUrl.replace(DOCUMENT_ID_PLACEHOLDER, documentId)); + + if (customProperties !== undefined && customProperties.contentHash !== undefined) { + finalUrl.searchParams.set('contentHash', customProperties.contentHash); + } + + return decodeURI(finalUrl.toString()); +}