Skip to content

Commit

Permalink
Closed #9
Browse files Browse the repository at this point in the history
Updater now correctly follows redirects
  • Loading branch information
XDjackieXD committed Sep 18, 2016
1 parent 13775b0 commit 7eda662
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
49 changes: 48 additions & 1 deletion src/at/chaosfield/packupdate/FileManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package at.chaosfield.packupdate;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -95,7 +96,53 @@ public static BufferedReader getLocalFile(String fileName) throws IOException{

//Download a binary file to a given location
public static void downloadFile(String fileUrl, String destination) throws IOException{
FileUtils.copyURLToFile(new URL(fileUrl), new File(destination));
//FileUtils.copyURLToFile(new URL(fileUrl), new File(destination));

OutputStream outStream = null;
HttpURLConnection urlCon = null;

InputStream inStream = null;
try {
byte[] buf;
int byteRead, byteWritten = 0;
outStream = new BufferedOutputStream(new FileOutputStream(destination));

URL url, base, next;
String location;

while(true){
url = new URL(fileUrl);
urlCon = (HttpURLConnection) url.openConnection();
urlCon.setConnectTimeout(15000);
urlCon.setReadTimeout(15000);
urlCon.setInstanceFollowRedirects(false);

switch(urlCon.getResponseCode()){
case HttpURLConnection.HTTP_MOVED_PERM:
case HttpURLConnection.HTTP_MOVED_TEMP:
case 307:
location = urlCon.getHeaderField("Location");
base = new URL(fileUrl);
next = new URL(base, location);
fileUrl = next.toExternalForm();
continue;
}

break;
}

inStream = urlCon.getInputStream();
buf = new byte[1024];
while ((byteRead = inStream.read(buf)) != -1) {
outStream.write(buf, 0, byteRead);
byteWritten += byteRead;
}
}finally {
if(inStream != null)
inStream.close();
if(outStream != null)
outStream.close();
}
}

//Parse a PackInfo CSV file "name,version,download url,type"
Expand Down
1 change: 1 addition & 0 deletions src/at/chaosfield/packupdate/PackUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public void initRootLayout(){

Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();

((FxController) loader.getController()).setMain(this);
Expand Down
2 changes: 1 addition & 1 deletion src/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3
2.4

0 comments on commit 7eda662

Please sign in to comment.