2016-12-01 18:24:58 +01:00
/ * *
* updater for Project HomeFlix
* checks for Updates and downloads it in case there is one
* /
package application ;
import java.io.BufferedReader ;
2017-04-25 23:35:20 +02:00
import java.io.File ;
2016-12-01 18:24:58 +01:00
import java.io.IOException ;
import java.io.InputStreamReader ;
2017-04-25 23:35:20 +02:00
import java.net.HttpURLConnection ;
2016-12-01 18:24:58 +01:00
import java.net.URL ;
2017-04-25 23:35:20 +02:00
import javax.swing.ProgressMonitor ;
import javax.swing.ProgressMonitorInputStream ;
import org.apache.commons.io.FileUtils ;
2016-12-01 18:24:58 +01:00
2017-06-01 17:13:26 +02:00
import com.eclipsesource.json.Json ;
import com.eclipsesource.json.JsonArray ;
import com.eclipsesource.json.JsonObject ;
import com.eclipsesource.json.JsonValue ;
2017-03-25 16:20:48 +01:00
import javafx.application.Platform ;
2017-05-07 18:56:41 +02:00
public class updater implements Runnable {
2016-12-01 18:24:58 +01:00
2017-03-05 18:29:24 +01:00
private MainWindowController mainWindowController ;
private String buildNumber ;
2017-06-01 17:13:26 +02:00
private String apiOutput ;
private String updateBuildNumber ; //tag_name from Github
private String browserDownloadUrl ; //update download link
private String githubApi = " https://api.github.com/repos/Seil0/Project-HomeFlix/releases/latest " ;
2017-03-05 18:29:24 +01:00
2017-06-01 17:13:26 +02:00
public updater ( MainWindowController m , String buildNumber ) {
2016-12-01 18:24:58 +01:00
mainWindowController = m ;
2017-03-05 18:29:24 +01:00
this . buildNumber = buildNumber ;
2016-12-01 18:24:58 +01:00
}
2017-03-05 18:29:24 +01:00
public void run ( ) {
2016-12-01 18:24:58 +01:00
System . out . println ( " check for updates ... " ) ;
2017-03-25 16:20:48 +01:00
Platform . runLater ( ( ) - > {
mainWindowController . updateBtn . setText ( mainWindowController . bundle . getString ( " checkingUpdates " ) ) ;
} ) ;
2017-06-01 17:13:26 +02:00
try {
URL githubApiUrl = new URL ( githubApi ) ;
BufferedReader ina = new BufferedReader ( new InputStreamReader ( githubApiUrl . openStream ( ) ) ) ;
apiOutput = ina . readLine ( ) ;
ina . close ( ) ;
2016-12-01 18:24:58 +01:00
} catch ( IOException e1 ) {
2017-06-01 17:41:06 +02:00
Platform . runLater ( ( ) - > {
mainWindowController . showErrorMsg ( mainWindowController . errorUpdateV , e1 ) ;
} ) ;
2016-12-01 18:24:58 +01:00
}
2017-06-01 17:13:26 +02:00
JsonObject object = Json . parse ( apiOutput ) . asObject ( ) ;
JsonArray objectAssets = Json . parse ( apiOutput ) . asObject ( ) . get ( " assets " ) . asArray ( ) ;
updateBuildNumber = object . getString ( " tag_name " , " " ) ;
// updateName = object.getString("name", "");
// updateChanges = object.getString("body", "");
for ( JsonValue asset : objectAssets ) {
browserDownloadUrl = asset . asObject ( ) . getString ( " browser_download_url " , " " ) ;
}
2017-03-25 16:20:48 +01:00
System . out . println ( " Build: " + buildNumber + " , Update: " + updateBuildNumber ) ;
2016-12-01 18:24:58 +01:00
2017-03-25 16:20:48 +01:00
//Compares the program BuildNumber with the current BuildNumber if program BuildNumber < current BuildNumber then perform a update
2016-12-01 18:24:58 +01:00
int iversion = Integer . parseInt ( buildNumber ) ;
2017-03-25 16:20:48 +01:00
int iaktVersion = Integer . parseInt ( updateBuildNumber . replace ( " . " , " " ) ) ;
2016-12-01 18:24:58 +01:00
if ( iversion > = iaktVersion ) {
2017-03-25 16:20:48 +01:00
Platform . runLater ( ( ) - > {
mainWindowController . updateBtn . setText ( mainWindowController . bundle . getString ( " updateBtnNotavail " ) ) ;
} ) ;
2016-12-01 18:24:58 +01:00
System . out . println ( " no update available " ) ;
} else {
2017-03-25 16:20:48 +01:00
Platform . runLater ( ( ) - > {
mainWindowController . updateBtn . setText ( mainWindowController . bundle . getString ( " updateBtnavail " ) ) ;
} ) ;
2016-12-01 18:24:58 +01:00
System . out . println ( " update available " ) ;
2017-06-01 17:13:26 +02:00
System . out . println ( " download link: " + browserDownloadUrl ) ;
2017-06-01 17:31:15 +02:00
try {
2017-04-25 23:35:20 +02:00
//open new Http connection, ProgressMonitorInputStream for downloading the data
2017-06-01 17:31:15 +02:00
HttpURLConnection conn = ( HttpURLConnection ) new URL ( browserDownloadUrl ) . openConnection ( ) ;
2017-04-25 23:35:20 +02:00
ProgressMonitorInputStream pmis = new ProgressMonitorInputStream ( null , " Downloading... " , conn . getInputStream ( ) ) ;
ProgressMonitor pm = pmis . getProgressMonitor ( ) ;
pm . setMillisToDecideToPopup ( 0 ) ;
pm . setMillisToPopup ( 0 ) ;
pm . setMinimum ( 0 ) ; // tell the progress bar that we start at the beginning of the stream
pm . setMaximum ( conn . getContentLength ( ) ) ; // tell the progress bar the total number of bytes we are going to read.
2017-06-01 18:18:49 +02:00
FileUtils . copyInputStreamToFile ( pmis , new File ( " ProjectHomeFlix_update.jar " ) ) ; //download update
org . apache . commons . io . FileUtils . copyFile ( new File ( " ProjectHomeFlix_update.jar " ) , new File ( " ProjectHomeFlix.jar " ) ) ; //TODO rename update to old name
org . apache . commons . io . FileUtils . deleteQuietly ( new File ( " ProjectHomeFlix_update.jar " ) ) ; //delete update
2017-03-05 18:29:24 +01:00
Runtime . getRuntime ( ) . exec ( " java -jar ProjectHomeFlix.jar " ) ; //start again
System . exit ( 0 ) ; //finishes itself
} catch ( IOException e ) {
2017-06-01 17:13:26 +02:00
Platform . runLater ( ( ) - > {
mainWindowController . showErrorMsg ( mainWindowController . errorUpdateD , e ) ;
} ) ;
2017-03-05 18:29:24 +01:00
}
2016-12-01 18:24:58 +01:00
}
}
}