From 650837a649fdfb95b7a1cd0687428a84ef975980 Mon Sep 17 00:00:00 2001 From: Samriddhi-98 Date: Fri, 4 Sep 2026 15:32:45 +0530 Subject: [PATCH] BAH-5053 | Fix. maxDocumentSize limit overflow on the upload document action --- .../controller/VisitDocumentController.java | 4 ++- .../VisitDocumentControllerTest.java | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/VisitDocumentController.java b/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/VisitDocumentController.java index 885006731..670b0e3a5 100644 --- a/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/VisitDocumentController.java +++ b/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/VisitDocumentController.java @@ -77,7 +77,9 @@ public ResponseEntity> saveDocument(@RequestBody Documen if (!StringUtils.isEmpty(maxDocumentSize)) { Long maxDocumentSizeMb = Long.parseLong(maxDocumentSize); Long maxDocumentSizeBytes = maxDocumentSizeMb * 1024 * 1024; - if (document.getContent().length() > maxDocumentSizeBytes) { + // getContent() is base64, which is 4/3 the size of the file it encodes. + // Convert back to decoded bytes so the limit means what it says. + if (document.getContent().length() * 3L / 4 > maxDocumentSizeBytes) { logger.warn("Uploaded document size is greater than the maximum size " + maxDocumentSizeMb + "MB"); savedDocument.put("maxDocumentSizeMB", maxDocumentSizeMb); return new ResponseEntity<>(savedDocument, HttpStatus.PAYLOAD_TOO_LARGE); diff --git a/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/controller/VisitDocumentControllerTest.java b/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/controller/VisitDocumentControllerTest.java index bc26f634b..84d9461c8 100644 --- a/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/controller/VisitDocumentControllerTest.java +++ b/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/controller/VisitDocumentControllerTest.java @@ -286,4 +286,29 @@ public void shouldSaveDocumentIfDocumentSizeIsLessThanSizeLimit() throws Excepti verify(patientDocumentService, times(1)).saveDocument(1, "consultation", base64Content, "jpeg", document.getFileType(), document.getFileName()); } + + @Test + public void shouldSaveDocumentWhoseDecodedSizeIsUnderLimitEvenIfBase64PayloadExceedsIt() throws Exception { + PowerMockito.mockStatic(Context.class); + when(Context.getPatientService()).thenReturn(patientService); + + Patient patient = new Patient(); + patient.setId(1); + patient.setUuid("patient-uuid"); + when(patientService.getPatientByUuid("patient-uuid")).thenReturn(patient); + + when(administrationService.getGlobalProperty("bahmni.encounterType.default")).thenReturn("consultation"); + + Document document = new Document("abcd", "jpeg", null, "patient-uuid", "image", "file-name"); + + byte[] content = new byte[6 * 1024 * 1024]; + String base64Content = Base64.getEncoder().encodeToString(content); + document.setContent(base64Content); + + ResponseEntity> responseEntity = visitDocumentController.saveDocument(document); + + Assert.assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + + verify(patientDocumentService, times(1)).saveDocument(1, "consultation", base64Content, "jpeg", document.getFileType(), document.getFileName()); + } }