Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
artsiomkorzun committed Mar 6, 2024
1 parent 303396b commit b00556f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 20 deletions.
3 changes: 0 additions & 3 deletions src/main/java/com/epam/aidial/core/AiDial.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ public class AiDial {

private BlobStorage storage;
private ResourceService resourceService;
private InvitationService invitationService;
private ShareService shareService;
private PublicationService publicationService;

private LongSupplier clock = System::currentTimeMillis;
private Supplier<String> generator = () -> UUID.randomUUID().toString().replace("-", "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.epam.aidial.core.data.ResourceUrl;
import com.epam.aidial.core.security.EncryptionService;
import com.epam.aidial.core.storage.BlobStorage;
import com.epam.aidial.core.storage.BlobStorageUtil;
import com.epam.aidial.core.storage.ResourceDescription;
import com.epam.aidial.core.util.ProxyUtil;
import com.epam.aidial.core.util.UrlUtil;
Expand All @@ -23,7 +24,8 @@
import javax.annotation.Nullable;

import static com.epam.aidial.core.storage.BlobStorageUtil.PATH_SEPARATOR;
import static com.epam.aidial.core.storage.ResourceDescription.PUBLIC_BUCKET;
import static com.epam.aidial.core.storage.BlobStorageUtil.PUBLIC_BUCKET;
import static com.epam.aidial.core.storage.BlobStorageUtil.PUBLIC_LOCATION;

@RequiredArgsConstructor
public class PublicationService {
Expand All @@ -33,7 +35,7 @@ public class PublicationService {
};

private static final ResourceDescription PUBLIC_PUBLICATIONS = ResourceDescription.fromDecoded(
ResourceType.PUBLICATION, PUBLIC_BUCKET, ResourceDescription.PUBLIC_LOCATION,
ResourceType.PUBLICATION, PUBLIC_BUCKET, PUBLIC_LOCATION,
PUBLICATIONS_NAME);

private final EncryptionService encryption;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
@UtilityClass
public class BlobStorageUtil {

public static final String APPDATA_PATTERN = "appdata/%s";
public static final String PATH_SEPARATOR = "/";

private static final String USER_BUCKET_PATTERN = "Users/%s/";
public static final String PUBLIC_BUCKET = "public";
public static final String PUBLIC_LOCATION = PUBLIC_BUCKET + PATH_SEPARATOR;

public static final String APPDATA_PATTERN = "appdata/%s";
private static final String USER_BUCKET_PATTERN = "Users/%s/";
private static final String API_KEY_BUCKET_PATTERN = "Keys/%s/";

public static final String PATH_SEPARATOR = "/";

public String getContentType(String fileName) {
String mimeType = MimeMapping.getMimeTypeForFilename(fileName);
return mimeType == null ? "application/octet-stream" : mimeType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class ResourceDescription {

public static final String PUBLIC_BUCKET = "public";
public static final String PUBLIC_LOCATION = PUBLIC_BUCKET + BlobStorageUtil.PATH_SEPARATOR;

private static final Set<Character> INVALID_FILE_NAME_CHARS = Set.of('/', '{', '}');
private static final int MAX_PATH_SIZE = 900;

Expand Down Expand Up @@ -169,10 +166,19 @@ public static ResourceDescription fromLink(String link, EncryptionService encryp

private static ResourceDescription fromLink(String link, String bucketEncoded, String bucketDecoded) {
String[] parts = link.split(BlobStorageUtil.PATH_SEPARATOR);

if (parts.length < 2) {
throw new IllegalArgumentException("Invalid resource link provided " + link);
}

if (link.startsWith(BlobStorageUtil.PATH_SEPARATOR)) {
throw new IllegalArgumentException("Link must not start with " + BlobStorageUtil.PATH_SEPARATOR + ", but: " + link);
}

if (parts.length == 2 && !link.endsWith(BlobStorageUtil.PATH_SEPARATOR)) {
throw new IllegalArgumentException("Link must start resource/bucket/, but: " + BlobStorageUtil.PATH_SEPARATOR + ": " + link);
}

ResourceType resourceType = ResourceType.of(UrlUtil.decodePath(parts[0]));
String bucket = UrlUtil.decodePath(parts[1]);
if (!bucket.equals(bucketEncoded)) {
Expand All @@ -188,7 +194,7 @@ public static ResourceDescription fromBucketLink(String link, ResourceDescriptio
}

public static ResourceDescription fromPublicLink(String link) {
return fromLink(link, PUBLIC_BUCKET, PUBLIC_LOCATION);
return fromLink(link, BlobStorageUtil.PUBLIC_BUCKET, BlobStorageUtil.PUBLIC_LOCATION);
}

private static ResourceDescription from(ResourceType type, String bucketName, String bucketLocation,
Expand Down
7 changes: 0 additions & 7 deletions src/test/java/com/epam/aidial/core/ResourceBaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,6 @@ Response send(HttpMethod method, String path, String queryParams, String body, S
}
}

@SneakyThrows
private static String pretty(String json) {
ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
Object jsonObject = mapper.readValue(json, Object.class);
return mapper.writeValueAsString(jsonObject);
}

record Response(int status, String body) {
public boolean ok() {
return status() == 200;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,35 @@ public void testResourceWithInvalidFilename() {
assertThrows(IllegalArgumentException.class,
() -> ResourceDescription.fromEncoded(ResourceType.FILE, "bucket", "location/", "folder1/file%7D.txt"));
}

@Test
public void testValidPublicLinks() {
assertEquals(
ResourceDescription.fromPublicLink("publications/public/"),
ResourceDescription.fromEncoded(ResourceType.PUBLICATION, "public", "public/", "")
);

assertEquals(
ResourceDescription.fromPublicLink("publications/public/file"),
ResourceDescription.fromEncoded(ResourceType.PUBLICATION, "public", "public/", "file")
);

assertEquals(
ResourceDescription.fromPublicLink("publications/public/folder/"),
ResourceDescription.fromEncoded(ResourceType.PUBLICATION, "public", "public/", "folder/")
);

assertEquals(
ResourceDescription.fromPublicLink("publications/public/%30").getName(),
"0"
);
}

@Test
public void testInvalidPublicLinks() {
assertThrows(IllegalArgumentException.class, () -> ResourceDescription.fromPublicLink("/publications/public/"));
assertThrows(IllegalArgumentException.class, () -> ResourceDescription.fromPublicLink("publications/public"));
assertThrows(IllegalArgumentException.class, () -> ResourceDescription.fromPublicLink("publications/public"));
assertThrows(IllegalArgumentException.class, () -> ResourceDescription.fromPublicLink("publications/private/"));
}
}

0 comments on commit b00556f

Please sign in to comment.