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 file:// URI parsing #1601

Merged
merged 1 commit into from
Jan 30, 2023
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 coil-base/src/androidTest/java/coil/map/FileUriMapperTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package coil.map

import android.content.ContentResolver.SCHEME_FILE
import android.content.Context
import android.net.Uri
import androidx.core.net.toUri
import androidx.test.core.app.ApplicationProvider
import coil.request.Options
Expand Down Expand Up @@ -52,6 +53,21 @@ class FileUriMapperTest {
fun parsesPoundCharacterCorrectly() {
val path = "/sdcard/fi#le.jpg"
assertEquals(File(path), mapper.map(path.toUri(), Options(context)))
assertEquals(File(path), mapper.map("file://$path".toUri(), Options(context)))
}

/** Regression test: https://github.com/coil-kt/coil/issues/1513 */
@Test
fun ignoresAfterPathCorrectly() {
val expected = File("/sdcard/file.jpg")
val uri = Uri.parse("$SCHEME_FILE:///sdcard/file.jpg?query=value&query2=value#fragment")
assertEquals(expected, mapper.map(uri, Options(context)))
}

/** Regression test: https://github.com/coil-kt/coil/issues/1513 */
@Test
fun decodesEncodedPath() {
val expected = File("/sdcard/Some File.jpg")
val uri = Uri.parse("$SCHEME_FILE:///sdcard/Some%20File.jpg")
assertEquals(expected, mapper.map(uri, Options(context)))
}
}
9 changes: 7 additions & 2 deletions coil-base/src/main/java/coil/map/FileUriMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ internal class FileUriMapper : Mapper<Uri, File> {

override fun map(data: Uri, options: Options): File? {
if (!isApplicable(data)) return null
val uri = if (data.scheme == null) data else data.buildUpon().scheme(null).build()
return File(uri.toString())
if (data.scheme == SCHEME_FILE) {
return data.path?.let(::File)
} else {
// If the scheme is not "file", it's null, representing a literal path on disk.
// Assume the entire input, regardless of any reserved characters, is valid.
return File(data.toString())
}
}

private fun isApplicable(data: Uri): Boolean {
Expand Down