Skip to content

Commit 3092dd1

Browse files
fix: Opening local files with special characters (#3)
* fix: Opening files with special characters. * chore: Prepare to release version 1.0.1
1 parent f65bbfa commit 3092dd1

File tree

6 files changed

+28
-7
lines changed

6 files changed

+28
-7
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [1.0.1]
8+
9+
### 2025-05-28
10+
11+
- Fix opening local files with special characters.
12+
713
## [1.0.0]
814

915
### 2025-03-14

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ In your app-level gradle file, import the `ion-android-fileviewer` library like
2828

2929
```
3030
dependencies {
31-
implementation("io.ionic.libs:ionfileviewer-android:1.0.0")
31+
implementation("io.ionic.libs:ionfileviewer-android:1.0.1")
3232
}
3333
```
3434

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>ionfileviewer-android</artifactId>
9-
<version>1.0.0</version>
9+
<version>1.0.1</version>
1010
</project>

src/main/kotlin/io/ionic/libs/ionfileviewerlib/IONFLVWController.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import io.ionic.libs.ionfileviewerlib.helpers.IONFLVWInputsValidator
77
import io.ionic.libs.ionfileviewerlib.helpers.IONFLVWOpenDocumentHelper
88
import io.ionic.libs.ionfileviewerlib.helpers.runCatchingIONFLVWExceptions
99
import io.ionic.libs.ionfileviewerlib.model.IONFLVWException
10-
import kotlin.concurrent.thread
1110

1211
/**
1312
* Entry point in IONFileViewerLib-Android
@@ -41,8 +40,8 @@ class IONFLVWController internal constructor(
4140
if (!inputsValidator.isPathValid(filePath)) {
4241
throw IONFLVWException.InvalidPath(filePath)
4342
}
44-
val filePathNoSpaces: String = filePath.replace("%20| ".toRegex(), "\\ ")
45-
openDocumentHelper.openDocumentFromLocalPath(activity, filePathNoSpaces)
43+
val filePathNormalized: String = inputsValidator.normalizeFilePath(filePath)
44+
openDocumentHelper.openDocumentFromLocalPath(activity, filePathNormalized)
4645
}
4746

4847
/**

src/main/kotlin/io/ionic/libs/ionfileviewerlib/helpers/IONFLVWInputsValidator.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package io.ionic.libs.ionfileviewerlib.helpers
22

3+
import android.net.Uri
34
import android.os.Looper
5+
import androidx.core.net.toUri
6+
import io.ionic.libs.ionfileviewerlib.model.IONFLVWException
47
import java.util.regex.Pattern
58

69
class IONFLVWInputsValidator {
@@ -29,4 +32,17 @@ class IONFLVWInputsValidator {
2932
* @return true if in main thread, false otherwise
3033
*/
3134
fun isInMainThread(): Boolean = Looper.getMainLooper().isCurrentThread
35+
36+
/**
37+
* Normalizes a file path before opening it.
38+
* This means verifying the fields are URL decoded, while making sure we're not trying to decode already decoded fields
39+
*
40+
* @param filePath the file path to normalize
41+
* @throws IONFLVWException.InvalidPath if unable to normalize the file path
42+
* @return the normalized file path, with special characters decoded.
43+
*/
44+
fun normalizeFilePath(filePath: String): String {
45+
val filePathToNormalize = Uri.encode(Uri.decode(filePath))
46+
return filePathToNormalize.toUri().path ?: throw IONFLVWException.InvalidPath(filePath)
47+
}
3248
}

src/main/kotlin/io/ionic/libs/ionfileviewerlib/helpers/IONFLVWOpenDocumentHelper.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package io.ionic.libs.ionfileviewerlib.helpers
33
import android.app.Activity
44
import android.content.ActivityNotFoundException
55
import android.content.Intent
6-
import android.net.Uri
76
import android.webkit.MimeTypeMap
87
import androidx.core.content.FileProvider
8+
import androidx.core.net.toUri
99
import java.io.File
1010
import java.io.FileNotFoundException
1111

@@ -52,7 +52,7 @@ internal class IONFLVWOpenDocumentHelper {
5252
*/
5353
@Throws(ActivityNotFoundException::class)
5454
fun openDocumentFromURL(activity: Activity, url: String) {
55-
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
55+
val intent = Intent(Intent.ACTION_VIEW, url.toUri())
5656
activity.startActivity(intent)
5757
}
5858

0 commit comments

Comments
 (0)