Skip to content

Commit e321d8c

Browse files
fix: normalize file paths before transferring
Co-authored-by: OS-pedrogustavobilro <[email protected]>
1 parent 9ed8a4f commit e321d8c

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ class IONFLTRController internal constructor(
121121
inputsValidator.validateTransferInputs(options.url, options.filePath)
122122

123123
// Create parent directories if needed
124-
val targetFile = File(options.filePath)
124+
val normalizedFilePath = fileHelper.normalizeFilePath(options.filePath)
125+
val targetFile = File(normalizedFilePath)
125126
fileHelper.createParentDirectories(targetFile)
126127

127128
// Setup connection

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,29 @@ internal class IONFLTRFileHelper(val contentResolver: ContentResolver) {
3434
FileToUploadInfo(fileName, fileSize, inputStream)
3535
}
3636
} else {
37-
val filePathWithoutPrefix = filePath.removePrefix("file://")
38-
val fileObject = File(filePathWithoutPrefix)
37+
val cleanFilePath = normalizeFilePath(filePath)
38+
val fileObject = File(cleanFilePath)
3939
if (!fileObject.exists()) {
4040
throw IONFLTRException.FileDoesNotExist()
4141
}
4242
FileToUploadInfo(fileObject.name, fileObject.length(), FileInputStream(fileObject))
4343
}
4444
}
4545

46+
/**
47+
* Normalizes a file path by removing URI prefixes like "file://", "file:/", etc.
48+
*
49+
* @param filePath The file path that might contain URI prefixes
50+
* @return Cleaned file path without URI prefixes
51+
*/
52+
fun normalizeFilePath(filePath: String): String {
53+
return when {
54+
filePath.startsWith("file://") -> filePath.removePrefix("file://")
55+
filePath.startsWith("file:/") -> filePath.removePrefix("file:/")
56+
filePath.startsWith("file:") -> filePath.removePrefix("file:")
57+
else -> filePath
58+
}
59+
}
4660

4761
/**
4862
* Gets a MIME type based on the provided file path
@@ -51,8 +65,10 @@ internal class IONFLTRFileHelper(val contentResolver: ContentResolver) {
5165
* @return The MIME type or null if it was unable to determine
5266
*/
5367
fun getMimeType(filePath: String?): String? =
54-
MimeTypeMap.getFileExtensionFromUrl(filePath)?.let { extension ->
55-
MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)
68+
filePath?.let { normalizeFilePath(it) }?.let { normalizedPath ->
69+
MimeTypeMap.getFileExtensionFromUrl(normalizedPath)?.let { extension ->
70+
MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)
71+
}
5672
}
5773

5874
/**

0 commit comments

Comments
 (0)