2017-04-17 00:58:49 +02:00
/ * *
2017-04-19 02:15:44 +02:00
* cemu_UI
*
* Copyright 2017 < @Seil0 >
*
2017-10-15 13:37:45 +02:00
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
2017-04-19 02:15:44 +02:00
*
2017-10-15 13:37:45 +02:00
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston ,
* MA 02110 - 1301 , USA .
2017-04-17 00:58:49 +02:00
* /
2017-10-15 13:37:45 +02:00
2017-11-13 16:44:39 +01:00
package com.cemu_UI.controller ;
2017-04-17 00:58:49 +02:00
import java.io.File ;
2017-12-12 23:52:54 +01:00
import java.io.FileInputStream ;
import java.io.FileOutputStream ;
2017-04-17 00:58:49 +02:00
import java.io.IOException ;
2017-12-12 23:52:54 +01:00
import java.time.Instant ;
import java.util.zip.ZipEntry ;
import java.util.zip.ZipOutputStream ;
2017-04-17 00:58:49 +02:00
2017-09-12 15:04:21 +02:00
import org.apache.logging.log4j.LogManager ;
import org.apache.logging.log4j.Logger ;
2017-11-13 16:44:39 +01:00
import com.cemu_UI.application.MainWindowController ;
import com.cemu_UI.vendorCloudController.GoogleDriveController ;
2017-05-07 19:57:55 +02:00
import javafx.application.Platform ;
2017-12-12 23:52:54 +01:00
import net.lingala.zip4j.core.ZipFile ;
import net.lingala.zip4j.exception.ZipException ;
2017-04-17 00:58:49 +02:00
public class CloudController {
2017-11-13 16:44:39 +01:00
public CloudController ( MainWindowController mwc ) {
this . mwc = mwc ;
2017-04-17 00:58:49 +02:00
}
2017-11-13 16:44:39 +01:00
private MainWindowController mwc ;
2017-04-17 00:58:49 +02:00
private GoogleDriveController googleDriveController = new GoogleDriveController ( ) ;
2017-09-12 15:04:21 +02:00
private static final Logger LOGGER = LogManager . getLogger ( CloudController . class . getName ( ) ) ;
2017-04-17 00:58:49 +02:00
2017-11-13 16:44:39 +01:00
public boolean initializeConnection ( String cloudService , String cemuDirectory ) {
2017-10-29 23:27:36 +01:00
boolean success = false ;
2017-09-12 15:04:21 +02:00
LOGGER . info ( " sartting cloud initialisation ... " ) ;
2017-10-29 23:27:36 +01:00
2017-05-25 19:07:16 +02:00
if ( cloudService . equals ( " GoogleDrive " ) ) {
2017-09-12 15:04:21 +02:00
LOGGER . info ( " selected service is Google Drive " ) ;
2017-04-17 00:58:49 +02:00
try {
googleDriveController . main ( cemuDirectory ) ;
} catch ( IOException e ) {
2017-09-12 15:04:21 +02:00
LOGGER . error ( " error while initialize connection " , e ) ;
2017-10-29 23:27:36 +01:00
return success ;
2017-04-17 00:58:49 +02:00
}
2017-12-07 22:52:32 +01:00
success = true ;
2017-04-17 00:58:49 +02:00
}
2017-10-29 23:27:36 +01:00
2017-05-25 19:07:16 +02:00
if ( cloudService . equals ( " Dropbox " ) ) {
2017-09-12 15:04:21 +02:00
LOGGER . info ( " selected service is Dropbox " ) ;
2017-04-17 00:58:49 +02:00
}
2017-09-12 15:04:21 +02:00
LOGGER . info ( " cloud initialisation done! " ) ;
2017-10-29 23:27:36 +01:00
return success ;
2017-04-17 00:58:49 +02:00
}
2017-12-12 23:52:54 +01:00
/ * *
* to trigger a new sync set the mwc LastLocalSync to the actual time and call the sync method
* @param cloudService
* @param cemuDirectory
* @param cemu_UIDirectory
* /
public void sync ( String cloudService , String cemuDirectory , String cemu_UIDirectory ) {
// running sync in a new thread, instead of blocking the main thread
2017-05-07 19:57:55 +02:00
Thread thread = new Thread ( new Runnable ( ) {
2017-08-30 11:29:58 +02:00
@Override
2017-05-07 19:57:55 +02:00
public void run ( ) {
2017-12-12 23:52:54 +01:00
try {
Platform . runLater ( ( ) - > {
mwc . getPlayBtn ( ) . setDisable ( true ) ;
mwc . getPlayBtn ( ) . setText ( " syncing... " ) ;
} ) ;
LOGGER . info ( " starting synchronization in new thread ... " ) ;
// zip the saves folder
File zipFile = zipSavegames ( cemu_UIDirectory , cemuDirectory ) ;
// upload the zip
switch ( cloudService ) {
// use GoogleDriveController
case " GoogleDrive " :
LOGGER . info ( " using GoogleDriveController " ) ;
long lastCloudSync = googleDriveController . getLastCloudSync ( ) ;
if ( ! googleDriveController . checkFolder ( ) ) {
LOGGER . info ( " cloud sync folder dosen't exist, creating one! " ) ;
googleDriveController . creatFolder ( ) ;
googleDriveController . uploadZipFile ( zipFile ) ;
} else if ( mwc . getLastLocalSync ( ) > lastCloudSync ) {
LOGGER . info ( " local is new, going to upload zip " ) ;
googleDriveController . uploadZipFile ( zipFile ) ;
} else if ( mwc . getLastLocalSync ( ) < lastCloudSync ) {
LOGGER . info ( " cloud is new, going to download zip " ) ;
unzipSavegames ( cemuDirectory , googleDriveController . downloadZipFile ( cemu_UIDirectory ) ) ;
mwc . setLastLocalSync ( lastCloudSync ) ;
break ;
} else {
LOGGER . info ( " nothing to do " ) ;
break ;
}
mwc . setLastLocalSync ( Long . parseLong ( zipFile . getName ( ) . substring ( 0 , zipFile . getName ( ) . length ( ) - 4 ) ) ) ; // set time of last sucessfull sync
break ;
case " Dropbox " :
break ;
default :
LOGGER . warn ( " no cloud vendor found! " ) ;
break ;
}
zipFile . delete ( ) ; // delete zipfile in cem_UI directory
Platform . runLater ( ( ) - > {
mwc . getPlayBtn ( ) . setText ( " play " ) ;
mwc . getPlayBtn ( ) . setDisable ( false ) ;
mwc . saveSettings ( ) ;
} ) ;
LOGGER . info ( " synchronization successful! " ) ;
} catch ( Exception e ) {
// TODO: handle exception
}
}
} ) ;
2017-05-07 19:57:55 +02:00
thread . start ( ) ;
2017-04-17 00:58:49 +02:00
}
2017-12-12 23:52:54 +01:00
private File zipSavegames ( String cemu_UIDirectory , String cemuDirectory ) throws Exception {
long unixTimestamp = Instant . now ( ) . getEpochSecond ( ) ;
FileOutputStream fos = new FileOutputStream ( cemu_UIDirectory + " / " + unixTimestamp + " .zip " ) ;
ZipOutputStream zos = new ZipOutputStream ( fos ) ;
addDirToZipArchive ( zos , new File ( cemuDirectory + " /mlc01/usr/save " ) , null ) ;
zos . flush ( ) ;
fos . flush ( ) ;
zos . close ( ) ;
fos . close ( ) ;
return new File ( cemu_UIDirectory + " / " + unixTimestamp + " .zip " ) ;
2017-04-17 00:58:49 +02:00
}
2017-12-12 23:52:54 +01:00
private static void addDirToZipArchive ( ZipOutputStream zos , File fileToZip , String parrentDirectoryName ) throws Exception {
if ( fileToZip = = null | | ! fileToZip . exists ( ) ) {
return ;
}
2017-04-17 00:58:49 +02:00
2017-12-12 23:52:54 +01:00
String zipEntryName = fileToZip . getName ( ) ;
if ( parrentDirectoryName ! = null & & ! parrentDirectoryName . isEmpty ( ) ) {
zipEntryName = parrentDirectoryName + " / " + fileToZip . getName ( ) ;
}
if ( fileToZip . isDirectory ( ) ) {
// System.out.println("+" + zipEntryName);
for ( File file : fileToZip . listFiles ( ) ) {
addDirToZipArchive ( zos , file , zipEntryName ) ;
}
} else {
// System.out.println(" " + zipEntryName);
byte [ ] buffer = new byte [ 1024 ] ;
FileInputStream fis = new FileInputStream ( fileToZip ) ;
zos . putNextEntry ( new ZipEntry ( zipEntryName ) ) ;
int length ;
while ( ( length = fis . read ( buffer ) ) > 0 ) {
zos . write ( buffer , 0 , length ) ;
}
zos . closeEntry ( ) ;
fis . close ( ) ;
}
}
private void unzipSavegames ( String cemuDirectory , File outputFile ) {
try {
ZipFile zipFile = new ZipFile ( outputFile ) ;
zipFile . extractAll ( cemuDirectory + " /mlc01/usr " ) ;
outputFile . delete ( ) ;
LOGGER . info ( " unzip successfull " ) ;
} catch ( ZipException e ) {
LOGGER . error ( " an error occurred during unziping the file! " , e ) ;
2017-04-17 00:58:49 +02:00
}
}
2017-12-12 23:52:54 +01:00
2017-12-13 00:07:20 +01:00
public String getFolderID ( String cloudService ) {
String folderID = " " ;
if ( cloudService ! = null ) {
if ( cloudService . equals ( " GoogleDrive " ) ) {
folderID = googleDriveController . getFolderID ( ) ;
}
if ( cloudService . equals ( " Dropbox " ) ) {
}
}
return folderID ;
}
public void setFolderID ( String folderID , String cloudService ) {
if ( cloudService ! = null ) {
if ( cloudService . equals ( " GoogleDrive " ) ) {
googleDriveController . setFolderID ( folderID ) ;
}
if ( cloudService . equals ( " Dropbox " ) ) {
}
}
}
2017-04-17 00:58:49 +02:00
}