-
Notifications
You must be signed in to change notification settings - Fork 13.6k
chore: adapt code to use of new desktop setting for encrypted pdf preview size limit #40517
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nazabucciarelli
wants to merge
14
commits into
develop
Choose a base branch
from
fix/pdf-viewer-encrypted-room
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+89
−4
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
77b5b46
add encrypted pdf file blob codification
nazabucciarelli 339c708
add changeset
nazabucciarelli ab04cdf
add use of abort controller
nazabucciarelli 78225b7
add fetch failure handling
nazabucciarelli 98015cb
add new setting
nazabucciarelli 4b6851c
use bytes rather than megabytes for setting
nazabucciarelli a082845
address abortController race condition
nazabucciarelli 45fb0fe
update changeset
nazabucciarelli 6375d39
fix grammar in translation
nazabucciarelli 7a51828
extract encrypted pdf opening logic to hook
nazabucciarelli 68f5ae0
remove workspace setting to use desktop setting
nazabucciarelli a7323ab
convert setting value in bytes
nazabucciarelli ca906d3
add missing property signatura to IRocketChatDesktop interface
nazabucciarelli bff5af9
Merge branch 'develop' into fix/pdf-viewer-encrypted-room
nazabucciarelli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
apps/meteor/client/components/message/content/attachments/file/hooks/useOpenEncryptedPdf.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { useMediaUrl } from '@rocket.chat/ui-contexts'; | ||
| import { useId, useEffect, useRef } from 'react'; | ||
| import { useTranslation } from 'react-i18next'; | ||
|
|
||
| import { forAttachmentDownload, registerDownloadForUid } from '../../../../../../hooks/useDownloadFromServiceWorker'; | ||
|
|
||
| export const useOpenEncryptedPdf = () => { | ||
| const getURL = useMediaUrl(); | ||
| const pdfPreviewSizeLimit = window.RocketChatDesktop?.getE2ePdfPreviewSizeLimit?.() ?? 10; | ||
| const pdfPreviewSizeLimitInBytes = pdfPreviewSizeLimit * 1024 * 1024; | ||
| const uid = useId(); | ||
| const { t } = useTranslation(); | ||
|
|
||
| const blobUrlRef = useRef<string | undefined>(undefined); | ||
| const abortControllerRef = useRef<AbortController | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| return () => { | ||
| if (abortControllerRef.current) { | ||
| abortControllerRef.current.abort(); | ||
| } | ||
| if (blobUrlRef.current) { | ||
| URL.revokeObjectURL(blobUrlRef.current); | ||
| blobUrlRef.current = undefined; | ||
| } | ||
| }; | ||
| }, []); | ||
|
|
||
| const openEncryptedPdf = async ( | ||
| link: string, | ||
| title: string | undefined, | ||
| size: number | undefined, | ||
| format: string, | ||
| openDocumentViewer: (url: string, format: string, options: any) => void, | ||
| ) => { | ||
| if (size && size > pdfPreviewSizeLimitInBytes) { | ||
| registerDownloadForUid(uid, t, title); | ||
| forAttachmentDownload(uid, link); | ||
| return; | ||
| } | ||
|
|
||
| if (blobUrlRef.current) { | ||
| URL.revokeObjectURL(blobUrlRef.current); | ||
| blobUrlRef.current = undefined; | ||
| } | ||
|
|
||
| if (abortControllerRef.current) { | ||
| abortControllerRef.current.abort(); | ||
| } | ||
|
|
||
| const abortController = new AbortController(); | ||
| abortControllerRef.current = abortController; | ||
|
|
||
| try { | ||
| const response = await fetch(getURL(link), { | ||
| signal: abortController.signal, | ||
| }); | ||
| if (!response.ok) { | ||
| throw new Error(`Failed to fetch encrypted PDF: ${response.status}`); | ||
| } | ||
| const blob = await response.blob(); | ||
| if (abortController.signal.aborted || abortControllerRef.current !== abortController) { | ||
| return; | ||
| } | ||
| const blobUrl = URL.createObjectURL(blob); | ||
| blobUrlRef.current = blobUrl; | ||
| openDocumentViewer(blobUrl, format, title ?? ''); | ||
| } catch (error: any) { | ||
| if (error.name !== 'AbortError') { | ||
| console.error('Error opening preview of encrypted PDF', error); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| return openEncryptedPdf; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.