Skip to content

Commit

Permalink
add restAPI for downloading latest version of an extension (#657)
Browse files Browse the repository at this point in the history
* add restAPI for downloading latest version of an extension

* modified code

* remove query on line 177 that doesn't get assigned anywhere
  • Loading branch information
yiningwang11 authored Jan 23, 2023
1 parent 6b0d522 commit b32a5ef
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private ExtensionVersion findExtensionVersion(String namespace, String extension
@Transactional
public ResponseEntity<byte[]> getFile(String namespace, String extensionName, String targetPlatform, String version, String fileName) {
var extVersion = findExtensionVersion(namespace, extensionName, targetPlatform, version);
var resource = repositories.findFileByName(extVersion, fileName);
var resource = isType(fileName) ? repositories.findFileByType(extVersion, fileName.toLowerCase()) : repositories.findFileByName(extVersion, fileName);
if (resource == null)
throw new NotFoundException();
if (resource.getType().equals(DOWNLOAD))
Expand All @@ -182,6 +182,11 @@ public ResponseEntity<byte[]> getFile(String namespace, String extensionName, St
return storageUtil.getFileResponse(resource);
}

public boolean isType (String fileName){
var expectedTypes = Arrays.asList(FileResource.MANIFEST, FileResource.README, FileResource.LICENSE, FileResource.ICON, FileResource.DOWNLOAD, FileResource.CHANGELOG);
return expectedTypes.stream().anyMatch(fileName::equalsIgnoreCase);
}

@Override
public ReviewListJson getReviews(String namespace, String extensionName) {
var extension = repositories.findExtension(extensionName, namespace);
Expand Down
28 changes: 28 additions & 0 deletions server/src/test/java/org/eclipse/openvsx/RegistryAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,14 @@ public void testUnknownFile() throws Exception {
.andExpect(status().isNotFound());
}

@Test
public void testLatestFile() throws Exception {
mockLatest();
mockMvc.perform(get("/api/{namespace}/{extension}/{version}/file/{fileName}", "foo", "bar", "latest", "DOWNLOAD"))
.andExpect(status().isOk())
.andExpect(content().string("latest download"));
}

@Test
public void testReviews() throws Exception {
mockReviews();
Expand Down Expand Up @@ -1886,6 +1894,8 @@ private FileResource mockReadme(String targetPlatform) {
Mockito.when(entityManager.merge(resource)).thenReturn(resource);
Mockito.when(repositories.findFileByName(extVersion, "README"))
.thenReturn(resource);
Mockito.when(repositories.findFileByType(extVersion, FileResource.README))
.thenReturn(resource);
return resource;
}

Expand All @@ -1900,6 +1910,8 @@ private FileResource mockChangelog() {
Mockito.when(entityManager.merge(resource)).thenReturn(resource);
Mockito.when(repositories.findFileByName(extVersion, "CHANGELOG"))
.thenReturn(resource);
Mockito.when(repositories.findFileByType(extVersion, FileResource.CHANGELOG))
.thenReturn(resource);
return resource;
}

Expand All @@ -1914,6 +1926,22 @@ private FileResource mockLicense() {
Mockito.when(entityManager.merge(resource)).thenReturn(resource);
Mockito.when(repositories.findFileByName(extVersion, "LICENSE"))
.thenReturn(resource);
Mockito.when(repositories.findFileByType(extVersion, FileResource.LICENSE))
.thenReturn(resource);
return resource;
}

private FileResource mockLatest() {
var extVersion = mockExtension();
var resource = new FileResource();
resource.setExtension(extVersion);
resource.setName("DOWNLOAD");
resource.setType(FileResource.DOWNLOAD);
resource.setContent("latest download".getBytes());
resource.setStorageType(FileResource.STORAGE_DB);
Mockito.when(entityManager.merge(resource)).thenReturn(resource);
Mockito.when(repositories.findFileByType(extVersion, FileResource.DOWNLOAD))
.thenReturn(resource);
return resource;
}

Expand Down

0 comments on commit b32a5ef

Please sign in to comment.