google drive controller clean up
This commit is contained in:
parent
51875e3b72
commit
32e474b6be
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -189,6 +189,7 @@ public class MainWindowController {
|
||||||
romTextField.setText(romPath);
|
romTextField.setText(romPath);
|
||||||
colorPicker.setValue(Color.valueOf(getColor()));
|
colorPicker.setValue(Color.valueOf(getColor()));
|
||||||
fullscreenToggleBtn.setSelected(isFullscreen());
|
fullscreenToggleBtn.setSelected(isFullscreen());
|
||||||
|
cloudSyncToggleBtn.setSelected(isCloudSync());
|
||||||
edit.setDisable(true);
|
edit.setDisable(true);
|
||||||
applyColor();
|
applyColor();
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,24 +40,21 @@ public class GoogleDriveController {
|
||||||
private ArrayList<String> localSavegamesName = new ArrayList<>();
|
private ArrayList<String> localSavegamesName = new ArrayList<>();
|
||||||
private ArrayList<String> cloudSavegamesName = new ArrayList<>();
|
private ArrayList<String> cloudSavegamesName = new ArrayList<>();
|
||||||
|
|
||||||
/** Application name. */
|
|
||||||
private final String APPLICATION_NAME ="cemu_Ui Drive API Controller";
|
private final String APPLICATION_NAME ="cemu_Ui Drive API Controller";
|
||||||
|
|
||||||
/** Directory to store user credentials for this application. */
|
//Directory to store user credentials for this application
|
||||||
private final java.io.File DATA_STORE_DIR = new java.io.File(System.getProperty("user.home"), ".credentials/cemu_UI_credential");
|
private final java.io.File DATA_STORE_DIR = new java.io.File(System.getProperty("user.home"), ".credentials/cemu_UI_credential");
|
||||||
|
|
||||||
/** Global instance of the {@link FileDataStoreFactory}. */
|
//Global instance of the {@link FileDataStoreFactory}
|
||||||
private FileDataStoreFactory DATA_STORE_FACTORY;
|
private FileDataStoreFactory DATA_STORE_FACTORY;
|
||||||
|
|
||||||
/** Global instance of the JSON factory. */
|
//Global instance of the JSON factory
|
||||||
private final JsonFactory JSON_FACTORY =JacksonFactory.getDefaultInstance();
|
private final JsonFactory JSON_FACTORY =JacksonFactory.getDefaultInstance();
|
||||||
|
|
||||||
/** Global instance of the HTTP transport. */
|
//Global instance of the HTTP transport
|
||||||
private HttpTransport HTTP_TRANSPORT;
|
private HttpTransport HTTP_TRANSPORT;
|
||||||
|
|
||||||
/** Global instance of the scopes required by this quickstart.
|
/**If modifying these scopes, delete your previously saved credentials
|
||||||
*
|
|
||||||
* If modifying these scopes, delete your previously saved credentials
|
|
||||||
* at ~/.credentials/cemu_UI_credential
|
* at ~/.credentials/cemu_UI_credential
|
||||||
*/
|
*/
|
||||||
private final java.util.Collection<String> SCOPES = DriveScopes.all();
|
private final java.util.Collection<String> SCOPES = DriveScopes.all();
|
||||||
|
@ -110,45 +107,41 @@ public class GoogleDriveController {
|
||||||
public void main(String cemuDirectory) throws IOException {
|
public void main(String cemuDirectory) throws IOException {
|
||||||
service = getDriveService();
|
service = getDriveService();
|
||||||
this.cemuDirectory = cemuDirectory;
|
this.cemuDirectory = cemuDirectory;
|
||||||
// sync();
|
|
||||||
|
|
||||||
// upload();
|
|
||||||
// download();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sync(String cemuDirectory) throws IOException {
|
public void sync(String cemuDirectory) throws IOException {
|
||||||
getLocalSavegames();
|
getLocalSavegames();
|
||||||
// upload();
|
|
||||||
if(!checkFolder()) {
|
if (!checkFolder()) {
|
||||||
creatFolder();
|
creatFolder();
|
||||||
|
|
||||||
for(int i = 0; i < localSavegames.size(); i++) {
|
for (int i = 0; i < localSavegames.size(); i++) {
|
||||||
uploadFile(localSavegames.get(i));
|
uploadFile(localSavegames.get(i));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
getCloudSavegames();
|
getCloudSavegames();
|
||||||
System.out.println(cloudSavegames.size()+"; "+localSavegames.size()+"; "+localSavegamesName.size());
|
System.out.println(cloudSavegames.size() + "; " + localSavegames.size() + "; " + localSavegamesName.size());
|
||||||
|
|
||||||
//download files from cloud which don't exist locally
|
// download files from cloud which don't exist locally
|
||||||
for(int i = 0; i < cloudSavegames.size(); i++) {
|
for (int i = 0; i < cloudSavegames.size(); i++) {
|
||||||
|
|
||||||
// System.out.println(localSavegamesName.get(i)+"; "+cloudSavegames.get(i).getName());
|
// System.out.println(localSavegamesName.get(i)+";"+cloudSavegames.get(i).getName());
|
||||||
// System.out.println(localSavegames.get(a).getName()+"; "+cloudSavegames.get(i).getName().substring(9, cloudSavegames.get(i).getName().length()));
|
// System.out.println(localSavegames.get(a).getName()+";"+cloudSavegames.get(i).getName().substring(9,cloudSavegames.get(i).getName().length()));
|
||||||
|
|
||||||
// if the file exists locally check which one is newer
|
// if the file exists locally, check which one is newer
|
||||||
if(localSavegamesName.contains(cloudSavegames.get(i).getName())) {
|
if (localSavegamesName.contains(cloudSavegames.get(i).getName())) {
|
||||||
|
|
||||||
int localSavegamesNumber = localSavegamesName.indexOf(cloudSavegames.get(i).getName());
|
int localSavegamesNumber = localSavegamesName.indexOf(cloudSavegames.get(i).getName());
|
||||||
// System.out.println(localSavegamesNumber);
|
// System.out.println(localSavegamesNumber);
|
||||||
System.out.println(localSavegames.get(localSavegamesNumber).getName()+"; "+cloudSavegames.get(i).getName());
|
System.out.println(localSavegames.get(localSavegamesNumber).getName() + "; " + cloudSavegames.get(i).getName());
|
||||||
long localModified = new DateTime(localSavegames.get(localSavegamesNumber).lastModified()).getValue();
|
long localModified = new DateTime(localSavegames.get(localSavegamesNumber).lastModified()).getValue();
|
||||||
long cloudModified = cloudSavegames.get(i).getModifiedTime().getValue();
|
long cloudModified = cloudSavegames.get(i).getModifiedTime().getValue();
|
||||||
|
|
||||||
System.out.println(new Date(localModified)+"; "+new Date(cloudModified));
|
System.out.println(new Date(localModified) + "; " + new Date(cloudModified));
|
||||||
if(localModified == cloudModified) {
|
if (localModified == cloudModified) {
|
||||||
System.out.println("both files are the same, nothing to do \n");
|
System.out.println("both files are the same, nothing to do \n");
|
||||||
} else if(localModified >= cloudModified) {
|
} else if (localModified >= cloudModified) {
|
||||||
System.out.println("local is newer, going to upload local file \n");
|
System.out.println("local is newer, going to upload local file \n");
|
||||||
updateFile(cloudSavegames.get(i), localSavegames.get(localSavegamesNumber));
|
updateFile(cloudSavegames.get(i), localSavegames.get(localSavegamesNumber));
|
||||||
} else {
|
} else {
|
||||||
|
@ -157,39 +150,35 @@ public class GoogleDriveController {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
System.out.println("file doesn't exist locally");
|
System.out.println("file doesn't exist locally");
|
||||||
System.out.println("download "+cloudSavegames.get(i).getName()+" from the cloud");
|
System.out.println("download " + cloudSavegames.get(i).getName() + " from the cloud");
|
||||||
downloadFile(cloudSavegames.get(i));
|
downloadFile(cloudSavegames.get(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//upload file to cloud which don't exist in the cloud
|
// upload file to cloud which don't exist in the cloud
|
||||||
for(int j = 0; j < localSavegames.size(); j++) {
|
for (int j = 0; j < localSavegames.size(); j++) {
|
||||||
if(!cloudSavegamesName.contains(localSavegamesName.get(j))) {
|
if (!cloudSavegamesName.contains(localSavegamesName.get(j))) {
|
||||||
System.out.println("upload "+localSavegames.get(j).getName()+" to the cloud");
|
System.out.println("upload " + localSavegames.get(j).getName() + " to the cloud");
|
||||||
uploadFile(localSavegames.get(j));
|
uploadFile(localSavegames.get(j));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//check if there are new local files to upload or download, if so upload/download them and [check if there are files in the cloud were last edit is newer than local, and other way round]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//create a folder in google drive
|
||||||
private void creatFolder() throws IOException {
|
private 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");
|
||||||
fileMetadata.setMimeType("application/vnd.google-apps.folder");
|
fileMetadata.setMimeType("application/vnd.google-apps.folder");
|
||||||
|
|
||||||
File file = service.files().create(fileMetadata)
|
File file = service.files().create(fileMetadata).setFields("id").execute();
|
||||||
.setFields("id")
|
|
||||||
.execute();
|
|
||||||
System.out.println("Folder ID: " + file.getId());
|
System.out.println("Folder ID: " + file.getId());
|
||||||
folderID = file.getId();
|
folderID = file.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//check if folder already exist
|
||||||
private boolean checkFolder() {
|
private 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'");
|
||||||
|
@ -205,6 +194,7 @@ public class GoogleDriveController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//reading all local savegames
|
||||||
private void getLocalSavegames() throws IOException {
|
private void getLocalSavegames() throws IOException {
|
||||||
java.io.File dir = new java.io.File(cemuDirectory+"/mlc01/emulatorSave");
|
java.io.File dir = new java.io.File(cemuDirectory+"/mlc01/emulatorSave");
|
||||||
String[] extensions = new String[] { "dat" };
|
String[] extensions = new String[] { "dat" };
|
||||||
|
@ -221,6 +211,7 @@ public class GoogleDriveController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//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)");
|
Files.List request = service.files().list().setQ("fileExtension = 'dat' and '"+folderID+"' in parents").setFields("nextPageToken, files(id, name, size, modifiedTime, createdTime)");
|
||||||
FileList files = request.execute();
|
FileList files = request.execute();
|
||||||
|
@ -231,9 +222,9 @@ public class GoogleDriveController {
|
||||||
// System.out.println(file.getName());
|
// System.out.println(file.getName());
|
||||||
// System.out.println(file.getModifiedTime()+"\n");
|
// System.out.println(file.getModifiedTime()+"\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//upload a file to the cloud from the local savegames folder
|
||||||
public void uploadFile(java.io.File uploadFile) throws IOException{
|
public void uploadFile(java.io.File uploadFile) throws IOException{
|
||||||
System.out.println("uploading ...");
|
System.out.println("uploading ...");
|
||||||
File fileMetadata = new File();
|
File fileMetadata = new File();
|
||||||
|
@ -245,19 +236,17 @@ public class GoogleDriveController {
|
||||||
|
|
||||||
FileContent mediaContent = new FileContent("", uploadFile);
|
FileContent mediaContent = new FileContent("", uploadFile);
|
||||||
File file = service.files().create(fileMetadata, mediaContent).setFields("id, parents").execute();
|
File file = service.files().create(fileMetadata, mediaContent).setFields("id, parents").execute();
|
||||||
// uploadedFile = file;
|
|
||||||
System.out.println("File ID: " + file.getId());
|
System.out.println("File ID: " + file.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//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.println("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("/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(downloadFile.getId());
|
// System.out.println("DownloadfileID: " + downloadFile.getId());
|
||||||
if(!directoryFile.exists()) {
|
if(!directoryFile.exists()) {
|
||||||
directoryFile.mkdir();
|
directoryFile.mkdir();
|
||||||
}
|
}
|
||||||
|
@ -267,6 +256,7 @@ public class GoogleDriveController {
|
||||||
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
|
||||||
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
|
//deleting old file
|
||||||
service.files().delete(oldFile.getId()).execute();
|
service.files().delete(oldFile.getId()).execute();
|
||||||
|
@ -279,7 +269,6 @@ public class GoogleDriveController {
|
||||||
|
|
||||||
FileContent mediaContent = new FileContent("", newFile);
|
FileContent mediaContent = new FileContent("", newFile);
|
||||||
File file = service.files().create(fileMetadata, mediaContent).setFields("id, parents").execute();
|
File file = service.files().create(fileMetadata, mediaContent).setFields("id, parents").execute();
|
||||||
// uploadedFile = file;
|
|
||||||
System.out.println("File ID: " + file.getId());
|
System.out.println("File ID: " + file.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue