Skip to content

Commit

Permalink
Merge pull request #110 from dterefe/main
Browse files Browse the repository at this point in the history
Fixes for NextCloud and GoogleDrive Connector
  • Loading branch information
dterefe authored Jan 24, 2025
2 parents c942f85 + 0fae82e commit 74c930d
Show file tree
Hide file tree
Showing 4 changed files with 318 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2304,6 +2304,11 @@ public void setPipelineStatus(String name, String status) {
pipelineStatus.put(name, status);
}

public static String getLocalhost() {
boolean isDocker = Files.exists(Paths.get("/.dockerenv"));
return isDocker ? "http://host.docker.internal" : "http://127.0.0.1";
}

public DebugLevel getDebugLevel() {
return debugLevel;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.texttechnologylab.DockerUnifiedUIMAInterface.document_handler;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.InputStreamContent;
import com.google.api.client.http.javanet.NetHttpTransport;
Expand All @@ -13,10 +12,10 @@
import org.texttechnologylab.DockerUnifiedUIMAInterface.monitoring.DUUIStatus;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


Expand All @@ -26,74 +25,78 @@ public class DUUIGoogleDriveDocumentHandler implements IDUUIDocumentHandler, IDU
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static String root = "";
private final Drive service;

public DUUIGoogleDriveDocumentHandler(Credential credential) throws GeneralSecurityException, IOException {
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
credential.refreshToken();

createAppFolder(service);

}

public static void createAppFolder(Drive service) throws IOException, IllegalStateException {
String folderName = "DUUI";
String parentFolderName = "Apps";

// Search for the Docker Unified UIMA Interface folder
String parentQuery = "mimeType='application/vnd.google-apps.folder' and name='" + parentFolderName + "'";
FileList parentResult = service.files().list()
.setQ(parentQuery)
.setFields("files(id)")
.execute();

List<File> parentFiles = parentResult.getFiles();

if (!folderExists(service)) {
Optional<File> maybeParentFolder = parentFiles.stream().filter(File::getIsAppAuthorized).findFirst();
File parentFolder;
if (maybeParentFolder.isEmpty()) {
File appsFolder = new File()
.setName("Apps")
.setMimeType("application/vnd.google-apps.folder");

File createdAppsFolder = service.files().create(appsFolder).setFields("id").execute();
createdAppsFolder.setIsAppAuthorized(true);

File duuiFolder = new File()
.setName("Docker Unified UIMA Interface")
.setName(folderName)
.setMimeType("application/vnd.google-apps.folder")
.setParents(Collections.singletonList(createdAppsFolder.getId()));

File createdDuuiFolder = service.files().create(duuiFolder).setFields("id").execute();

createdDuuiFolder.setIsAppAuthorized(true);
root = createdDuuiFolder.getId();
return;
} else {
parentFolder = maybeParentFolder.get();
}

}

public static boolean folderExists(Drive service) throws IOException, IllegalStateException {
String folderName = "Docker Unified UIMA Interface";
String parentFolderName = "Apps";

// Search for the Docker Unified UIMA Interface folder
String query = "mimeType='application/vnd.google-apps.folder' and name='" + folderName + "'";
String query =
"mimeType='application/vnd.google-apps.folder' and name='" + folderName + "' and '" + maybeParentFolder.get().getId() + "' in parents";
FileList result = service.files().list()
.setQ(query)
.setFields("files(id, name, parents)")
.execute();

List<File> files = result.getFiles();

if (files.isEmpty()) {
return false;
}

if (files.size() > 1) {
throw new IllegalStateException("Multiple folders named '" + folderName + "' found. Please delete all (including the ones contained in the trash) but one.");
}

File duuiFolder = files.get(0);

if (duuiFolder.getParents() == null || duuiFolder.getParents().isEmpty()) {
throw new IllegalStateException("The folder '" + folderName + "' has no parent. This is not allowed.");
}
Optional<File> maybeFolder = files.stream().filter(File::getIsAppAuthorized).findFirst();

if (duuiFolder.getParents().size() > 1) {
throw new IllegalStateException("The folder '" + folderName + "' has multiple parents. This is not allowed.");
}
if (maybeFolder.isEmpty()) {

String parentId = duuiFolder.getParents().get(0);
File parentFolder = service.files().get(parentId)
.setFields("id, name")
.execute();
File duuiFolder = new File()
.setName(folderName)
.setMimeType("application/vnd.google-apps.folder")
.setParents(Collections.singletonList(parentFolder.getId()));

if (!parentFolder.getName().equalsIgnoreCase(parentFolderName)) {
throw new IllegalStateException("The parent folder is not '" + parentFolderName + "'. Please check the folder structure.");
File createdDuuiFolder = service.files().create(duuiFolder).setFields("id").execute();
createdDuuiFolder.setIsAppAuthorized(true);
root = createdDuuiFolder.getId();
} else {
root = maybeFolder.get().getId();
}

root = duuiFolder.getId();

return true;
}

public static void main(String... args) throws IOException, GeneralSecurityException {
Expand Down Expand Up @@ -269,6 +272,7 @@ public List<DUUIDocument> listDocuments_(String searchPath, String fileExtension
documents = List.of();
} else {
documents = files.stream()
.filter(File::getIsAppAuthorized)
.map(f -> new DUUIDocument(f.getName(), f.getId(), f.getSize()))
.collect(Collectors.toList());
}
Expand All @@ -279,7 +283,7 @@ public List<DUUIDocument> listDocuments_(String searchPath, String fileExtension
@Override
public DUUIFolder getFolderStructure() {

DUUIFolder root = new DUUIFolder(this.root, "Files");
DUUIFolder root = new DUUIFolder(DUUIGoogleDriveDocumentHandler.root, "Files");

return getFolderStructure(root);
}
Expand Down
Loading

0 comments on commit 74c930d

Please sign in to comment.