Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getFileExtension } from '../../../../../../lib/utils/getFileExtension';
import { forAttachmentDownload, registerDownloadForUid } from '../../../../../hooks/useDownloadFromServiceWorker';
import MessageCollapsible from '../../../MessageCollapsible';
import AttachmentSize from '../structure/AttachmentSize';
import { useOpenEncryptedPdf } from './hooks/useOpenEncryptedPdf';

const openDocumentViewer = window.RocketChatDesktop?.openDocumentViewer;

Expand All @@ -31,24 +32,31 @@ const GenericFileAttachment = ({
const getURL = useMediaUrl();
const uid = useId();
const { t } = useTranslation();
const openEncryptedPdf = useOpenEncryptedPdf();

const handleTitleClick = (event: UIEvent): void => {
const handleTitleClick = async (event: UIEvent): Promise<void> => {
if (!link) {
return;
}

if (openDocumentViewer && format === 'PDF') {
const isEncrypted = link.includes('/file-decrypt/');

if (format === 'PDF' && openDocumentViewer) {
event.preventDefault();

if (isEncrypted) {
openEncryptedPdf(link, title, size, format, openDocumentViewer);
return;
}

const url = new URL(getURL(link), window.location.origin);
url.searchParams.set('contentDisposition', 'inline');
openDocumentViewer(url.toString(), format, '');
return;
}

if (link.includes('/file-decrypt/')) {
if (isEncrypted) {
event.preventDefault();

registerDownloadForUid(uid, t, title);
forAttachmentDownload(uid, link);
}
Expand Down
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);
Comment thread
nazabucciarelli marked this conversation as resolved.
}
}
};

return openEncryptedPdf;
};
1 change: 1 addition & 0 deletions packages/desktop-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ export interface IRocketChatDesktop {
setUserToken: (token: string, userId: string) => void;
openDocumentViewer: (url: string, format: string, options: any) => void;
reloadServer: () => void;
getE2ePdfPreviewSizeLimit: () => number;
openInBrowser: (url: string) => void;
}
Loading