Skip to content

Commit

Permalink
Fix connection timeout when add a large m3u playlist file (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
tungnk123 authored Dec 1, 2024
1 parent 6cd3443 commit a0e774b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
18 changes: 17 additions & 1 deletion core/src/main/java/com/m3u/core/util/Files.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a0e774b

Please sign in to comment.