Show download size on videos (#1038)

This commit is contained in:
CranberrySoup 2024-04-13 22:45:58 +00:00 committed by GitHub
parent afdc4988ac
commit aa8972870c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 30 additions and 5 deletions

View file

@ -5,6 +5,7 @@ import android.content.*
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.text.format.Formatter.formatFileSize
import android.util.Log import android.util.Log
import android.widget.Toast import android.widget.Toast
import androidx.annotation.MainThread import androidx.annotation.MainThread
@ -20,7 +21,6 @@ import com.lagradost.cloudstream3.APIHolder.apis
import com.lagradost.cloudstream3.APIHolder.getId import com.lagradost.cloudstream3.APIHolder.getId
import com.lagradost.cloudstream3.APIHolder.unixTime import com.lagradost.cloudstream3.APIHolder.unixTime
import com.lagradost.cloudstream3.APIHolder.unixTimeMS import com.lagradost.cloudstream3.APIHolder.unixTimeMS
import com.lagradost.cloudstream3.AcraApplication.Companion.context
import com.lagradost.cloudstream3.AcraApplication.Companion.setKey import com.lagradost.cloudstream3.AcraApplication.Companion.setKey
import com.lagradost.cloudstream3.CommonActivity.activity import com.lagradost.cloudstream3.CommonActivity.activity
import com.lagradost.cloudstream3.CommonActivity.getCastSession import com.lagradost.cloudstream3.CommonActivity.getCastSession
@ -1280,9 +1280,14 @@ class ResultViewModel2 : ViewModel() {
callback: (Pair<LinkLoadingResult, Int>) -> Unit, callback: (Pair<LinkLoadingResult, Int>) -> Unit,
) { ) {
loadLinks(result, isVisible = true, type) { links -> loadLinks(result, isVisible = true, type) { links ->
// Could not find a better way to do this
val context = AcraApplication.context
postPopup( postPopup(
text, text,
links.links.map { txt("${it.name} ${Qualities.getStringByInt(it.quality)}") }) { links.links.apmap {
val size = it.getVideoSize()?.let { size -> " " + formatFileSize(context, size) } ?: ""
txt("${it.name} ${Qualities.getStringByInt(it.quality)}$size")
}) {
callback.invoke(links to (it ?: return@postPopup)) callback.invoke(links to (it ?: return@postPopup))
} }
} }

View file

@ -404,8 +404,28 @@ open class ExtractorLink constructor(
open val extractorData: String? = null, open val extractorData: String? = null,
open val type: ExtractorLinkType, open val type: ExtractorLinkType,
) : VideoDownloadManager.IDownloadableMinimum { ) : VideoDownloadManager.IDownloadableMinimum {
val isM3u8 : Boolean get() = type == ExtractorLinkType.M3U8 val isM3u8: Boolean get() = type == ExtractorLinkType.M3U8
val isDash : Boolean get() = type == ExtractorLinkType.DASH val isDash: Boolean get() = type == ExtractorLinkType.DASH
// Cached video size
private var videoSize: Long? = null
/**
* Get video size in bytes with one head request. Only available for ExtractorLinkType.Video
* @param timeoutSeconds timeout of the head request.
*/
suspend fun getVideoSize(timeoutSeconds: Long = 3L): Long? {
// Content-Length is not applicable to other types of formats
if (this.type != ExtractorLinkType.VIDEO) return null
videoSize = videoSize ?: runCatching {
val response =
app.head(this.url, headers = headers, referer = referer, timeout = timeoutSeconds)
response.headers["Content-Length"]?.toLong()
}.getOrNull()
return videoSize
}
@JsonIgnore @JsonIgnore
fun getAllHeaders() : Map<String, String> { fun getAllHeaders() : Map<String, String> {