Fix connection timeout when add a large m3u playlist file (#216)

This commit is contained in:
tungnk123
2024-12-01 22:48:46 +07:00
committed by GitHub
parent 6cd34439d1
commit a0e774b244
2 changed files with 26 additions and 6 deletions

View File

@ -4,6 +4,8 @@ import android.content.ContentResolver
import android.net.Uri
import android.provider.OpenableColumns
import androidx.core.net.toFile
import java.io.File
import java.io.IOException
fun Uri.readFileName(resolver: ContentResolver): String? {
return if (this == Uri.EMPTY) null
@ -32,4 +34,18 @@ fun Uri.readFileContent(resolver: ContentResolver): String? {
else -> null
}
}
}
fun Uri.copyToFile(resolver: ContentResolver, destinationFile: File): Boolean {
return try {
resolver.openInputStream(this)?.use { inputStream ->
destinationFile.outputStream().use { outputStream ->
inputStream.copyTo(outputStream)
}
}
true
} catch (e: IOException) {
e.printStackTrace()
false
}
}

View File

@ -16,7 +16,7 @@ import com.m3u.core.architecture.logger.sandBox
import com.m3u.core.architecture.preferences.PlaylistStrategy
import com.m3u.core.architecture.preferences.Preferences
import com.m3u.core.util.basic.startsWithAny
import com.m3u.core.util.readFileContent
import com.m3u.core.util.copyToFile
import com.m3u.core.util.readFileName
import com.m3u.data.api.OkhttpClient
import com.m3u.data.database.dao.ChannelDao
@ -650,16 +650,20 @@ internal class PlaylistRepositoryImpl @Inject constructor(
return withContext(ioDispatcher) {
val contentResolver = context.contentResolver
val filename = uri.readFileName(contentResolver) ?: filenameWithTimezone
val content = uri.readFileContent(contentResolver).orEmpty()
val file = File(context.filesDir, filename)
file.writeText(content)
val destinationFile = File(context.filesDir, filename)
val newUrl = Uri.decode(file.toUri().toString())
val success = uri.copyToFile(contentResolver, destinationFile)
if (!success) {
return@withContext this@actualUrl
}
val newUrl = Uri.decode(destinationFile.toUri().toString())
playlistDao.updateUrl(this@actualUrl, newUrl)
newUrl
}
}
private fun openNetworkInput(url: String): InputStream? {
val request = Request.Builder()
.url(url)