0bytes downloads fix

This commit is contained in:
firelight 2024-07-18 02:02:35 +02:00 committed by GitHub
parent a157115cfa
commit 627dd45309
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -546,7 +546,8 @@ object VideoDownloadManager {
tryResume: Boolean,
): StreamData {
return setupStream(
context.getBasePath().first ?: getDefaultDir(context) ?: throw IOException("Bad config"),
context.getBasePath().first ?: getDefaultDir(context)
?: throw IOException("Bad config"),
name,
folder,
extension,
@ -1028,7 +1029,10 @@ object VideoDownloadManager {
tryResume: Boolean,
parentId: Int?,
createNotificationCallback: (CreateNotificationMetadata) -> Unit,
parallelConnections: Int = 3
parallelConnections: Int = 3,
/** how many bytes a valid file must be in bytes,
* this should be different for subtitles and video */
minimumSize: Long = 100
): DownloadStatus = withContext(Dispatchers.IO) {
if (parallelConnections < 1) {
return@withContext DOWNLOAD_INVALID_INPUT
@ -1074,6 +1078,13 @@ object VideoDownloadManager {
)
)
if (items.totalLength != null && items.totalLength < minimumSize) {
fileStream.closeQuietly()
metadata.onDelete()
stream.delete()
return@withContext DOWNLOAD_INVALID_INPUT
}
metadata.totalBytes = items.totalLength
metadata.type = DownloadType.IsDownloading
metadata.setDownloadFileInfoTemplate(
@ -1223,6 +1234,16 @@ object VideoDownloadManager {
return@withContext DOWNLOAD_STOPPED
}
// in case the head request lies about content-size,
// then we don't want shit output
if (metadata.bytesDownloaded < minimumSize) {
// we need to close before delete
fileStream.closeQuietly()
metadata.onDelete()
stream.delete()
return@withContext DOWNLOAD_INVALID_INPUT
}
metadata.type = DownloadType.IsDone
return@withContext DOWNLOAD_SUCCESS
} catch (e: IOException) {
@ -1274,6 +1295,7 @@ object VideoDownloadManager {
val displayName = getDisplayName(name, extension)
val stream =
setupStream(baseFile, name, folder, extension, startAt > 0)
if (!stream.resume) startAt = 0
fileStream = stream.open()
@ -1300,6 +1322,7 @@ object VideoDownloadManager {
) + if (link.referer.isNotBlank()) mapOf("referer" to link.referer) else emptyMap()
)
)
val items = M3u8Helper2.hslLazy(listOf(m3u8))
metadata.hlsTotal = items.size
@ -1576,6 +1599,7 @@ object VideoDownloadManager {
callback, parallelConnections = maxConcurrentConnections
)
}
ExtractorLinkType.VIDEO -> {
return downloadThing(
context,
@ -1585,9 +1609,13 @@ object VideoDownloadManager {
"mp4",
tryResume,
ep.id,
callback, parallelConnections = maxConcurrentConnections
callback,
parallelConnections = maxConcurrentConnections,
/** We require at least 10 MB video files */
minimumSize = (1 shl 20) * 10
)
}
else -> throw IllegalArgumentException("unsuported download type")
}
} catch (t: Throwable) {