Skip to content

Commit

Permalink
feat: Show message if imported pdf is encrypted
Browse files Browse the repository at this point in the history
  • Loading branch information
JF-Cozy committed Jul 18, 2024
1 parent d48ddde commit f66c512
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
28 changes: 28 additions & 0 deletions src/components/ModelSteps/Scan/EncryptedPDFModal.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react'

import Button from 'cozy-ui/transpiled/react/Buttons'
import { ConfirmDialog } from 'cozy-ui/transpiled/react/CozyDialogs'
import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n'

const EncryptedPDFModal = ({ onClose }) => {
const { t } = useI18n()

return (
<ConfirmDialog
open
title={t('EncryptedPDFModal.title')}
content={t('EncryptedPDFModal.content')}
actions={
<Button
variant="secondary"
fullWidth
label={t('EncryptedPDFModal.action')}
onClick={onClose}
/>
}
onClose={onClose}
/>
)
}

export default EncryptedPDFModal
24 changes: 19 additions & 5 deletions src/components/ModelSteps/Scan/ScanWrapper.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { PDFDocument } from 'pdf-lib'
import React, { useEffect, useState } from 'react'
import { useParams, useSearchParams } from 'react-router-dom'
import { PaperDefinitionsStepPropTypes } from 'src/PaperDefinitionsPropTypes'
import { useFormData } from 'src/components/Contexts/FormDataProvider'
import { useModal } from 'src/components/Contexts/ModalProvider'
import { useStepperDialog } from 'src/components/Contexts/StepperDialogProvider'
import EncryptedPDFModal from 'src/components/ModelSteps/Scan/EncryptedPDFModal'
import ScanDialog from 'src/components/ModelSteps/Scan/ScanDialog'
import ScanResultDialog from 'src/components/ModelSteps/ScanResult/ScanResultDialog'
import {
Expand All @@ -17,14 +20,21 @@ import {
storeCreatePaperDataBackup
} from 'src/helpers/paperDataBackup'

import { useClient, models } from 'cozy-client'
import { useClient } from 'cozy-client'
import { fetchBlobFileById } from 'cozy-client/dist/models/file'
import { useWebviewIntent } from 'cozy-intent'
import minilog from 'cozy-minilog'
import FilePicker from 'cozy-ui/transpiled/react/FilePicker'

const log = minilog('ScanWrapper')

const { fetchBlobFileById } = models.file
const isFileEncryptedPDF = async file => {
if (!file.type || file.type !== 'application/pdf') return false

const arrayBuffer = await file.arrayBuffer()
const pdf = await PDFDocument.load(arrayBuffer, { ignoreEncryption: true })
return pdf.isEncrypted
}

const ScanWrapper = ({ currentStep, onClose, onBack }) => {
const client = useClient()
Expand All @@ -36,6 +46,7 @@ const ScanWrapper = ({ currentStep, onClose, onBack }) => {
const [currentFile, setCurrentFile] = useState(null)
const [isFilePickerModalOpen, setIsFilePickerModalOpen] = useState(false)
const webviewIntent = useWebviewIntent()
const { pushModal, popModal } = useModal()

const fromFlagshipUpload = searchParams.get('fromFlagshipUpload')
useEffect(() => {
Expand All @@ -45,9 +56,12 @@ const ScanWrapper = ({ currentStep, onClose, onBack }) => {
}
}, [formData, currentStepIndex])

const onChangeFile = (file, { replace = false } = {}) => {
const onChangeFile = async (file, { replace = false } = {}) => {
// TODO : Integrate the values recovered by the OCR
if (file) {
if (await isFileEncryptedPDF(file)) {
return pushModal(<EncryptedPDFModal onClose={popModal} />)
}
if (replace) {
setFormData(prev => ({
...prev,
Expand Down Expand Up @@ -87,7 +101,7 @@ const ScanWrapper = ({ currentStep, onClose, onBack }) => {
name: cozyFileId,
from: 'cozy'
})
onChangeFile(file)
await onChangeFile(file)
}

const onOpenFlagshipScan = async () => {
Expand All @@ -114,7 +128,7 @@ const ScanWrapper = ({ currentStep, onClose, onBack }) => {
name: FLAGSHIP_SCAN_TEMP_FILENAME,
type: 'image/png'
})
onChangeFile(file)
await onChangeFile(file)
} catch (error) {
log.error('Flagship scan error', error)
}
Expand Down
5 changes: 5 additions & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,11 @@
"PagePickerModal": {
"splitFile": "Split file"
},
"EncryptedPDFModal": {
"title": "Incompatible file",
"content": "You have selected an encrypted PDF file. Encrypted items cannot be imported into My Papers.",
"action": "I understand"
},
"PreviewConfirm": {
"title": "Overview of the 2 sides of the document",
"text": "The 2 sides of the document will be displayed in this preview. Don't worry, when the document is transmitted, <b>only the selected side will be shown</b>.",
Expand Down
5 changes: 5 additions & 0 deletions src/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,11 @@
"PagePickerModal": {
"splitFile": "Séparer le document"
},
"EncryptedPDFModal": {
"title": "Fichier incompatible",
"content": "Vous avez sélectionné un fichier PDF chiffré. Les éléments chiffrés ne peuvent pas être importés dans l'application Mes Papiers",
"action": "J'ai compris"
},
"PreviewConfirm": {
"title": "Aperçu des 2 faces du document",
"text": "Les 2 faces du documents seront affichées dans cet aperçu. Pas d’inquiétude, à la transmission, <b>le document comportera uniquement la face sélectionnée</b>.",
Expand Down

0 comments on commit f66c512

Please sign in to comment.