mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 22:30: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
|
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
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
@ -22,8 +26,19 @@ data class PipedStream(
|
|||||||
val audioTrackId: String? = null,
|
val audioTrackId: String? = null,
|
||||||
val contentLength: Long = -1
|
val contentLength: Long = -1
|
||||||
) {
|
) {
|
||||||
fun getQualityString(fileName: String): String {
|
private fun getQualityString(fileName: String): String {
|
||||||
return "${fileName}_${quality?.replace(" ", "_")}_$format." +
|
return "${fileName}_${quality?.replace(" ", "_")}_$format." +
|
||||||
mimeType?.split("/")?.last()
|
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 {
|
val stream = videoStreams.find {
|
||||||
it.quality == videoQuality && it.format == videoFormat
|
it.quality == videoQuality && it.format == videoFormat
|
||||||
}
|
}
|
||||||
items.add(
|
stream?.toDownloadItem(FileType.VIDEO, videoId, fileName)?.let { items.add(it) }
|
||||||
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,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!audioQuality.isNullOrEmpty() && !audioFormat.isNullOrEmpty()) {
|
if (!audioQuality.isNullOrEmpty() && !audioFormat.isNullOrEmpty()) {
|
||||||
val stream = audioStreams.find {
|
val stream = audioStreams.find {
|
||||||
it.quality == audioQuality && it.format == audioFormat
|
it.quality == audioQuality && it.format == audioFormat
|
||||||
}
|
}
|
||||||
items.add(
|
stream?.toDownloadItem(FileType.AUDIO, videoId, fileName)?.let { items.add(it) }
|
||||||
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,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!subtitleCode.isNullOrEmpty()) {
|
if (!subtitleCode.isNullOrEmpty()) {
|
||||||
|
@ -5,7 +5,7 @@ import kotlinx.coroutines.withContext
|
|||||||
import java.net.HttpURLConnection
|
import java.net.HttpURLConnection
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
|
|
||||||
suspend fun URL.getContentLength(def: Long = -1): Long {
|
suspend fun URL.getContentLength(): Long? {
|
||||||
try {
|
try {
|
||||||
return withContext(Dispatchers.IO) {
|
return withContext(Dispatchers.IO) {
|
||||||
val connection = openConnection() as HttpURLConnection
|
val connection = openConnection() as HttpURLConnection
|
||||||
@ -20,5 +20,5 @@ suspend fun URL.getContentLength(def: Long = -1): Long {
|
|||||||
}
|
}
|
||||||
} catch (e: Exception) { e.printStackTrace() }
|
} catch (e: Exception) { e.printStackTrace() }
|
||||||
|
|
||||||
return def
|
return null
|
||||||
}
|
}
|
||||||
|
@ -167,8 +167,9 @@ class DownloadService : LifecycleService() {
|
|||||||
var totalRead = path.fileSize()
|
var totalRead = path.fileSize()
|
||||||
val url = URL(item.url ?: return)
|
val url = URL(item.url ?: return)
|
||||||
|
|
||||||
url.getContentLength().let { size ->
|
// only fetch the content length if it's not been returned by the API
|
||||||
if (size > 0 && size != item.downloadSize) {
|
if (item.downloadSize == 0L) {
|
||||||
|
url.getContentLength()?.takeIf { it != item.downloadSize }?.let { size ->
|
||||||
item.downloadSize = size
|
item.downloadSize = size
|
||||||
Database.downloadDao().updateDownloadItem(item)
|
Database.downloadDao().updateDownloadItem(item)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user