mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-16 23:30:29 +05:30
24 lines
716 B
Kotlin
24 lines
716 B
Kotlin
|
package com.github.libretube.extensions
|
||
|
|
||
|
import kotlinx.coroutines.Dispatchers
|
||
|
import kotlinx.coroutines.withContext
|
||
|
import java.net.HttpURLConnection
|
||
|
import java.net.URL
|
||
|
|
||
|
suspend fun URL.getContentLength(def: Long = -1): Long {
|
||
|
try {
|
||
|
return withContext(Dispatchers.IO) {
|
||
|
val con = openConnection() as HttpURLConnection
|
||
|
con.setRequestProperty("Range", "bytes=0-")
|
||
|
|
||
|
val value = con.getHeaderField("content-length")
|
||
|
// If connection accepts range header, try to get total bytes
|
||
|
?: con.getHeaderField("content-range").split("/")[1]
|
||
|
|
||
|
value.toLong()
|
||
|
}
|
||
|
} catch (e: Exception) { e.printStackTrace() }
|
||
|
|
||
|
return def
|
||
|
}
|