dependencies: Check if a server support resuming downloading
This commit is contained in:
@@ -70,48 +70,32 @@ class DependencyDownloader(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun tryHttpDownload(originalUrl: URL, connection: HttpURLConnection, tmpFile: File) {
|
private fun resumeDownload(originalUrl: URL, originalConnection: HttpURLConnection, tmpFile: File) {
|
||||||
@Suppress("NAME_SHADOWING")
|
originalConnection.connect()
|
||||||
var connection = connection
|
val totalBytes = originalConnection.contentLengthLong
|
||||||
connection.connect()
|
val currentBytes = tmpFile.length()
|
||||||
val totalBytes = connection.contentLengthLong
|
if (currentBytes >= totalBytes || originalConnection.getHeaderField("Accept-Ranges") != "bytes") {
|
||||||
var currentBytes = 0L
|
// The temporary file is bigger then expected or the server doesn't support resuming downloading.
|
||||||
if (tmpFile.exists()) {
|
// Download the file from scratch.
|
||||||
currentBytes = tmpFile.length()
|
doDownload(originalUrl, originalConnection, tmpFile, 0, totalBytes, false)
|
||||||
if (currentBytes < totalBytes) {
|
} else {
|
||||||
connection.disconnect()
|
originalConnection.disconnect()
|
||||||
connection = originalUrl.openConnection() as HttpURLConnection
|
val rangeConnection = originalUrl.openConnection() as HttpURLConnection
|
||||||
connection.setRequestProperty("range", "bytes=$currentBytes-")
|
rangeConnection.setRequestProperty("range", "bytes=$currentBytes-")
|
||||||
connection.connect()
|
rangeConnection.connect()
|
||||||
} else {
|
doDownload(originalUrl, rangeConnection, tmpFile, currentBytes, totalBytes, true)
|
||||||
currentBytes = 0
|
|
||||||
tmpFile.delete()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
doDownload(originalUrl, connection, tmpFile, currentBytes, totalBytes, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun tryOtherDownload(originalUrl: URL, connection: URLConnection, tmpFile: File) {
|
|
||||||
connection.connect()
|
|
||||||
val currentBytes = 0L
|
|
||||||
val totalBytes = connection.contentLengthLong
|
|
||||||
|
|
||||||
try {
|
|
||||||
doDownload(originalUrl, connection, tmpFile, currentBytes, totalBytes, false)
|
|
||||||
} catch(e: Throwable) {
|
|
||||||
tmpFile.delete()
|
|
||||||
throw e
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Performs an attempt to download a specified file into the specified location */
|
/** Performs an attempt to download a specified file into the specified location */
|
||||||
private fun tryDownload(url: URL, tmpFile: File) {
|
private fun tryDownload(url: URL, tmpFile: File) {
|
||||||
val connection = url.openConnection()
|
val connection = url.openConnection()
|
||||||
if (connection is HttpURLConnection) {
|
if (connection is HttpURLConnection && tmpFile.exists()) {
|
||||||
tryHttpDownload(url, connection, tmpFile)
|
resumeDownload(url, connection, tmpFile)
|
||||||
} else {
|
} else {
|
||||||
tryOtherDownload(url, connection, tmpFile)
|
connection.connect()
|
||||||
|
val totalBytes = connection.contentLengthLong
|
||||||
|
doDownload(url, connection, tmpFile, 0, totalBytes, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user