Skip to content

Commit

Permalink
fix: Publish request fails for quick apps #653 (#654)
Browse files Browse the repository at this point in the history
  • Loading branch information
astsiapanay authored Jan 28, 2025
1 parent 91d0c02 commit 6f9da9f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -378,26 +378,6 @@ private void prepareAndValidatePublicationRequest(ProxyContext context, Publicat
validateRules(publication);
}

/**
* Builds the target folder path for custom application files.
*
* @param resource the publication resource containing the target URL
* @return the constructed target folder path for custom application files
*/
private static String buildTargetFolderForCustomAppFiles(Publication.Resource resource) {
String targetUrl = resource.getTargetUrl();
// Find the index of the end of a bucket segment (the second slash in the target URL)
int indexOfBucketEndSlash = targetUrl.indexOf(ResourceDescriptor.PATH_SEPARATOR, targetUrl.indexOf(ResourceDescriptor.PATH_SEPARATOR) + 1);
// Find the index of the start of a file name (the last slash in the target URL)
int indexOfFileNameStartSlash = targetUrl.lastIndexOf(ResourceDescriptor.PATH_SEPARATOR);
// Extract the application path from the target URL
String appPath = targetUrl.substring(indexOfBucketEndSlash + 1, indexOfFileNameStartSlash);
// Extract the application name from the target URL
String appName = targetUrl.substring(indexOfFileNameStartSlash + 1);
// Construct and return the target folder path
return appPath + ResourceDescriptor.PATH_SEPARATOR + "." + appName + ResourceDescriptor.PATH_SEPARATOR;
}

private void addCustomApplicationRelatedFiles(ProxyContext context, Publication publication) {
List<String> existingUrls = publication.getResources().stream()
.map(Publication.Resource::getSourceUrl)
Expand All @@ -416,7 +396,7 @@ private void addCustomApplicationRelatedFiles(ProxyContext context, Publication
if (application.getApplicationTypeSchemaId() == null) {
return Stream.empty();
}
String targetFolder = buildTargetFolderForCustomAppFiles(resource);
String targetFolder = PublicationUtil.buildTargetFolderForCustomAppFiles(resource.getTargetUrl(), encryption);
return ApplicationTypeSchemaUtils.getFiles(context.getConfig(), application, encryption, resourceService)
.stream()
.filter(sourceDescriptor -> !existingUrls.contains(sourceDescriptor.getUrl()) && !sourceDescriptor.isPublic())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.epam.aidial.core.server.service;

import com.epam.aidial.core.server.data.ResourceTypes;
import com.epam.aidial.core.server.security.EncryptionService;
import com.epam.aidial.core.server.util.ProxyUtil;
import com.epam.aidial.core.server.util.ResourceDescriptorFactory;
import com.epam.aidial.core.storage.resource.ResourceDescriptor;
import com.epam.aidial.core.storage.util.UrlUtil;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import lombok.experimental.UtilityClass;

import java.util.List;
import java.util.Map;

@UtilityClass
Expand Down Expand Up @@ -96,4 +100,29 @@ private JsonObject replaceConversationIdentity(String conversationBody, Resource
conversation.put("folderId", folderUrl);
return conversation;
}


/**
* Builds the target folder path for custom application files.
*
* @param targetUrl the target URL of the application being published
* @return the constructed target folder path for custom application files
*/
static String buildTargetFolderForCustomAppFiles(String targetUrl, EncryptionService encryptionService) {
ResourceDescriptor descriptor = ResourceDescriptorFactory.fromAnyUrl(targetUrl, encryptionService);
if (descriptor.isFolder()) {
throw new IllegalArgumentException("target url must be a file");
}
if (descriptor.getType() != ResourceTypes.APPLICATION) {
throw new IllegalArgumentException("target url must be an application type");
}
String appName = descriptor.getName();
List<String> folders = descriptor.getParentFolders();
if (folders.isEmpty()) {
return "." + appName + ResourceDescriptor.PATH_SEPARATOR;
} else {
String appPath = String.join(ResourceDescriptor.PATH_SEPARATOR, folders);
return appPath + ResourceDescriptor.PATH_SEPARATOR + "." + appName + ResourceDescriptor.PATH_SEPARATOR;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,38 @@

import com.epam.aidial.core.server.ResourceBaseTest;
import com.epam.aidial.core.server.data.ResourceTypes;
import com.epam.aidial.core.server.security.EncryptionService;
import com.epam.aidial.core.server.util.ProxyUtil;
import com.epam.aidial.core.server.util.ResourceDescriptorFactory;
import com.epam.aidial.core.storage.resource.ResourceDescriptor;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public class PublicationUtilTest {

@Mock
private EncryptionService encryptionService;

@Test
public void testBuildTargetFolderForCustomAppFiles() {
when(encryptionService.decrypt(any(String.class))).thenReturn("location/");
assertEquals("folder/.appA/", PublicationUtil.buildTargetFolderForCustomAppFiles("applications/asdfoiefjio/folder/appA", encryptionService));
assertEquals(".appA/", PublicationUtil.buildTargetFolderForCustomAppFiles("applications/asdfoiefjio/appA", encryptionService));
assertThrows(IllegalArgumentException.class, () -> PublicationUtil.buildTargetFolderForCustomAppFiles("applications/asdfoiefjio/appA/", encryptionService));
assertThrows(IllegalArgumentException.class, () -> PublicationUtil.buildTargetFolderForCustomAppFiles("prompts/asdfoiefjio/appA", encryptionService));
}

@Test
void testConversationIdReplacement() {
ResourceDescriptor targetResource1 = ResourceDescriptorFactory.fromDecoded(ResourceTypes.CONVERSATION, "bucketName", "bucket/location/", "conversation");
Expand Down

0 comments on commit 6f9da9f

Please sign in to comment.