Skip to content

Commit

Permalink
fix: Validate max file size for attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
ahbakken committed Jan 10, 2024
1 parent 3fce9c4 commit eeb63b7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
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());
}
}

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

0 comments on commit eeb63b7

Please sign in to comment.