Skip to content

Commit c2eff7f

Browse files
Stop user from uploading invalid file types + add logging
1 parent fe92abb commit c2eff7f

File tree

5 files changed

+39
-12
lines changed

5 files changed

+39
-12
lines changed

frontend/src/components/attachments/upload-button.tsx

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const MAX_SIZE_BYTES_TOTAL = MAX_SIZE_MIB_TOTAL * BYTES_PER_MIB;
2020

2121
const FORMATTER = new Intl.NumberFormat('nb-NO');
2222

23+
const VALID_FILE_TYPES = ['image/png', 'image/jpeg', 'image/jpg', 'application/pdf'];
24+
2325
interface Props {
2426
caseId: string;
2527
inputId: string;
@@ -57,28 +59,49 @@ export const UploadButton = ({ inputId, setLoading, isLoading, addError, caseId,
5759
return;
5860
}
5961

62+
const uploadedSize = attachments.reduce((total, attachment) => total + attachment.sizeInBytes, 0);
63+
const includedFiles: File[] = [];
64+
const exclusionReasons: string[] = [];
6065
let pickedSize = 0;
6166

6267
for (const file of files) {
63-
pickedSize += file.size;
68+
if (!VALID_FILE_TYPES.includes(file.type)) {
69+
addError(`${file.name}: ${error_messages[ErrorMessageKeys.FILE_COULD_NOT_BE_CONVERTED]}`);
70+
exclusionReasons.push(`Invalid type: ${file.type}`);
71+
continue;
72+
}
6473

6574
if (file.size > MAX_SIZE_BYTES_SINGLE) {
6675
addError(`${file.name}: ${error_messages[ErrorMessageKeys.TOO_LARGE]}`);
67-
return;
76+
exclusionReasons.push(`File too large: ${FORMATTER.format(file.size / BYTES_PER_KB)} KiB`);
77+
continue;
6878
}
79+
80+
const overhead = (includedFiles.length + 1) * OVERHEAD_BYTES;
81+
82+
if (uploadedSize + pickedSize + overhead + file.size > MAX_SIZE_BYTES_TOTAL) {
83+
addError(`${error_messages.could_not_add} ${file.name}. ${error_messages[ErrorMessageKeys.TOTAL_TOO_LARGE]}`);
84+
exclusionReasons.push(
85+
`Total size exceeded. Could not add file of size: ${FORMATTER.format(file.size / BYTES_PER_KB)} KiB. Used: ${FORMATTER.format((uploadedSize + pickedSize + overhead) / BYTES_PER_KB)} KiB.`,
86+
);
87+
continue;
88+
}
89+
90+
pickedSize += file.size;
91+
includedFiles.push(file);
6992
}
7093

71-
const uploadedSize = attachments.reduce((total, attachment) => total + attachment.sizeInBytes, 0);
72-
const overhead = files.length * OVERHEAD_BYTES;
94+
if (exclusionReasons.length > 0) {
95+
appEvent(AppEventEnum.UPLOAD_FILES_ERROR, { exclusions: exclusionReasons });
96+
}
7397

74-
if (uploadedSize + pickedSize + overhead > MAX_SIZE_BYTES_TOTAL) {
75-
addError(error_messages[ErrorMessageKeys.TOTAL_TOO_LARGE]);
98+
if (includedFiles.length === 0) {
7699
return;
77100
}
78101

79102
setLoading(true);
80103

81-
const uploads = Array.from(files).map(async (file) => {
104+
const uploads = includedFiles.map(async (file) => {
82105
try {
83106
await uploadAttachment({ caseId, file }).unwrap();
84107
} catch (err) {
@@ -112,7 +135,7 @@ export const UploadButton = ({ inputId, setLoading, isLoading, addError, caseId,
112135
id={inputId}
113136
type="file"
114137
multiple
115-
accept="image/png, image/jpeg, image/jpg, .pdf"
138+
accept={[...VALID_FILE_TYPES, '.pdf'].join(',')}
116139
ref={fileInput}
117140
onChange={(e) => {
118141
upload(e);

frontend/src/language/en.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ export const en: Translations = {
380380
[ErrorMessageKeys.FILE_COULD_NOT_BE_CONVERTED]:
381381
'You have tried to include an attachment with a format we do not support. Attachments are limited to til PNG, JPEG, and PDF.',
382382
download: 'Failed to download PDF. Please try again later.',
383+
could_not_add: 'Could not add',
383384
skjema: {
384385
title: 'Form is not complete',
385386
fnr_dnr_or_npid: 'You must input a valid national identity number, D number or NPID.',

frontend/src/language/nb.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ export const nb = {
383383
[ErrorMessageKeys.FILE_COULD_NOT_BE_CONVERTED]:
384384
'Du har prøvd å legge til et vedlegg med et format vi ikke støtter. Vedlegg er begrenset til PNG, JPEG, og PDF.',
385385
download: 'Klarte ikke å laste ned PDF. Vennligst prøv igjen senere.',
386+
could_not_add: 'Kunne ikke legge til',
386387
skjema: {
387388
title: 'Feil i skjema',
388389
fnr_dnr_or_npid: 'Du må fylle inn et gyldig fødselsnummer, D-nummer eller NPID.',

frontend/src/language/nn.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ export const nn: Translations = {
2222
[CaseType.ANKE]:
2323
'Dersom du er ein arbeidsgivar må du logge ut og sende anka i posten. Du legg inn fødselsnummeret eller D-nummeret til den arbeidstakaren som vedtaket gjeld for, skriv ut anka og signerer som arbeidsgivar.',
2424
[CaseType.ETTERSENDELSE_KLAGE]:
25-
'Dersom du er ein arbeidsgivar må du logge ut og legge inn fødselsnummeret eller D-nummeret til den arbeidstakaren som ettersendinga gjeld for, skriv ut framsida og signerer som arbeidsgivar.',
25+
'Dersom du er ein arbeidsgivar må du logge ut og leggje inn fødselsnummeret eller D-nummeret til den arbeidstakaren som ettersendinga gjeld for, skriv ut framsida og signerer som arbeidsgivar.',
2626
[CaseType.ETTERSENDELSE_ANKE]:
27-
'Dersom du er ein arbeidsgivar må du logge ut og legge inn fødselsnummeret eller D-nummeret til den arbeidstakaren som ettersendinga gjeld for, skriv ut framsida og signerer som arbeidsgivar.',
27+
'Dersom du er ein arbeidsgivar må du logge ut og leggje inn fødselsnummeret eller D-nummeret til den arbeidstakaren som ettersendinga gjeld for, skriv ut framsida og signerer som arbeidsgivar.',
2828
},
2929
common: {
3030
title_fragment: {
@@ -56,7 +56,7 @@ export const nn: Translations = {
5656
clear_errors: 'Fjern feilmeldingar',
5757
title: 'Vedlegg',
5858
upload_button_text: 'Last opp nytt vedlegg',
59-
description: 'Har du informasjon du ønsker å legge ved, lastar du det opp her.',
59+
description: 'Har du informasjon du ønsker å leggje ved, lastar du det opp her.',
6060
supported_types: [
6161
<span key="1">Filtyper som støttast: </span>,
6262
<span key="2">
@@ -379,8 +379,9 @@ export const nn: Translations = {
379379
[ErrorMessageKeys.EMPTY]: 'Du kan ikkje sende inn ei tom fil.',
380380
[ErrorMessageKeys.VIRUS]: 'Vi mistenker at fila di inneheld eit virus, den kan derfor ikkje sendast inn.',
381381
[ErrorMessageKeys.FILE_COULD_NOT_BE_CONVERTED]:
382-
'Du har prøvd å legge til eit vedlegg med eit format vi ikkje støttar. Vedlegg er avgrensa til PNG, JPEG og PDF.',
382+
'Du har prøvd å leggje til eit vedlegg med eit format vi ikkje støttar. Vedlegg er avgrensa til PNG, JPEG og PDF.',
383383
download: 'Klarte ikkje å laste ned PDF. Ver venleg og prøv på nytt seinare.',
384+
could_not_add: 'Kunne ikkje leggje til',
384385
skjema: {
385386
title: 'Feil i skjema',
386387
fnr_dnr_or_npid: 'Du må fylle inn eit gyldig fødselsnummer, D-nummer eller NPID.',

frontend/src/logging/action.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export enum AppEventEnum {
2323
UPLOAD_FILES_CLICK = 'Click upload attachment',
2424
UPLOAD_FILES_DONE = 'Done uploading files',
2525
UPLOAD_FILES_START = 'Start uploading files',
26+
UPLOAD_FILES_ERROR = 'Upload files error',
2627
USER_LOGIN = 'Login',
2728
MISSING_AUTH = 'Missing authentication',
2829
LOGGED_OUT_MODAL_OPEN = 'Logged out modal open',

0 commit comments

Comments
 (0)