From 9e99b0358f28ce99ada8b18eb520110dc38468a4 Mon Sep 17 00:00:00 2001 From: PiTrem Date: Mon, 13 May 2024 23:14:37 +0200 Subject: [PATCH] fix: viewing of pdf in analysis preview also src pdfworker locally --- app/packs/src/components/common/ImageModal.js | 4 ++-- config/initializers/content_security_policy.rb | 16 +++++++++++++++- package_postinstall.sh | 7 ++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/app/packs/src/components/common/ImageModal.js b/app/packs/src/components/common/ImageModal.js index d8f4a123ee..0b60fa64f6 100644 --- a/app/packs/src/components/common/ImageModal.js +++ b/app/packs/src/components/common/ImageModal.js @@ -6,7 +6,7 @@ import AttachmentFetcher from 'src/fetchers/AttachmentFetcher'; import { stopEvent } from 'src/utilities/DomHelper'; import { Document, Page, pdfjs } from 'react-pdf'; -pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`; +pdfjs.GlobalWorkerOptions.workerSrc = '/pdf.worker.min.js'; export default class ImageModal extends Component { constructor(props) { @@ -35,7 +35,7 @@ export default class ImageModal extends Component { } } - shouldComponentUpdate(nextState) { + shouldComponentUpdate(nextProps, nextState) { if ( this.state.numOfPages === nextState.numOfPages && this.state.numOfPages !== 0 diff --git a/config/initializers/content_security_policy.rb b/config/initializers/content_security_policy.rb index 1815653ba4..fe30a39681 100644 --- a/config/initializers/content_security_policy.rb +++ b/config/initializers/content_security_policy.rb @@ -14,7 +14,21 @@ # # Specify URI for violation reports # # policy.report_uri "/csp-violation-report-endpoint" - policy.connect_src :self, :https, "http://localhost:3035", "ws://localhost:3035" if Rails.env.development? + + # connect_src for log and webpack-dev-serverwebsockets in development + if Rails.env.development? + url = URI.parse(ENV['PUBLIC_URL'] || '') + scheme = url.scheme || 'http' + host = url.host + port = url.port + + src = [:self, :https, 'http://localhost:3035', 'ws://localhost:3035'] + src += ["ws://#{host}:3035", "#{scheme}://#{host}:3035"] if host.present? + src += ["#{scheme}://#{host}:#{url.port}"] if host.present? && port.present? + puts "connect_src: #{src}" + + policy.connect_src *src + end end # If you are using UJS then enable automatic nonce generation diff --git a/package_postinstall.sh b/package_postinstall.sh index 75227a16ef..ca0fc99847 100755 --- a/package_postinstall.sh +++ b/package_postinstall.sh @@ -33,6 +33,11 @@ rm -f ./public/svgedit && ln -s "$node_modules_folder"/svgedit/dist/editor ./pub yellow "Finished adding symbolic link to svg editor in public folder" +# copy pdfjs worker to public folder +node_modules_folder_pdfjs="$(node -e 'const p = require.resolve("pdfjs-dist/build/pdf.worker"); console.log(p.slice(0, p.indexOf("pdfjs-dist/build/pdf.worker")))')" +rm -f ./public/pdf.worker.min.js && ln -s "$node_modules_folder_pdfjs"pdfjs-dist/build/pdf.worker.min.js ./public/pdf.worker.min.js +yellow "Finished adding symbolic link to pdf worker in public folder" + # d3js source files src_d3=( "@complat/react-spectra-editor/dist/components/common/draw.js" @@ -54,4 +59,4 @@ for src_file in "${src_d3[@]}"; do echo -e "\n});" >> "$src" fi yellow "Done rewriting import for d3.js in $src" -done \ No newline at end of file +done