Skip to content
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

fix: Validate max file size for attachments #149

Closed
wants to merge 5 commits into from
Closed
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
6 changes: 6 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 13.2.4-beta02

---

- Bug fix: Max size on attachments set in questionaire-extention is used to validate the file in stead of hard coded value of 4MB

## 13.2.2

---
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@helsenorge/refero",
"version": "13.2.3",
"version": "13.2.4-beta02",
"engines": {
"node": "^18.0.0",
"npm": ">=9.0.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { QuestionnaireItem } from '../../../../types/fhir';
import { getMaxSizeExtensionValue } from '../../../../util/extension';

describe('Attachment form component', () => {
const questionExtention: QuestionnaireItem = {
linkId: '4c71df6e-d743-46ba-d81f-f62777ffddb4',
type: 'attachment',
text: '5mb',
extension: [
{
url: 'http://hl7.org/fhir/StructureDefinition/maxSize',
valueDecimal: 5,
},
],
};
it('Should give back right value from extention in MB', () => {
const test = getMaxSizeExtensionValue(questionExtention);
expect(test).toBe(5);
});
const question: QuestionnaireItem = {
linkId: '4c71df6e-d743-46ba-d81f-f62777ffddb4',
type: 'attachment',
Copy link
Collaborator

@AG-83 AG-83 Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Det er ikke veldig viktig men man kan bruke
import itemType from '../constants/itemType';
type: itemType.ATTATCHMENT

text: 'No extention',
};
it('Should fail to undefined without extention', () => {
const test = getMaxSizeExtensionValue(question);
expect(test).toBe(undefined);
});
});
11 changes: 8 additions & 3 deletions src/components/formcomponents/attachment/attachmenthtml.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { sizeIsValid, mimeTypeIsValid } from '@helsenorge/file-upload/components
import Validation, { ValidationProps } from '@helsenorge/form/components/form/validation';

import constants, { VALID_FILE_TYPES } from '../../../constants';
import { getValidationTextExtension } from '../../../util/extension';
import { getMaxSizeExtensionValue, getValidationTextExtension } from '../../../util/extension';
import { Resources } from '../../../util/resources';

interface Props {
Expand Down Expand Up @@ -65,7 +65,12 @@ const attachmentHtml: React.SFC<Props & ValidationProps> = ({
children,
...other
}) => {
const maxFilesize = attachmentMaxFileSize ? attachmentMaxFileSize : constants.MAX_FILE_SIZE;
const maxValueFromQuestionaire = getMaxSizeExtensionValue(item);
const maxFilesize = maxValueFromQuestionaire
? maxValueFromQuestionaire * 1024 * 1024
: attachmentMaxFileSize
? attachmentMaxFileSize
: constants.MAX_FILE_SIZE;
const validFileTypes = attachmentValidTypes ? attachmentValidTypes : VALID_FILE_TYPES;
const deleteText = resources ? resources.deleteAttachmentText : undefined;

Expand Down Expand Up @@ -118,7 +123,7 @@ function getErrorMessage(
if (!mimeTypeIsValid(file, validFileTypes)) {
return resources.validationFileType;
} else if (!sizeIsValid(file, maxFileSize)) {
return resources.validationFileMax;
return resources.validationFileMax.replace('25', (maxFileSize / 1024 / 1024).toString());
Copy link
Collaborator

@AG-83 AG-83 Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Er ikke helt sikker at det er lurt at gjøre det hit. Er det ikke HN-Skjema sender i riktig format til refero. Kanskje det trenger ikke gjøre noe endring i feilmeldingen

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vi har denne i HN-Skjema
skjemaResources.validationFileMax = skjemaResources.validationFileMax.replace(
'{0}',
DEFAULT_MAX_ATTACHMENT_FILE_SIZE_HN_SKJEMA_MB.toString()
);

Copy link
Collaborator

@einett2039121 einett2039121 Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enig, da tror jeg har forstått det riktitg, Slik jeg forsøker å løse nå, så bruker jeg bare
den replace opplegget i HN-Skjema. Refero bare returnerer tall tilbake ,( finne
riktig tall ( storelse) ved å sjekke linkid på attachment mot link id på item er min tanke )

}
}

Expand Down
1 change: 1 addition & 0 deletions src/constants/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default {
MARKDOWN_URL: 'http://hl7.org/fhir/StructureDefinition/rendering-markdown',
QUESTIONNAIRE_HIDDEN: 'http://hl7.org/fhir/StructureDefinition/questionnaire-hidden',
ORDINAL_VALUE: 'http://hl7.org/fhir/StructureDefinition/ordinalValue',
MAX_SIZE_URL: 'http://hl7.org/fhir/StructureDefinition/maxSize',

VALIDATIONTEXT_URL: 'http://ehelse.no/fhir/StructureDefinition/validationtext',
REPEATSTEXT_URL: 'http://ehelse.no/fhir/StructureDefinition/repeatstext',
Expand Down
11 changes: 11 additions & 0 deletions src/util/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ export function getQuestionnaireUnitExtensionValue(item: QuestionnaireItem): Cod
return extension.valueCoding;
}

export function getMaxSizeExtensionValue(item: QuestionnaireItem): number | undefined {
const maxValue = getExtension(ExtensionConstants.MAX_SIZE_URL, item);
if (maxValue && maxValue.valueDecimal !== null && maxValue.valueDecimal !== undefined) {
return Number(maxValue.valueDecimal);
}
if (maxValue && maxValue.valueInteger !== null && maxValue.valueInteger !== undefined) {
return Number(maxValue.valueInteger);
}
return undefined;
}

export function getMaxValueExtensionValue(item: QuestionnaireItem): number | undefined {
const maxValue = getExtension(ExtensionConstants.MAX_VALUE_URL, item);
if (maxValue && maxValue.valueDecimal !== null && maxValue.valueDecimal !== undefined) {
Expand Down