Skip to content

Commit bbe590f

Browse files
PR Review fixes - I
1 parent 4c34db9 commit bbe590f

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

cds-feature-attachments/src/main/java/com/sap/cds/feature/attachments/handler/applicationservice/helper/mimeTypeValidation/AttachmentDataExtractor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ private static String validateAndNormalize(String fileName) {
7070
throw new ServiceException(ErrorStatuses.BAD_REQUEST, "Filename must not be blank");
7171
}
7272

73-
int lastDotIndex = trimmedFileName.lastIndexOf('.');
74-
if (lastDotIndex == -1 || lastDotIndex == trimmedFileName.length() - 1) {
73+
if (trimmedFileName.endsWith(".")) {
7574
throw new ServiceException(ErrorStatuses.BAD_REQUEST, "Invalid filename format: " + fileName);
7675
}
76+
7777
return trimmedFileName;
7878
}
7979

cds-feature-attachments/src/main/java/com/sap/cds/feature/attachments/handler/applicationservice/helper/mimeTypeValidation/MediaTypeService.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,24 @@ public static boolean isMimeTypeAllowed(
5454
return false;
5555
}
5656

57-
if (acceptableMediaTypes == null
58-
|| acceptableMediaTypes.isEmpty()
59-
|| acceptableMediaTypes.contains("*/*")) return true;
57+
if (acceptableMediaTypes == null || acceptableMediaTypes.isEmpty()) {
58+
return true;
59+
}
6060

6161
String baseMimeType = mimeType.trim().toLowerCase();
6262
Collection<String> normalizedTypes =
6363
acceptableMediaTypes.stream().map(type -> type.trim().toLowerCase()).toList();
6464

65+
if (normalizedTypes.contains("*/*")) {
66+
return true;
67+
}
68+
6569
return normalizedTypes.stream()
6670
.anyMatch(
67-
type -> {
68-
return type.endsWith("/*")
69-
? baseMimeType.startsWith(type.substring(0, type.length() - 1))
70-
: baseMimeType.equals(type);
71-
});
71+
type ->
72+
type.endsWith("/*")
73+
? baseMimeType.startsWith(type.substring(0, type.length() - 1))
74+
: baseMimeType.equals(type));
7275
}
7376

7477
private static String fallbackToDefaultMimeType(String fileName) {

integration-tests/srv/src/test/java/com/sap/cds/feature/attachments/integrationtests/nondraftservice/MediaValidatedAttachmentsNonDraftTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void shouldRejectAttachment_whenFileHasNoExtension() throws Exception {
108108
requestHelper.executePostWithMatcher(
109109
createUrl(rootId, MEDIA_VALIDATED_ATTACHMENTS),
110110
attachmentMetadata,
111-
status().isBadRequest());
111+
status().isUnsupportedMediaType());
112112
}
113113

114114
@Test

0 commit comments

Comments
 (0)