This commit is contained in:
2023-06-05 10:21:30 -04:00
parent 3ff8762512
commit f34c0a39c0
16 changed files with 592 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
package tech.nevets.gdrivemigrator;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.List;
public class GoogleConnector {
private static final String APP_NAME = "GMigrator";
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
private static final String TOKENS_DIRECTORY_PATH = "tokens";
private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE);
private static final String CREDENTIALS_FILE_PATH = "/oauth-token.json";
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
InputStream is = GoogleConnector.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
if (is == null) {
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
}
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(is));
GoogleAuthorizationCodeFlow authFlow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder()
.setPort(8000)
.build();
Credential credential = new AuthorizationCodeInstalledApp(authFlow, receiver)
.authorize("user");
return credential;
}
public static List<File> getFiles() throws Exception {
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName(APP_NAME)
.build();
FileList result = service.files().list()
.setPageSize(10)
.setFields("nextPageToken, files(id, name)")
.execute();
return result.getFiles();
}
}

View File

@@ -0,0 +1,25 @@
package tech.nevets.gdrivemigrator;
import com.google.api.services.drive.model.File;
import spark.Request;
import spark.Response;
import spark.Route;
import java.util.List;
public class RootRoute implements Route {
@Override
public Object handle(Request request, Response response) throws Exception {
List<File> files = GoogleConnector.getFiles();
return filesToString(files);
}
private String filesToString(List<File> files) {
StringBuilder sb = new StringBuilder();
for (File file : files) {
sb.append(file.getName()).append(",").append(file.getId()).append("\n");
}
return sb.toString();
}
}

View File

@@ -0,0 +1,12 @@
package tech.nevets.gdrivemigrator;
import static spark.Spark.*;
public class Server {
public static void main(String[] args) {
port(8080);
get("/", new RootRoute());
}
}