google drive integration v2

this will hopefully fix up and downloading all day long
This commit is contained in:
Seil0 2017-05-04 22:07:50 +02:00
parent bd81cca0a7
commit c7fe070de6
12 changed files with 109 additions and 49 deletions

View File

@ -17,5 +17,6 @@
<classpathentry kind="lib" path="src/libraries/google_apis/jetty-6.1.25.jar"/> <classpathentry kind="lib" path="src/libraries/google_apis/jetty-6.1.25.jar"/>
<classpathentry kind="lib" path="src/libraries/google_apis/jetty-util-6.1.25.jar"/> <classpathentry kind="lib" path="src/libraries/google_apis/jetty-util-6.1.25.jar"/>
<classpathentry kind="lib" path="src/libraries/google_apis/jackson-core-2.8.8.jar"/> <classpathentry kind="lib" path="src/libraries/google_apis/jackson-core-2.8.8.jar"/>
<classpathentry kind="lib" path="src/libraries/commons-codec-1.10.jar"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

2
bin/.gitignore vendored
View File

@ -1 +1 @@
/application/ /cloudControllerInstances/

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -43,6 +43,27 @@ public class CloudController {
System.out.println("done!"); System.out.println("done!");
} }
void stratupCheck(String cloudService, String cemuDirectory) {
if(cloudService == "GoogleDrive") {
try {
if (!googleDriveController.checkFolder()) {
googleDriveController.creatFolder();
main.mainWindowController.saveSettings();
googleDriveController.uploadAllFiles();
} else {
sync(cloudService, cemuDirectory);
}
} catch (IOException e) {
e.printStackTrace();
}
}
if(cloudService == "Dropbox") {
}
}
void sync(String cloudService, String cemuDirectory) { void sync(String cloudService, String cemuDirectory) {
//running sync in a new thread, instead of blocking the main thread //running sync in a new thread, instead of blocking the main thread

View File

@ -35,7 +35,7 @@ import javafx.scene.layout.AnchorPane;
public class Main extends Application { public class Main extends Application {
Stage primaryStage; Stage primaryStage;
private MainWindowController mainWindowController; MainWindowController mainWindowController;
CloudController cloudController; CloudController cloudController;
private String dirWin = System.getProperty("user.home") + "/Documents/cemu_UI"; //Windows: C:/Users/"User"/Documents/HomeFlix private String dirWin = System.getProperty("user.home") + "/Documents/cemu_UI"; //Windows: C:/Users/"User"/Documents/HomeFlix
private String dirLinux = System.getProperty("user.home") + "/cemu_UI"; //Linux: /home/"User"/HomeFlix private String dirLinux = System.getProperty("user.home") + "/cemu_UI"; //Linux: /home/"User"/HomeFlix
@ -122,8 +122,8 @@ public class Main extends Application {
mainWindowController.loadSettings(); mainWindowController.loadSettings();
if(mainWindowController.isCloudSync()) { if(mainWindowController.isCloudSync()) {
cloudController.initializeConnection(mainWindowController.getCloudService(), mainWindowController.getCemuPath()); cloudController.initializeConnection(mainWindowController.getCloudService(), mainWindowController.getCemuPath());
cloudController.sync(mainWindowController.getCloudService(), mainWindowController.getCemuPath()); cloudController.stratupCheck(mainWindowController.getCloudService(), mainWindowController.getCemuPath());
mainWindowController.saveSettings();//TODO find a better way // mainWindowController.saveSettings();//TODO find a better way
} }
mainWindowController.dbController.main(); mainWindowController.dbController.main();
mainWindowController.addUIData(); mainWindowController.addUIData();

View File

@ -1,5 +1,6 @@
package cloudControllerInstances; package cloudControllerInstances;
import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -111,63 +112,80 @@ public class GoogleDriveController {
public void sync(String cemuDirectory) throws IOException { public void sync(String cemuDirectory) throws IOException {
getLocalSavegames(); getLocalSavegames();
//
// if (!checkFolder()) {
// creatFolder();
//
// for (int i = 0; i < localSavegames.size(); i++) {
// uploadFile(localSavegames.get(i));
// }
// } else {
if (!checkFolder()) { getCloudSavegames();
creatFolder(); // System.out.println(cloudSavegames.size() + "; " + localSavegames.size() + "; " + localSavegamesName.size());
for (int i = 0; i < localSavegames.size(); i++) { // download files from cloud which don't exist locally
uploadFile(localSavegames.get(i)); for (int i = 0; i < cloudSavegames.size(); i++) {
}
} else {
getCloudSavegames(); // System.out.println(localSavegamesName.get(i)+";"+cloudSavegames.get(i).getName());
System.out.println(cloudSavegames.size() + "; " + localSavegames.size() + "; " + localSavegamesName.size()); // System.out.println(localSavegames.get(a).getName()+";"+cloudSavegames.get(i).getName().substring(9,cloudSavegames.get(i).getName().length()));
// download files from cloud which don't exist locally // if the file exists locally, check which one is newer
for (int i = 0; i < cloudSavegames.size(); i++) { if (localSavegamesName.contains(cloudSavegames.get(i).getName())) {
// System.out.println(localSavegamesName.get(i)+";"+cloudSavegames.get(i).getName()); int localSavegamesNumber = localSavegamesName.indexOf(cloudSavegames.get(i).getName());
// System.out.println(localSavegames.get(a).getName()+";"+cloudSavegames.get(i).getName().substring(9,cloudSavegames.get(i).getName().length())); long localModified = new DateTime(localSavegames.get(localSavegamesNumber).lastModified()).getValue();
long cloudModified = cloudSavegames.get(i).getModifiedTime().getValue();
FileInputStream fis = new FileInputStream(localSavegames.get(localSavegamesNumber));
// if the file exists locally, check which one is newer // // System.out.println(localSavegamesNumber);
if (localSavegamesName.contains(cloudSavegames.get(i).getName())) { //// System.out.println(localSavegames.get(localSavegamesNumber).getName() + "; " + cloudSavegames.get(i).getName());
// System.out.println(localModified + "; " + cloudModified);
int localSavegamesNumber = localSavegamesName.indexOf(cloudSavegames.get(i).getName()); //// System.out.println(new Date(localModified) + "; " + new Date(cloudModified));
// System.out.println(localSavegamesNumber); // if (localModified == cloudModified) {
System.out.println(localSavegames.get(localSavegamesNumber).getName() + "; " + cloudSavegames.get(i).getName()); // System.out.println("both files are the same, nothing to do \n");
long localModified = new DateTime(localSavegames.get(localSavegamesNumber).lastModified()).getValue(); // } else if (localModified >= cloudModified) {
long cloudModified = cloudSavegames.get(i).getModifiedTime().getValue(); // System.out.println("\nlocal is newer, going to upload local file");
// System.out.println("uploading "+ localSavegames.get(localSavegamesNumber).getName()+"("+new Date(localModified)+")");
System.out.println(new Date(localModified) + "; " + new Date(cloudModified)); //// updateFile(cloudSavegames.get(i), localSavegames.get(localSavegamesNumber));
if (localModified == cloudModified) { // } else {
System.out.println("both files are the same, nothing to do \n"); // System.out.println("\ncloud is newer, going to download cloud file");
} else if (localModified >= cloudModified) { // System.out.println("downloading "+ cloudSavegames.get(i).getName() + "(" + new Date(cloudModified) + ")");
System.out.println("local is newer, going to upload local file \n"); //// downloadFile(cloudSavegames.get(i));
// }
if (cloudSavegames.get(i).getMd5Checksum().equals(org.apache.commons.codec.digest.DigestUtils.md5Hex(fis))) {
System.out.println("both files are the same, nothing to do");
} else {
if (localModified >= cloudModified) {
System.out.print("local is newer, ");
updateFile(cloudSavegames.get(i), localSavegames.get(localSavegamesNumber)); updateFile(cloudSavegames.get(i), localSavegames.get(localSavegamesNumber));
} else { } else {
System.out.println("cloud is newer, going to download cloud file \n"); System.out.print("cloud is newer, ");
downloadFile(cloudSavegames.get(i)); downloadFile(cloudSavegames.get(i));
} }
} else {
System.out.println("file doesn't exist locally");
System.out.println("download " + cloudSavegames.get(i).getName() + " from the cloud");
downloadFile(cloudSavegames.get(i));
} }
}
// upload file to cloud which don't exist in the cloud } else {
for (int j = 0; j < localSavegames.size(); j++) { System.out.print("file doesn't exist locally, ");
if (!cloudSavegamesName.contains(localSavegamesName.get(j))) { downloadFile(cloudSavegames.get(i));
System.out.println("upload " + localSavegames.get(j).getName() + " to the cloud");
uploadFile(localSavegames.get(j));
}
} }
} }
// upload file to cloud which don't exist in the cloud
for (int j = 0; j < localSavegames.size(); j++) {
if (!cloudSavegamesName.contains(localSavegamesName.get(j))) {
uploadFile(localSavegames.get(j));
}
}
// }
} }
//create a folder in google drive //create a folder in google drive
private void creatFolder() throws IOException { public void creatFolder() throws IOException {
System.out.println("creating new folder"); System.out.println("creating new folder");
File fileMetadata = new File(); File fileMetadata = new File();
fileMetadata.setName("cemu_savegames"); fileMetadata.setName("cemu_savegames");
@ -179,7 +197,7 @@ public class GoogleDriveController {
} }
//check if folder already exist //check if folder already exist
private boolean checkFolder() { public boolean checkFolder() {
try { try {
Files.List request = service.files().list().setQ("mimeType = 'application/vnd.google-apps.folder' and name = 'cemu_savegames'"); Files.List request = service.files().list().setQ("mimeType = 'application/vnd.google-apps.folder' and name = 'cemu_savegames'");
FileList files = request.execute(); FileList files = request.execute();
@ -213,7 +231,8 @@ public class GoogleDriveController {
//reading all cloud savegames //reading all cloud savegames
private void getCloudSavegames() throws IOException { private void getCloudSavegames() throws IOException {
Files.List request = service.files().list().setQ("fileExtension = 'dat' and '"+folderID+"' in parents").setFields("nextPageToken, files(id, name, size, modifiedTime, createdTime)"); System.out.println("getting all cloud savegames");
Files.List request = service.files().list().setQ("fileExtension = 'dat' and '"+folderID+"' in parents").setFields("nextPageToken, files(id, name, size, modifiedTime, createdTime, md5Checksum)");
FileList files = request.execute(); FileList files = request.execute();
for (File file : files.getFiles()) { for (File file : files.getFiles()) {
@ -241,25 +260,27 @@ public class GoogleDriveController {
//download a file from the cloud to the local savegames folder //download a file from the cloud to the local savegames folder
private void downloadFile(File downloadFile) throws IOException{ private void downloadFile(File downloadFile) throws IOException{
System.out.println("downloading "+downloadFile.getName()+" ..."); System.out.print("downloading "+downloadFile.getName()+"... ");
// String directory = downloadFile.getName().substring(0,8); // String directory = downloadFile.getName().substring(0,8);
java.io.File directoryFile = new java.io.File("/mlc01/emulatorSave/"+ downloadFile.getName().substring(0,8)); java.io.File directoryFile = new java.io.File(cemuDirectory+"/mlc01/emulatorSave/"+ downloadFile.getName().substring(0,8));
String file = downloadFile.getName().substring(9,downloadFile.getName().length()); String file = downloadFile.getName().substring(9,downloadFile.getName().length());
// System.out.println(cemuDirectory+"/mlc01/emulatorSave/"+ directory +"/"+ file); // System.out.println(cemuDirectory+"/mlc01/emulatorSave/"+ directory +"/"+ file);
// System.out.println("DownloadfileID: " + downloadFile.getId()); // System.out.println("DownloadfileID: " + downloadFile.getId());
if(!directoryFile.exists()) { if(!directoryFile.exists()) {
System.out.println("dir dosent exist");
directoryFile.mkdir(); directoryFile.mkdir();
} }
OutputStream outputStream = new FileOutputStream(directoryFile +"/"+ file); //TODO needs to be tested OutputStream outputStream = new FileOutputStream(directoryFile +"/"+ file); //TODO needs to be tested
service.files().get(downloadFile.getId()).executeMediaAndDownloadTo(outputStream); service.files().get(downloadFile.getId()).executeMediaAndDownloadTo(outputStream);
System.out.println("done"); System.out.println("done");
} }
//update a file in the cloud, by deleting the old one and uploading an new with the same id //update a file in the cloud, by deleting the old one and uploading an new with the same id
private void updateFile(File oldFile, java.io.File newFile) throws IOException { private void updateFile(File oldFile, java.io.File newFile) throws IOException {
//deleting old file System.out.println("updating " +oldFile.getName()+"... ");
service.files().delete(oldFile.getId()).execute(); service.files().delete(oldFile.getId()).execute(); //deleting old file
//uploading new file //uploading new file
File fileMetadata = new File(); File fileMetadata = new File();
@ -271,6 +292,23 @@ public class GoogleDriveController {
File file = service.files().create(fileMetadata, mediaContent).setFields("id, parents").execute(); File file = service.files().create(fileMetadata, mediaContent).setFields("id, parents").execute();
System.out.println("File ID: " + file.getId()); System.out.println("File ID: " + file.getId());
} }
public void uploadAllFiles() {
new Thread() {
public void run() {
try {
getLocalSavegames();
for (int i = 0; i < localSavegames.size(); i++) {
uploadFile(localSavegames.get(i));
}
} catch (IOException e) {
// Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
public String getFolderID() { public String getFolderID() {
return folderID; return folderID;

Binary file not shown.