|
5 | 5 |
|
6 | 6 | import static java.util.Objects.requireNonNull; |
7 | 7 |
|
| 8 | +import java.io.IOException; |
| 9 | + |
8 | 10 | import com.sap.cds.CdsData; |
9 | 11 | import com.sap.cds.CdsDataProcessor; |
10 | 12 | import com.sap.cds.CdsDataProcessor.Filter; |
|
34 | 36 | import java.util.Map; |
35 | 37 | import java.util.concurrent.atomic.AtomicBoolean; |
36 | 38 | import java.util.concurrent.atomic.AtomicReference; |
37 | | -import java.util.regex.Matcher; |
38 | | -import java.util.regex.Pattern; |
39 | 39 |
|
40 | 40 | import org.slf4j.Logger; |
41 | 41 | import org.slf4j.LoggerFactory; |
|
52 | 52 | public class UpdateAttachmentsHandler implements EventHandler { |
53 | 53 |
|
54 | 54 | private static final Logger logger = LoggerFactory.getLogger(UpdateAttachmentsHandler.class); |
55 | | - public static final Filter VALMAX_FILTER = (path, element, type) -> element.findAnnotation("Validation.Maximum") |
| 55 | + public static final Filter VALMAX_FILTER = (path, element, type) -> element.getName().contentEquals("content") && element.findAnnotation("Validation.Maximum") |
56 | 56 | .isPresent(); |
57 | 57 |
|
58 | 58 | private final ModifyAttachmentEventFactory eventFactory; |
@@ -88,10 +88,20 @@ void processBefore(CdsUpdateEventContext context, List<CdsData> data) { |
88 | 88 | boolean associationsAreUnchanged = associationsAreUnchanged(target, data); |
89 | 89 |
|
90 | 90 | if (ApplicationHandlerHelper.containsContentField(target, data) || !associationsAreUnchanged) { |
91 | | - |
92 | 91 | // Check here for size of new attachments |
93 | 92 | if (containsValMaxAnnotation(target, data)) { |
| 93 | + List<Attachments> attachments = ApplicationHandlerHelper.condenseAttachments(data, target); |
94 | 94 | long maxSizeValue = FileSizeUtils.convertValMaxToInt(getValMaxValue(target, data)); |
| 95 | + attachments.forEach(attachment -> { |
| 96 | + try { |
| 97 | + int size = attachment.getContent().available(); |
| 98 | + if (size > maxSizeValue) { |
| 99 | + throw new IllegalArgumentException("Attachment " + attachment.getFileName() + " exceeds the maximum allowed size of " + maxSizeValue + " bytes."); |
| 100 | + } |
| 101 | + } catch (IOException e) { |
| 102 | + throw new RuntimeException("Failed to read attachment content size", e); |
| 103 | + } |
| 104 | + }); |
95 | 105 | logger.debug("Validation.Maximum annotation found with value: {}", maxSizeValue); |
96 | 106 | } |
97 | 107 |
|
|
0 commit comments