3
3
Fork 1
mirror of https://github.com/recloudstream/cloudstream.git synced 2024-08-15 01:53:11 +00:00

Make sure matching subtitles are also deleted

This commit is contained in:
Luna712 2024-07-10 13:55:48 -06:00 committed by GitHub
parent 14c1a36f82
commit 20b2fcaedc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 49 additions and 5 deletions
app/src/main/java/com/lagradost/cloudstream3

View file

@ -5,6 +5,7 @@ import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.ui.player.PlayerSubtitleHelper.Companion.toSubtitleMimeType
import com.lagradost.cloudstream3.utils.ExtractorLink
import com.lagradost.cloudstream3.utils.VideoDownloadManager
import com.lagradost.cloudstream3.utils.VideoDownloadManager.cleanDisplayName
import kotlin.math.max
import kotlin.math.min
@ -49,10 +50,6 @@ class DownloadFileGenerator(
return null
}
fun cleanDisplayName(name: String): String {
return name.substringBeforeLast('.').trim()
}
override suspend fun generateLinks(
clearCache: Boolean,
type: LoadType,

View file

@ -1760,7 +1760,54 @@ object VideoDownloadManager {
downloadProgressEvent.invoke(Triple(id, 0, 0))
downloadStatusEvent.invoke(id to DownloadType.IsStopped)
downloadDeleteEvent.invoke(id)
return info.toFile(context)?.delete() ?: false
val isFileDeleted = info.toFile(context)?.delete() ?: false
if (isFileDeleted) deleteSubtitles(context, info)
return isFileDeleted
}
private fun deleteSubtitles(context: Context, info: DownloadedFileInfo): Boolean {
val relative = info.relativePath
val display = info.displayName
val cleanDisplay = cleanDisplayName(display)
val subtitleFiles = getFolder(context, relative, info.basePath)
?.mapNotNull { (name, uri) ->
if (listOf(
".vtt",
".srt",
".txt",
".ass",
".ttml",
".sbv",
".dfxp"
).none { name.contains(it, true) }
) return@mapNotNull null
if (name.equals(display, true)) return@mapNotNull null
val cleanName = cleanDisplayName(name)
if (!cleanName.startsWith(cleanDisplay, true)) return@mapNotNull null
SafeFile.fromUri(context, uri)
} ?: return false
var allDeleted = true
subtitleFiles.forEach { subtitleFile ->
if (!subtitleFile.delete()) {
Log.e("SubtitleDeletion", "Failed to delete subtitle file: ${subtitleFile.name()}")
allDeleted = false
}
}
return allDeleted
}
fun cleanDisplayName(name: String): String {
return name.substringBeforeLast('.').trim()
}
fun getDownloadResumePackage(context: Context, id: Int): DownloadResumePackage? {