Skip to content

Commit 7de4435

Browse files
committed
fix: use encoded uri for file path
1 parent 395ad71 commit 7de4435

File tree

4 files changed

+17
-21
lines changed

4 files changed

+17
-21
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
<modelVersion>4.0.0</modelVersion>
77
<groupId>io.ionic.libs</groupId>
88
<artifactId>ionfiletransfer-android</artifactId>
9-
<version>0.0.1-dev-3</version>
9+
<version>0.0.1-dev-4</version>
1010
</project>

src/main/kotlin/io/ionic/libs/ionfiletransferlib/IONFLTRController.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import java.io.BufferedOutputStream
2323
import java.io.File
2424
import java.io.FileOutputStream
2525
import java.net.HttpURLConnection
26+
import java.net.URI
2627

2728
/**
2829
* Entry point in IONFileTransferLib-Android
@@ -118,11 +119,11 @@ class IONFLTRController internal constructor(
118119
*/
119120
private fun prepareForDownload(options: IONFLTRDownloadOptions): Pair<File, HttpURLConnection> {
120121
// Validate inputs
121-
inputsValidator.validateTransferInputs(options.url, options.filePath)
122+
val normalizedFilePath = fileHelper.normalizeFilePath(options.filePath)
123+
inputsValidator.validateTransferInputs(options.url, normalizedFilePath)
122124

123125
// Create parent directories if needed
124-
val normalizedFilePath = fileHelper.normalizeFilePath(options.filePath)
125-
val targetFile = File(normalizedFilePath)
126+
val targetFile = File(URI(normalizedFilePath).path)
126127
fileHelper.createParentDirectories(targetFile)
127128

128129
// Setup connection

src/main/kotlin/io/ionic/libs/ionfiletransferlib/helpers/IONFLTRFileHelper.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import java.io.File
1212
import java.io.FileInputStream
1313
import java.io.InputStream
1414
import androidx.core.net.toUri
15+
import java.net.URI
16+
import java.net.URLDecoder
17+
import java.net.URLEncoder
1518

1619
internal class IONFLTRFileHelper(val contentResolver: ContentResolver) {
1720
/**
@@ -35,7 +38,7 @@ internal class IONFLTRFileHelper(val contentResolver: ContentResolver) {
3538
}
3639
} else {
3740
val cleanFilePath = normalizeFilePath(filePath)
38-
val fileObject = File(cleanFilePath)
41+
val fileObject = File(URI(cleanFilePath).path)
3942
if (!fileObject.exists()) {
4043
throw IONFLTRException.FileDoesNotExist()
4144
}
@@ -50,12 +53,17 @@ internal class IONFLTRFileHelper(val contentResolver: ContentResolver) {
5053
* @return Cleaned file path without URI prefixes
5154
*/
5255
fun normalizeFilePath(filePath: String): String {
53-
return when {
56+
val path = when {
5457
filePath.startsWith("file://") -> filePath.removePrefix("file://")
5558
filePath.startsWith("file:/") -> filePath.removePrefix("file:/")
5659
filePath.startsWith("file:") -> filePath.removePrefix("file:")
5760
else -> filePath
5861
}
62+
63+
return URLEncoder.encode(
64+
URLDecoder.decode(path, Charsets.UTF_8.toString()),
65+
Charsets.UTF_8.toString()
66+
).replace("+", "%20")
5967
}
6068

6169
/**

src/main/kotlin/io/ionic/libs/ionfiletransferlib/helpers/IONFLTRInputsValidator.kt

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,8 @@ internal class IONFLTRInputsValidator {
3535
}
3636

3737
return try {
38-
val resolvedPath: String
39-
if (path.startsWith("file://")) {
40-
val encodedPath = URLEncoder.encode(
41-
path.replace("file://", ""),
42-
Charsets.UTF_8.toString()
43-
).replace("+", "%20")
44-
val uri = URI(encodedPath)
45-
if (uri.path == null) {
46-
return false
47-
}
48-
resolvedPath = uri.path
49-
} else {
50-
resolvedPath = path
51-
}
52-
File(resolvedPath).isAbsolute
38+
val uri = URI(path).path
39+
File(uri).isAbsolute
5340
} catch (e: URISyntaxException) {
5441
false
5542
}

0 commit comments

Comments
 (0)