diff --git a/core/src/main/java/com/m3u/core/util/Files.kt b/core/src/main/java/com/m3u/core/util/Files.kt index dc99f385..c880da5b 100644 --- a/core/src/main/java/com/m3u/core/util/Files.kt +++ b/core/src/main/java/com/m3u/core/util/Files.kt @@ -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 } -} \ No newline at end of file +} + +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 + } +} diff --git a/data/src/main/java/com/m3u/data/repository/playlist/PlaylistRepositoryImpl.kt b/data/src/main/java/com/m3u/data/repository/playlist/PlaylistRepositoryImpl.kt index d1baed88..658abc61 100644 --- a/data/src/main/java/com/m3u/data/repository/playlist/PlaylistRepositoryImpl.kt +++ b/data/src/main/java/com/m3u/data/repository/playlist/PlaylistRepositoryImpl.kt @@ -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)