11package file
22
33import global.Log
4-
5-
64import logging.KotlinLogging
7- import ui.MainWindow
8- import workers.DownloadWithProgressWorker
95import java.io.BufferedInputStream
106import java.io.BufferedOutputStream
117import java.io.FileOutputStream
@@ -14,64 +10,56 @@ import java.net.HttpURLConnection
1410import java.net.URL
1511import java.nio.file.Files
1612import java.nio.file.Path
13+
1714object Download {
1815 private val log = KotlinLogging .logger {}
1916
2017 private const val compilerReleaseBaseUrl = " https://github.com/wurstscript/WurstScript/releases/download/nightly/"
2118 private const val bareboneUrl = " github.com/wurstscript/wurst-project-template/archive/master.zip"
2219
23- @Throws(IOException ::class )
24- private fun downloadFile (filePath : String , callback : (Path ) -> Unit ) {
25- if (SetupApp .setup.isGUILaunch) {
26- DownloadWithProgressWorker (filePath, MainWindow .ui.progressBar, callback).execute()
27- } else {
28- downloadDirect(filePath, callback)
29- }
30- }
31-
32- fun getHttpURLConnection (filePath : String ): HttpURLConnection {
33- val url = URL (filePath)
34- val httpConnection = url.openConnection() as HttpURLConnection
35- httpConnection.connectTimeout = 14000
36- httpConnection.readTimeout = 20000
37- httpConnection.addRequestProperty(" User-Agent" , " Chrome" )
38- return httpConnection
39- }
40-
4120 @Throws(IOException ::class )
4221 fun downloadSetup (callback : (Path ) -> Unit ) {
4322 throw UnsupportedOperationException (" Standalone grill updates are not currently published via GitHub releases." )
4423 }
4524
4625 @Throws(IOException ::class )
4726 fun downloadCompiler (callback : (Path ) -> Unit ) {
48- downloadFile (compilerReleaseBaseUrl + getCompilerArchiveName(), callback)
27+ downloadDirect (compilerReleaseBaseUrl + getCompilerArchiveName(), callback)
4928 }
5029
5130 @Throws(IOException ::class )
5231 fun downloadBareboneProject (callback : (Path ) -> Unit ) {
5332 try {
54- downloadFile (" https://$bareboneUrl " , callback)
33+ downloadDirect (" https://$bareboneUrl " , callback)
5534 } catch (e: Exception ) {
56- log.warn( " downloadBareboneProject Exception caught" , e)
35+ log.warn(" downloadBareboneProject Exception caught" , e)
5736 Log .println (" Https error, falling back to unsafe http." )
58- downloadFile (" http://$bareboneUrl " , callback)
37+ downloadDirect (" http://$bareboneUrl " , callback)
5938 }
6039 }
6140
41+ fun getHttpURLConnection (filePath : String ): HttpURLConnection {
42+ val url = URL (filePath)
43+ val httpConnection = url.openConnection() as HttpURLConnection
44+ httpConnection.connectTimeout = 14000
45+ httpConnection.readTimeout = 20000
46+ httpConnection.addRequestProperty(" User-Agent" , " Chrome" )
47+ return httpConnection
48+ }
49+
6250 private fun downloadDirect (filePath : String , callback : (Path ) -> Unit ) {
63- val httpConnection = getHttpURLConnection(filePath)
64- val completeFileSize = httpConnection.contentLength
65- val size = completeFileSize / 1024 / 1024
66- log.info(" \t \uD83D\uDCE5 (" + (if (size == 0 ) " <1" else size) + " MB)" )
67- val input = java.io. BufferedInputStream (httpConnection.inputStream)
68- val downloadedFile = createDownloadTempFile(filePath)
51+ val httpConnection = getHttpURLConnection(filePath)
52+ val completeFileSize = httpConnection.contentLength
53+ val size = completeFileSize / 1024 / 1024
54+ log.info(" \t 📥 (" + (if (size == 0 ) " <1" else size) + " MB)" )
55+ val input = BufferedInputStream (httpConnection.inputStream)
56+ val downloadedFile = createDownloadTempFile(filePath)
6957
70- readStream(downloadedFile, input)
58+ readStream(downloadedFile, input)
7159
72- input.close()
73- callback.invoke(downloadedFile)
74- }
60+ input.close()
61+ callback.invoke(downloadedFile)
62+ }
7563
7664 private fun readStream (destination : Path , input : BufferedInputStream ) {
7765 FileOutputStream (destination.toFile()).use { fos ->
0 commit comments