Skip to content

Commit

Permalink
fixed the issue where we had to expand files
Browse files Browse the repository at this point in the history
  • Loading branch information
jgoldverg committed Nov 23, 2022
1 parent bfa40bc commit c0f3538
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
Expand All @@ -41,8 +45,7 @@ public void createClient(EndpointCredential credential) {
public List<EntityInfo> expandedFileSystem(List<EntityInfo> userSelectedResources, String basePath) {
List<EntityInfo> filesToSend = new ArrayList<>();
Stack<Element> directoriesToTraverse = new Stack<>();
//traverse user selected folders or files
if(basePath.isEmpty()) basePath = "/";
if (basePath.isEmpty()) basePath = "/";
if (userSelectedResources.isEmpty()) { //we move the whole damn server
logger.info(this.credential.getUri() + basePath);
Document doc = Jsoup.connect(this.credential.getUri() + basePath).get();
Expand All @@ -56,15 +59,22 @@ public List<EntityInfo> expandedFileSystem(List<EntityInfo> userSelectedResource
}
} else { //move only files/folders the user selected
for (EntityInfo selectedFiles : userSelectedResources) {
logger.info(this.credential.getUri() + basePath +selectedFiles.getPath());
Document doc = Jsoup.connect(this.credential.getUri() + basePath +selectedFiles.getPath()).get();
Elements links = doc.select("body a");
for (Element elem : links) {
if (elem.text().endsWith("/")) { //directory to expand
directoriesToTraverse.push(elem);
} else { //we have a file
filesToSend.add(fromElement(elem));
logger.info(this.credential.getUri() + basePath + selectedFiles.getPath());
//we have a folder to transfer
if(selectedFiles.getPath().endsWith("/")){
Document doc = Jsoup.connect(this.credential.getUri() + basePath + selectedFiles.getPath())
.ignoreContentType(true)
.get();
Elements links = doc.select("body a");
for (Element elem : links) {
if (elem.text().endsWith("/")) { //directory to expand
directoriesToTraverse.push(elem);
} else { //we have a file
filesToSend.add(fromElement(elem));
}
}
}else{
filesToSend.add(this.fileToInfo(this.credential.getUri() + basePath + selectedFiles.getPath()));
}
}
}
Expand All @@ -74,8 +84,8 @@ public List<EntityInfo> expandedFileSystem(List<EntityInfo> userSelectedResource
if (directory.text().contains("..") || directory.text().contains(".")) {
continue;
}
logger.info(directory.baseUri() + "/" +directory.text());
Document doc = Jsoup.connect(directory.baseUri() + "/" +directory.text()).get();
logger.info(directory.baseUri() + "/" + directory.text());
Document doc = Jsoup.connect(directory.baseUri() + "/" + directory.text()).get();
Elements links = doc.select("body a");
for (Element elem : links) {
if (elem.text().endsWith("/")) { //directory to expand
Expand All @@ -100,4 +110,18 @@ public EntityInfo fromElement(Element elem) throws IOException {
fileInfo.setPath(url.getPath());
return fileInfo;
}

public EntityInfo fileToInfo(String strUrl) throws IOException {
EntityInfo fileInfo = new EntityInfo();
URL url = new URL(strUrl);
URLConnection conn = url.openConnection();
long fileSize = conn.getContentLengthLong();
logger.info("File Name:{}", conn.getURL().getFile());
logger.info("file size={}", fileSize);
logger.info("File Path: {}", url.getPath());
fileInfo.setId(conn.getURL().getFile());
fileInfo.setSize(fileSize);
fileInfo.setPath(url.getPath());
return fileInfo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class HttpExpanderTest extends TestCase {
public AccountEndpointCredential credential(){
AccountEndpointCredential cred = new AccountEndpointCredential();
cred.setAccountId("testHttpServer");
cred.setUsername("cc");
cred.setUri("http://129.114.109.132:80");
return cred;
}
Expand Down Expand Up @@ -76,10 +77,25 @@ public void testNestedTwoLevels(){
Assert.assertEquals(2, fInfo.size());
}

public void testOneFileExpansion(){
testObj = new HttpExpander();
testObj.createClient(this.credential());
ArrayList<EntityInfo> selectedFolders = new ArrayList<>();
selectedFolders.add(singleFileInfo());
List<EntityInfo> fInfo = testObj.expandedFileSystem(selectedFolders, "");
for(EntityInfo fileInfo : fInfo){
if(fileInfo.getId().equals("monty-1.dmg")){
Assert.assertEquals(1073741824L, fileInfo.getSize());
}
System.out.println(fileInfo.toString());
}
Assert.assertEquals(1, fInfo.size());
}

public EntityInfo parallelFilesDir(){
EntityInfo fileInfo = new EntityInfo();
fileInfo.setId("parallelFiles/");
fileInfo.setPath("/parallelFiles/");
fileInfo.setPath("parallelFiles/");
return fileInfo;
}

Expand All @@ -93,7 +109,14 @@ public EntityInfo anotherLayer(){
public EntityInfo helloWorldTwoFiles(){
EntityInfo fileInfo = new EntityInfo();
fileInfo.setId("helloWorld/");
fileInfo.setPath("/helloWorld/");
fileInfo.setPath("helloWorld/");
return fileInfo;
}

public EntityInfo singleFileInfo(){
EntityInfo fileInfo = new EntityInfo();
fileInfo.setId("monty-1.dmg");
fileInfo.setPath("monty-1.dmg");
return fileInfo;
}
}

0 comments on commit c0f3538

Please sign in to comment.