private void replaceAttachments()

in server/src/main/java/com/epam/aidial/core/server/service/PublicationUtil.java [50:83]


    private void replaceAttachments(JsonArray messages, Map<String, String> attachmentsMapping) {
        if (messages == null || messages.isEmpty()) {
            return;
        }

        for (int i = 0; i < messages.size(); i++) {
            JsonObject message = messages.getJsonObject(i);
            JsonObject customContent = message.getJsonObject("custom_content");
            if (customContent == null) {
                continue;
            }
            JsonArray attachments = customContent.getJsonArray("attachments");
            if (attachments == null || attachments.isEmpty()) {
                continue;
            }
            for (int j = 0; j < attachments.size(); j++) {
                JsonObject attachment = attachments.getJsonObject(j);
                String url = attachment.getString("url");
                if (url == null) {
                    continue;
                }
                boolean isMetadata = false;
                if (url.startsWith(ProxyUtil.METADATA_PREFIX)) {
                    isMetadata = true;
                    url = url.substring(ProxyUtil.METADATA_PREFIX.length());
                }
                String decodedUrl = UrlUtil.decodePath(url);
                String toReplace = attachmentsMapping.get(decodedUrl);
                if (toReplace != null) {
                    attachment.put("url", isMetadata ? ProxyUtil.METADATA_PREFIX + toReplace : toReplace);
                }
            }
        }
    }