From c83d794826b4476b1ca6e199a19a418bf7b27b8c Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 8 Jun 2023 19:38:51 +0200 Subject: [PATCH] Use the contentLength returned by the API for downloading --- .../github/libretube/api/obj/PipedStream.kt | 17 ++++++++++++- .../com/github/libretube/api/obj/Streams.kt | 24 ++----------------- .../libretube/extensions/ContentLength.kt | 4 ++-- .../libretube/services/DownloadService.kt | 5 ++-- 4 files changed, 23 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/com/github/libretube/api/obj/PipedStream.kt b/app/src/main/java/com/github/libretube/api/obj/PipedStream.kt index a07898791..908a38edc 100644 --- a/app/src/main/java/com/github/libretube/api/obj/PipedStream.kt +++ b/app/src/main/java/com/github/libretube/api/obj/PipedStream.kt @@ -1,5 +1,9 @@ package com.github.libretube.api.obj +import com.github.libretube.db.obj.DownloadItem +import com.github.libretube.enums.FileType +import com.github.libretube.helpers.ProxyHelper +import java.nio.file.Paths import kotlinx.serialization.Serializable @Serializable @@ -22,8 +26,19 @@ data class PipedStream( val audioTrackId: String? = null, val contentLength: Long = -1 ) { - fun getQualityString(fileName: String): String { + private fun getQualityString(fileName: String): String { return "${fileName}_${quality?.replace(" ", "_")}_$format." + mimeType?.split("/")?.last() } + + fun toDownloadItem(fileType: FileType, videoId: String, fileName: String) = DownloadItem( + type = fileType, + videoId = videoId, + fileName = getQualityString(fileName), + path = Paths.get(""), + url = url?.let { ProxyHelper.unwrapIfEnabled(it) }, + format = format, + quality = quality, + downloadSize = contentLength + ) } diff --git a/app/src/main/java/com/github/libretube/api/obj/Streams.kt b/app/src/main/java/com/github/libretube/api/obj/Streams.kt index 9306507a4..a6e24f917 100644 --- a/app/src/main/java/com/github/libretube/api/obj/Streams.kt +++ b/app/src/main/java/com/github/libretube/api/obj/Streams.kt @@ -50,34 +50,14 @@ data class Streams( val stream = videoStreams.find { it.quality == videoQuality && it.format == videoFormat } - items.add( - DownloadItem( - type = FileType.VIDEO, - videoId = videoId, - fileName = stream?.getQualityString(fileName).orEmpty(), - path = Paths.get(""), - url = stream?.url?.let { ProxyHelper.unwrapIfEnabled(it) }, - format = videoFormat, - quality = videoQuality, - ), - ) + stream?.toDownloadItem(FileType.VIDEO, videoId, fileName)?.let { items.add(it) } } if (!audioQuality.isNullOrEmpty() && !audioFormat.isNullOrEmpty()) { val stream = audioStreams.find { it.quality == audioQuality && it.format == audioFormat } - items.add( - DownloadItem( - type = FileType.AUDIO, - videoId = videoId, - fileName = stream?.getQualityString(fileName).orEmpty(), - path = Paths.get(""), - url = stream?.url?.let { ProxyHelper.unwrapIfEnabled(it) }, - format = audioFormat, - quality = audioQuality, - ), - ) + stream?.toDownloadItem(FileType.AUDIO, videoId, fileName)?.let { items.add(it) } } if (!subtitleCode.isNullOrEmpty()) { diff --git a/app/src/main/java/com/github/libretube/extensions/ContentLength.kt b/app/src/main/java/com/github/libretube/extensions/ContentLength.kt index 575497453..b7c69a147 100644 --- a/app/src/main/java/com/github/libretube/extensions/ContentLength.kt +++ b/app/src/main/java/com/github/libretube/extensions/ContentLength.kt @@ -5,7 +5,7 @@ import kotlinx.coroutines.withContext import java.net.HttpURLConnection import java.net.URL -suspend fun URL.getContentLength(def: Long = -1): Long { +suspend fun URL.getContentLength(): Long? { try { return withContext(Dispatchers.IO) { val connection = openConnection() as HttpURLConnection @@ -20,5 +20,5 @@ suspend fun URL.getContentLength(def: Long = -1): Long { } } catch (e: Exception) { e.printStackTrace() } - return def + return null } diff --git a/app/src/main/java/com/github/libretube/services/DownloadService.kt b/app/src/main/java/com/github/libretube/services/DownloadService.kt index 1a7345e5d..eef504add 100644 --- a/app/src/main/java/com/github/libretube/services/DownloadService.kt +++ b/app/src/main/java/com/github/libretube/services/DownloadService.kt @@ -167,8 +167,9 @@ class DownloadService : LifecycleService() { var totalRead = path.fileSize() val url = URL(item.url ?: return) - url.getContentLength().let { size -> - if (size > 0 && size != item.downloadSize) { + // only fetch the content length if it's not been returned by the API + if (item.downloadSize == 0L) { + url.getContentLength()?.takeIf { it != item.downloadSize }?.let { size -> item.downloadSize = size Database.downloadDao().updateDownloadItem(item) }