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, tryResume: Boolean,
): StreamData { ): StreamData {
return setupStream( return setupStream(
context.getBasePath().first ?: getDefaultDir(context) ?: throw IOException("Bad config"), context.getBasePath().first ?: getDefaultDir(context)
?: throw IOException("Bad config"),
name, name,
folder, folder,
extension, extension,
@ -945,7 +946,7 @@ object VideoDownloadManager {
bufferSize: Int = DEFAULT_BUFFER_SIZE, bufferSize: Int = DEFAULT_BUFFER_SIZE,
/** how many bytes bytes it should require to use the parallel downloader instead, /** how many bytes bytes it should require to use the parallel downloader instead,
* if we download a very small file we don't want it parallel */ * if we download a very small file we don't want it parallel */
maximumSmallSize : Long = chuckSize * 2 maximumSmallSize: Long = chuckSize * 2
): LazyStreamDownloadData { ): LazyStreamDownloadData {
// we don't want to make a separate connection for every 1kb // we don't want to make a separate connection for every 1kb
require(chuckSize > 1000) require(chuckSize > 1000)
@ -1028,7 +1029,10 @@ object VideoDownloadManager {
tryResume: Boolean, tryResume: Boolean,
parentId: Int?, parentId: Int?,
createNotificationCallback: (CreateNotificationMetadata) -> Unit, 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) { ): DownloadStatus = withContext(Dispatchers.IO) {
if (parallelConnections < 1) { if (parallelConnections < 1) {
return@withContext DOWNLOAD_INVALID_INPUT 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.totalBytes = items.totalLength
metadata.type = DownloadType.IsDownloading metadata.type = DownloadType.IsDownloading
metadata.setDownloadFileInfoTemplate( metadata.setDownloadFileInfoTemplate(
@ -1223,6 +1234,16 @@ object VideoDownloadManager {
return@withContext DOWNLOAD_STOPPED 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 metadata.type = DownloadType.IsDone
return@withContext DOWNLOAD_SUCCESS return@withContext DOWNLOAD_SUCCESS
} catch (e: IOException) { } catch (e: IOException) {
@ -1274,6 +1295,7 @@ object VideoDownloadManager {
val displayName = getDisplayName(name, extension) val displayName = getDisplayName(name, extension)
val stream = val stream =
setupStream(baseFile, name, folder, extension, startAt > 0) setupStream(baseFile, name, folder, extension, startAt > 0)
if (!stream.resume) startAt = 0 if (!stream.resume) startAt = 0
fileStream = stream.open() fileStream = stream.open()
@ -1300,6 +1322,7 @@ object VideoDownloadManager {
) + if (link.referer.isNotBlank()) mapOf("referer" to link.referer) else emptyMap() ) + if (link.referer.isNotBlank()) mapOf("referer" to link.referer) else emptyMap()
) )
) )
val items = M3u8Helper2.hslLazy(listOf(m3u8)) val items = M3u8Helper2.hslLazy(listOf(m3u8))
metadata.hlsTotal = items.size metadata.hlsTotal = items.size
@ -1397,7 +1420,7 @@ object VideoDownloadManager {
try { try {
// may cause java.lang.IllegalStateException: Mutex is not locked because of cancelling // may cause java.lang.IllegalStateException: Mutex is not locked because of cancelling
fileMutex.unlock() fileMutex.unlock()
} catch (t : Throwable) { } catch (t: Throwable) {
logError(t) logError(t)
} }
} }
@ -1524,7 +1547,7 @@ object VideoDownloadManager {
tryResume: Boolean = false, tryResume: Boolean = false,
): DownloadStatus { ): DownloadStatus {
// no support for these file formats // no support for these file formats
if(link.type == ExtractorLinkType.MAGNET || link.type == ExtractorLinkType.TORRENT || link.type == ExtractorLinkType.DASH) { if (link.type == ExtractorLinkType.MAGNET || link.type == ExtractorLinkType.TORRENT || link.type == ExtractorLinkType.DASH) {
return DOWNLOAD_INVALID_INPUT return DOWNLOAD_INVALID_INPUT
} }
@ -1556,7 +1579,7 @@ object VideoDownloadManager {
} }
try { try {
when(link.type) { when (link.type) {
ExtractorLinkType.M3U8 -> { ExtractorLinkType.M3U8 -> {
val startIndex = if (tryResume) { val startIndex = if (tryResume) {
context.getKey<DownloadedFileInfo>( context.getKey<DownloadedFileInfo>(
@ -1576,6 +1599,7 @@ object VideoDownloadManager {
callback, parallelConnections = maxConcurrentConnections callback, parallelConnections = maxConcurrentConnections
) )
} }
ExtractorLinkType.VIDEO -> { ExtractorLinkType.VIDEO -> {
return downloadThing( return downloadThing(
context, context,
@ -1585,9 +1609,13 @@ object VideoDownloadManager {
"mp4", "mp4",
tryResume, tryResume,
ep.id, 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") else -> throw IllegalArgumentException("unsuported download type")
} }
} catch (t: Throwable) { } catch (t: Throwable) {