fixed subs on downloads

This commit is contained in:
LagradOst 2023-08-30 00:52:34 +02:00
parent ce1f48978b
commit 6089cbc484
3 changed files with 45 additions and 32 deletions

View File

@ -233,7 +233,7 @@ dependencies {
// To fix SSL fuckery on android 9
implementation("org.conscrypt:conscrypt-android:2.2.1")
// Util to skip the URI file fuckery 🙏
implementation("com.github.LagradOst:SafeFile:0.0.3")
implementation("com.github.LagradOst:SafeFile:0.0.5")
// API because cba maintaining it myself
implementation("com.uwetrottmann.tmdb2:tmdb-java:2.6.0")

View File

@ -50,6 +50,10 @@ class DownloadFileGenerator(
return null
}
fun cleanDisplayName(name: String): String {
return name.substringBeforeLast('.').trim()
}
override suspend fun generateLinks(
clearCache: Boolean,
isCasting: Boolean,
@ -58,39 +62,48 @@ class DownloadFileGenerator(
offset: Int,
): Boolean {
val meta = episodes[currentIndex + offset]
callback(Pair(null, meta))
callback(null to meta)
context?.let { ctx ->
val relative = meta.relativePath
val display = meta.displayName
val ctx = context ?: return true
val relative = meta.relativePath ?: return true
val display = meta.displayName ?: return true
if (display == null || relative == null) {
return@let
val cleanDisplay = cleanDisplayName(display)
VideoDownloadManager.getFolder(ctx, relative, meta.basePath)
?.forEach { (name, uri) ->
// only these files are allowed, so no videos as subtitles
if (listOf(
".vtt",
".srt",
".txt",
".ass",
".ttml",
".sbv",
".dfxp"
).none { name.contains(it, true) }
) return@forEach
// cant have the exact same file as a subtitle
if (name.equals(display, true)) return@forEach
val cleanName = cleanDisplayName(name)
// we only want files with the approx same name
if (!cleanName.startsWith(cleanDisplay, true)) return@forEach
val realName = cleanName.removePrefix(cleanDisplay)
subtitleCallback(
SubtitleData(
realName.ifBlank { ctx.getString(R.string.default_subtitles) },
uri.toString(),
SubtitleOrigin.DOWNLOADED_FILE,
name.toSubtitleMimeType(),
emptyMap()
)
)
}
VideoDownloadManager.getFolder(ctx, relative, meta.basePath)
?.forEach { file ->
val name = display.removeSuffix(".mp4")
if (file.first != meta.displayName && file.first.startsWith(name)) {
val realName = file.first.removePrefix(name)
.removeSuffix(".vtt")
.removeSuffix(".srt")
.removeSuffix(".txt")
.trim()
.removePrefix("(")
.removeSuffix(")")
subtitleCallback(
SubtitleData(
realName.ifBlank { ctx.getString(R.string.default_subtitles) },
file.second.toString(),
SubtitleOrigin.DOWNLOADED_FILE,
name.toSubtitleMimeType(),
emptyMap()
)
)
}
}
}
return true
}

View File

@ -505,7 +505,7 @@ object VideoDownloadManager {
): List<Pair<String, Uri>>? {
val base = basePathToFile(context, basePath)
val folder = base?.gotoDirectory(relativePath, false) ?: return null
if (folder.isDirectory() != false) return null
//if (folder.isDirectory() != false) return null
return folder.listFiles()
?.mapNotNull { (it.name() ?: "") to (it.uri() ?: return@mapNotNull null) }