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();
|
||||||
|
@ -99,7 +96,7 @@ public class GoogleDriveController {
|
||||||
* @return an authorized Drive client service
|
* @return an authorized Drive client service
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public Drive getDriveService() throws IOException {
|
public Drive getDriveService() throws IOException {
|
||||||
Credential credential = authorize();
|
Credential credential = authorize();
|
||||||
return new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
|
return new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
|
||||||
.setApplicationName(APPLICATION_NAME)
|
.setApplicationName(APPLICATION_NAME)
|
||||||
|
@ -110,87 +107,79 @@ 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 {
|
||||||
System.out.println("cloud is newer, going to download cloud file \n");
|
System.out.println("cloud is newer, going to download cloud file \n");
|
||||||
downloadFile(cloudSavegames.get(i));
|
downloadFile(cloudSavegames.get(i));
|
||||||
}
|
}
|
||||||
} 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean checkFolder() {
|
//check if folder already exist
|
||||||
|
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'");
|
||||||
FileList files = request.execute();
|
FileList files = request.execute();
|
||||||
|
@ -203,38 +192,40 @@ public class GoogleDriveController {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void getLocalSavegames() throws IOException {
|
//reading all local savegames
|
||||||
java.io.File dir = new java.io.File(cemuDirectory+"/mlc01/emulatorSave");
|
private void getLocalSavegames() throws IOException {
|
||||||
String[] extensions = new String[] { "dat" };
|
java.io.File dir = new java.io.File(cemuDirectory+"/mlc01/emulatorSave");
|
||||||
|
String[] extensions = new String[] { "dat" };
|
||||||
|
|
||||||
System.out.println("Getting all .dat files in " + dir.getCanonicalPath()+" including those in subdirectories \n");
|
System.out.println("Getting all .dat files in " + dir.getCanonicalPath()+" including those in subdirectories \n");
|
||||||
List<java.io.File> files = (List<java.io.File>) FileUtils.listFiles(dir, extensions, true);
|
List<java.io.File> files = (List<java.io.File>) FileUtils.listFiles(dir, extensions, true);
|
||||||
for (java.io.File file : files) {
|
for (java.io.File file : files) {
|
||||||
localSavegamesName.add(file.getParentFile().getName()+"_"+file.getName());
|
localSavegamesName.add(file.getParentFile().getName()+"_"+file.getName());
|
||||||
localSavegames.add(file);
|
localSavegames.add(file);
|
||||||
// System.out.println(file.getAbsolutePath());
|
// System.out.println(file.getAbsolutePath());
|
||||||
// System.out.println(file.getParentFile().getName());
|
// System.out.println(file.getParentFile().getName());
|
||||||
// System.out.println(file.lastModified());
|
// System.out.println(file.lastModified());
|
||||||
// System.out.println(file.getName()+"\n");
|
// System.out.println(file.getName()+"\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void getCloudSavegames() throws IOException {
|
//reading all cloud savegames
|
||||||
Files.List request = service.files().list().setQ("fileExtension = 'dat' and '"+folderID+"' in parents").setFields("nextPageToken, files(id, name, size, modifiedTime, createdTime)");
|
private void getCloudSavegames() throws IOException {
|
||||||
FileList files = request.execute();
|
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();
|
||||||
|
|
||||||
for (File file : files.getFiles()) {
|
for (File file : files.getFiles()) {
|
||||||
cloudSavegamesName.add(file.getName());
|
cloudSavegamesName.add(file.getName());
|
||||||
cloudSavegames.add(file);
|
cloudSavegames.add(file);
|
||||||
// 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();
|
||||||
fileMetadata.setName(uploadFile.getParentFile().getName()+"_"+uploadFile.getName());
|
fileMetadata.setName(uploadFile.getParentFile().getName()+"_"+uploadFile.getName());
|
||||||
|
@ -245,42 +236,40 @@ 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("DownloadfileID: " + downloadFile.getId());
|
||||||
// System.out.println(downloadFile.getId());
|
if(!directoryFile.exists()) {
|
||||||
if(!directoryFile.exists()) {
|
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateFile(File oldFile, java.io.File newFile) throws IOException {
|
//update a file in the cloud, by deleting the old one and uploading an new with the same id
|
||||||
//deleting old file
|
private void updateFile(File oldFile, java.io.File newFile) throws IOException {
|
||||||
service.files().delete(oldFile.getId()).execute();
|
//deleting old file
|
||||||
|
service.files().delete(oldFile.getId()).execute();
|
||||||
|
|
||||||
//uploading new file
|
//uploading new file
|
||||||
File fileMetadata = new File();
|
File fileMetadata = new File();
|
||||||
fileMetadata.setName(newFile.getParentFile().getName()+"_"+newFile.getName());
|
fileMetadata.setName(newFile.getParentFile().getName()+"_"+newFile.getName());
|
||||||
fileMetadata.setParents(Collections.singletonList(folderID));
|
fileMetadata.setParents(Collections.singletonList(folderID));
|
||||||
fileMetadata.setModifiedTime(new DateTime(newFile.lastModified()));
|
fileMetadata.setModifiedTime(new DateTime(newFile.lastModified()));
|
||||||
|
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFolderID() {
|
public String getFolderID() {
|
||||||
|
|
Loading…
Reference in New Issue