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);
|
||||
colorPicker.setValue(Color.valueOf(getColor()));
|
||||
fullscreenToggleBtn.setSelected(isFullscreen());
|
||||
cloudSyncToggleBtn.setSelected(isCloudSync());
|
||||
edit.setDisable(true);
|
||||
applyColor();
|
||||
}
|
||||
|
|
|
@ -40,24 +40,21 @@ public class GoogleDriveController {
|
|||
private ArrayList<String> localSavegamesName = new ArrayList<>();
|
||||
private ArrayList<String> cloudSavegamesName = new ArrayList<>();
|
||||
|
||||
/** Application name. */
|
||||
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");
|
||||
|
||||
/** Global instance of the {@link FileDataStoreFactory}. */
|
||||
//Global instance of the {@link FileDataStoreFactory}
|
||||
private FileDataStoreFactory DATA_STORE_FACTORY;
|
||||
|
||||
/** Global instance of the JSON factory. */
|
||||
//Global instance of the JSON factory
|
||||
private final JsonFactory JSON_FACTORY =JacksonFactory.getDefaultInstance();
|
||||
|
||||
/** Global instance of the HTTP transport. */
|
||||
//Global instance of the 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
|
||||
*/
|
||||
private final java.util.Collection<String> SCOPES = DriveScopes.all();
|
||||
|
@ -99,7 +96,7 @@ public class GoogleDriveController {
|
|||
* @return an authorized Drive client service
|
||||
* @throws IOException
|
||||
*/
|
||||
public Drive getDriveService() throws IOException {
|
||||
public Drive getDriveService() throws IOException {
|
||||
Credential credential = authorize();
|
||||
return new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
|
||||
.setApplicationName(APPLICATION_NAME)
|
||||
|
@ -110,87 +107,79 @@ public class GoogleDriveController {
|
|||
public void main(String cemuDirectory) throws IOException {
|
||||
service = getDriveService();
|
||||
this.cemuDirectory = cemuDirectory;
|
||||
// sync();
|
||||
|
||||
// upload();
|
||||
// download();
|
||||
}
|
||||
|
||||
public void sync(String cemuDirectory) throws IOException {
|
||||
getLocalSavegames();
|
||||
// upload();
|
||||
if(!checkFolder()) {
|
||||
creatFolder();
|
||||
|
||||
for(int i = 0; i < localSavegames.size(); i++) {
|
||||
uploadFile(localSavegames.get(i));
|
||||
}
|
||||
} else {
|
||||
|
||||
getCloudSavegames();
|
||||
System.out.println(cloudSavegames.size()+"; "+localSavegames.size()+"; "+localSavegamesName.size());
|
||||
|
||||
//download files from cloud which don't exist locally
|
||||
for(int i = 0; i < cloudSavegames.size(); i++) {
|
||||
|
||||
// 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()));
|
||||
|
||||
// if the file exists locally check which one is newer
|
||||
if(localSavegamesName.contains(cloudSavegames.get(i).getName())) {
|
||||
|
||||
int localSavegamesNumber = localSavegamesName.indexOf(cloudSavegames.get(i).getName());
|
||||
// System.out.println(localSavegamesNumber);
|
||||
System.out.println(localSavegames.get(localSavegamesNumber).getName()+"; "+cloudSavegames.get(i).getName());
|
||||
long localModified = new DateTime(localSavegames.get(localSavegamesNumber).lastModified()).getValue();
|
||||
long cloudModified = cloudSavegames.get(i).getModifiedTime().getValue();
|
||||
|
||||
System.out.println(new Date(localModified)+"; "+new Date(cloudModified));
|
||||
if(localModified == cloudModified) {
|
||||
System.out.println("both files are the same, nothing to do \n");
|
||||
} else if(localModified >= cloudModified) {
|
||||
System.out.println("local is newer, going to upload local file \n");
|
||||
updateFile(cloudSavegames.get(i), localSavegames.get(localSavegamesNumber));
|
||||
} else {
|
||||
System.out.println("cloud is newer, going to download cloud file \n");
|
||||
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
|
||||
for(int j = 0; j < localSavegames.size(); j++) {
|
||||
if(!cloudSavegamesName.contains(localSavegamesName.get(j))) {
|
||||
System.out.println("upload "+localSavegames.get(j).getName()+" to the cloud");
|
||||
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]
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void creatFolder() throws IOException {
|
||||
public void sync(String cemuDirectory) throws IOException {
|
||||
getLocalSavegames();
|
||||
|
||||
if (!checkFolder()) {
|
||||
creatFolder();
|
||||
|
||||
for (int i = 0; i < localSavegames.size(); i++) {
|
||||
uploadFile(localSavegames.get(i));
|
||||
}
|
||||
} else {
|
||||
|
||||
getCloudSavegames();
|
||||
System.out.println(cloudSavegames.size() + "; " + localSavegames.size() + "; " + localSavegamesName.size());
|
||||
|
||||
// download files from cloud which don't exist locally
|
||||
for (int i = 0; i < cloudSavegames.size(); i++) {
|
||||
|
||||
// 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()));
|
||||
|
||||
// if the file exists locally, check which one is newer
|
||||
if (localSavegamesName.contains(cloudSavegames.get(i).getName())) {
|
||||
|
||||
int localSavegamesNumber = localSavegamesName.indexOf(cloudSavegames.get(i).getName());
|
||||
// System.out.println(localSavegamesNumber);
|
||||
System.out.println(localSavegames.get(localSavegamesNumber).getName() + "; " + cloudSavegames.get(i).getName());
|
||||
long localModified = new DateTime(localSavegames.get(localSavegamesNumber).lastModified()).getValue();
|
||||
long cloudModified = cloudSavegames.get(i).getModifiedTime().getValue();
|
||||
|
||||
System.out.println(new Date(localModified) + "; " + new Date(cloudModified));
|
||||
if (localModified == cloudModified) {
|
||||
System.out.println("both files are the same, nothing to do \n");
|
||||
} else if (localModified >= cloudModified) {
|
||||
System.out.println("local is newer, going to upload local file \n");
|
||||
updateFile(cloudSavegames.get(i), localSavegames.get(localSavegamesNumber));
|
||||
} else {
|
||||
System.out.println("cloud is newer, going to download cloud file \n");
|
||||
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
|
||||
for (int j = 0; j < localSavegames.size(); j++) {
|
||||
if (!cloudSavegamesName.contains(localSavegamesName.get(j))) {
|
||||
System.out.println("upload " + localSavegames.get(j).getName() + " to the cloud");
|
||||
uploadFile(localSavegames.get(j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//create a folder in google drive
|
||||
private void creatFolder() throws IOException {
|
||||
System.out.println("creating new folder");
|
||||
File fileMetadata = new File();
|
||||
fileMetadata.setName("cemu_savegames");
|
||||
fileMetadata.setMimeType("application/vnd.google-apps.folder");
|
||||
|
||||
File file = service.files().create(fileMetadata)
|
||||
.setFields("id")
|
||||
.execute();
|
||||
File file = service.files().create(fileMetadata).setFields("id").execute();
|
||||
System.out.println("Folder ID: " + file.getId());
|
||||
folderID = file.getId();
|
||||
}
|
||||
|
||||
private boolean checkFolder() {
|
||||
}
|
||||
|
||||
//check if folder already exist
|
||||
private boolean checkFolder() {
|
||||
try {
|
||||
Files.List request = service.files().list().setQ("mimeType = 'application/vnd.google-apps.folder' and name = 'cemu_savegames'");
|
||||
FileList files = request.execute();
|
||||
|
@ -203,38 +192,40 @@ public class GoogleDriveController {
|
|||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void getLocalSavegames() throws IOException {
|
||||
java.io.File dir = new java.io.File(cemuDirectory+"/mlc01/emulatorSave");
|
||||
String[] extensions = new String[] { "dat" };
|
||||
}
|
||||
|
||||
//reading all local savegames
|
||||
private void getLocalSavegames() throws IOException {
|
||||
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");
|
||||
List<java.io.File> files = (List<java.io.File>) FileUtils.listFiles(dir, extensions, true);
|
||||
for (java.io.File file : files) {
|
||||
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);
|
||||
for (java.io.File file : files) {
|
||||
localSavegamesName.add(file.getParentFile().getName()+"_"+file.getName());
|
||||
localSavegames.add(file);
|
||||
// System.out.println(file.getAbsolutePath());
|
||||
// System.out.println(file.getParentFile().getName());
|
||||
// System.out.println(file.lastModified());
|
||||
// System.out.println(file.getName()+"\n");
|
||||
}
|
||||
}
|
||||
|
||||
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)");
|
||||
FileList files = request.execute();
|
||||
}
|
||||
}
|
||||
|
||||
//reading all cloud savegames
|
||||
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)");
|
||||
FileList files = request.execute();
|
||||
|
||||
for (File file : files.getFiles()) {
|
||||
cloudSavegamesName.add(file.getName());
|
||||
cloudSavegames.add(file);
|
||||
for (File file : files.getFiles()) {
|
||||
cloudSavegamesName.add(file.getName());
|
||||
cloudSavegames.add(file);
|
||||
// System.out.println(file.getName());
|
||||
// System.out.println(file.getModifiedTime()+"\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void uploadFile(java.io.File uploadFile) throws IOException{
|
||||
}
|
||||
}
|
||||
|
||||
//upload a file to the cloud from the local savegames folder
|
||||
public void uploadFile(java.io.File uploadFile) throws IOException{
|
||||
System.out.println("uploading ...");
|
||||
File fileMetadata = new File();
|
||||
fileMetadata.setName(uploadFile.getParentFile().getName()+"_"+uploadFile.getName());
|
||||
|
@ -245,42 +236,40 @@ public class GoogleDriveController {
|
|||
|
||||
FileContent mediaContent = new FileContent("", uploadFile);
|
||||
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());
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void downloadFile(File downloadFile) throws IOException{
|
||||
System.out.println("downloading "+downloadFile.getName()+" ...");
|
||||
// String directory = 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());
|
||||
// System.out.println(cemuDirectory+"/mlc01/emulatorSave/"+ directory +"/"+ file);
|
||||
// System.out.println(downloadFile.getId());
|
||||
if(!directoryFile.exists()) {
|
||||
directoryFile.mkdir();
|
||||
}
|
||||
//download a file from the cloud to the local savegames folder
|
||||
private void downloadFile(File downloadFile) throws IOException{
|
||||
System.out.println("downloading "+downloadFile.getName()+" ...");
|
||||
// String directory = 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());
|
||||
// System.out.println(cemuDirectory+"/mlc01/emulatorSave/"+ directory +"/"+ file);
|
||||
// System.out.println("DownloadfileID: " + downloadFile.getId());
|
||||
if(!directoryFile.exists()) {
|
||||
directoryFile.mkdir();
|
||||
}
|
||||
|
||||
OutputStream outputStream = new FileOutputStream(directoryFile +"/"+ file); //TODO needs to be tested
|
||||
service.files().get(downloadFile.getId()).executeMediaAndDownloadTo(outputStream);
|
||||
System.out.println("done");
|
||||
}
|
||||
|
||||
private void updateFile(File oldFile, java.io.File newFile) throws IOException {
|
||||
//deleting old file
|
||||
service.files().delete(oldFile.getId()).execute();
|
||||
OutputStream outputStream = new FileOutputStream(directoryFile +"/"+ file); //TODO needs to be tested
|
||||
service.files().get(downloadFile.getId()).executeMediaAndDownloadTo(outputStream);
|
||||
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 {
|
||||
//deleting old file
|
||||
service.files().delete(oldFile.getId()).execute();
|
||||
|
||||
//uploading new file
|
||||
File fileMetadata = new File();
|
||||
fileMetadata.setName(newFile.getParentFile().getName()+"_"+newFile.getName());
|
||||
fileMetadata.setParents(Collections.singletonList(folderID));
|
||||
fileMetadata.setModifiedTime(new DateTime(newFile.lastModified()));
|
||||
//uploading new file
|
||||
File fileMetadata = new File();
|
||||
fileMetadata.setName(newFile.getParentFile().getName()+"_"+newFile.getName());
|
||||
fileMetadata.setParents(Collections.singletonList(folderID));
|
||||
fileMetadata.setModifiedTime(new DateTime(newFile.lastModified()));
|
||||
|
||||
FileContent mediaContent = new FileContent("", newFile);
|
||||
File file = service.files().create(fileMetadata, mediaContent).setFields("id, parents").execute();
|
||||
// uploadedFile = file;
|
||||
System.out.println("File ID: " + file.getId());
|
||||
FileContent mediaContent = new FileContent("", newFile);
|
||||
File file = service.files().create(fileMetadata, mediaContent).setFields("id, parents").execute();
|
||||
System.out.println("File ID: " + file.getId());
|
||||
}
|
||||
|
||||
public String getFolderID() {
|
||||
|
|
Loading…
Reference in New Issue