forked from CinemaMod/mcef
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
40 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,25 +26,27 @@ | |
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; | ||
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; | ||
import org.apache.commons.io.FileUtils; | ||
import org.apache.http.HttpEntity; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.apache.http.util.EntityUtils; | ||
|
||
import java.io.*; | ||
import java.net.HttpURLConnection; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
|
||
/** | ||
* A downloader and extraction tool for java-cef builds. | ||
* <p> | ||
* Downloads for <a href="https://github.com/CinemaMod/java-cef">CinemaMod java-cef</a> are provided by the CinemaMod Group unless changed | ||
* Downloads for <a href="https://github.com/CCBlueX/java-cef">CCBlueX MCEF java-cef</a> are provided by CCBlueX unless changed | ||
* in the MCEFSettings properties file; see {@link MCEFSettings}. | ||
* Email [email protected] for any questions or concerns regarding the file hosting. | ||
* Email [email protected] for any questions or concerns regarding the file hosting. | ||
*/ | ||
public class MCEFResourceManager { | ||
|
||
private static final String JAVA_CEF_DOWNLOAD_URL = | ||
"${host}/java-cef-builds/${java-cef-commit}/${platform}.tar.gz"; | ||
"${host}/mcef-cef/${java-cef-commit}/${platform}"; | ||
private static final String JAVA_CEF_CHECKSUM_DOWNLOAD_URL = | ||
"${host}/java-cef-builds/${java-cef-commit}/${platform}.tar.gz.sha256"; | ||
"${host}/mcef-cef/${java-cef-commit}/${platform}/checksum"; | ||
|
||
private final String host; | ||
private final String javaCefCommitHash; | ||
|
@@ -65,7 +67,6 @@ private MCEFResourceManager(String host, String javaCefCommitHash, MCEFPlatform | |
public File getPlatformDirectory() { | ||
return platformDirectory; | ||
} | ||
|
||
public File getCommitDirectory() { | ||
return commitDirectory; | ||
} | ||
|
@@ -220,45 +221,39 @@ private boolean compareChecksum(File checksumFile, File archiveFile) { | |
} | ||
|
||
private void downloadFile(String urlString, File outputFile, MCEFProgressTracker percentCompleteConsumer) throws IOException { | ||
try { | ||
MCEF.INSTANCE.getLogger().debug("Downloading '{}' to '{}'", urlString, outputFile.getCanonicalPath()); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Error getting canonical path for file", e); | ||
} | ||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) { | ||
HttpGet httpGet = new HttpGet(urlString); | ||
|
||
try { | ||
URL url = new URL(urlString); | ||
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); | ||
|
||
int responseCode = httpConn.getResponseCode(); | ||
if(responseCode != HttpURLConnection.HTTP_OK) { | ||
throw new RuntimeException("HTTP error code: " + responseCode); | ||
} | ||
httpClient.execute(httpGet, response -> { | ||
int status = response.getStatusLine().getStatusCode(); | ||
if (status < 200 || status >= 300) { | ||
throw new IOException("Unexpected response status: " + status); | ||
} | ||
|
||
int fileSize = httpConn.getContentLength(); | ||
if (fileSize <= 0) { | ||
throw new RuntimeException("Cannot read file size or file size is 0"); | ||
} | ||
HttpEntity entity = response.getEntity(); | ||
if (entity == null) { | ||
throw new IOException("No content returned from " + urlString); | ||
} | ||
|
||
try (BufferedInputStream inputStream = new BufferedInputStream(httpConn.getInputStream()); | ||
FileOutputStream outputStream = new FileOutputStream(outputFile)) { | ||
|
||
byte[] buffer = new byte[2048]; | ||
int bytesRead; | ||
int readBytes = 0; | ||
while ((bytesRead = inputStream.read(buffer)) != -1) { | ||
outputStream.write(buffer, 0, bytesRead); | ||
readBytes += bytesRead; | ||
float percentComplete = (float) readBytes / fileSize; | ||
percentCompleteConsumer.setProgress(percentComplete); | ||
long contentLength = entity.getContentLength(); | ||
try (InputStream inputStream = entity.getContent(); | ||
FileOutputStream outputStream = new FileOutputStream(outputFile)) { | ||
|
||
byte[] buffer = new byte[8192]; | ||
int bytesRead; | ||
long totalBytesRead = 0; | ||
while ((bytesRead = inputStream.read(buffer)) != -1) { | ||
outputStream.write(buffer, 0, bytesRead); | ||
totalBytesRead += bytesRead; | ||
if (contentLength > 0) { | ||
float percentComplete = (float) totalBytesRead / contentLength; | ||
percentCompleteConsumer.setProgress(percentComplete); | ||
} | ||
} | ||
} | ||
} catch (IOException e) { | ||
throw new IOException("Error writing to file from input stream", e); | ||
} | ||
} catch (MalformedURLException e) { | ||
throw new RuntimeException("Invalid URL format for " + urlString, e); | ||
} catch (IOException e) { | ||
throw new IOException("Error connecting to " + urlString, e); | ||
EntityUtils.consume(entity); | ||
return null; | ||
}); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters