mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-15 06:40:30 +05:30
Merge pull request #3956 from Bnyro/master
Use the contentLength returned by the API for downloading
This commit is contained in:
commit
cef807fd1f
@ -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
|
||||
)
|
||||
}
|
||||
|
@ -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()) {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user