Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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