Skip to content
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.ionic.libs</groupId>
<artifactId>ionfiletransfer-android</artifactId>
<version>0.0.1-dev-1</version>
<version>0.0.1-dev-4</version>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream
import java.net.HttpURLConnection
import java.net.URI

/**
* Entry point in IONFileTransferLib-Android
Expand Down Expand Up @@ -118,11 +119,11 @@ class IONFLTRController internal constructor(
*/
private fun prepareForDownload(options: IONFLTRDownloadOptions): Pair<File, HttpURLConnection> {
// Validate inputs
inputsValidator.validateTransferInputs(options.url, options.filePath)
val normalizedFilePath = fileHelper.normalizeFilePath(options.filePath)
inputsValidator.validateTransferInputs(options.url, normalizedFilePath)

// Create parent directories if needed
val normalizedFilePath = fileHelper.normalizeFilePath(options.filePath)
val targetFile = File(normalizedFilePath)
val targetFile = File(URI(normalizedFilePath).path)
fileHelper.createParentDirectories(targetFile)

// Setup connection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import java.io.File
import java.io.FileInputStream
import java.io.InputStream
import androidx.core.net.toUri
import java.net.URI
import java.net.URLDecoder
import java.net.URLEncoder

internal class IONFLTRFileHelper(val contentResolver: ContentResolver) {
/**
Expand All @@ -35,7 +38,7 @@ internal class IONFLTRFileHelper(val contentResolver: ContentResolver) {
}
} else {
val cleanFilePath = normalizeFilePath(filePath)
val fileObject = File(cleanFilePath)
val fileObject = File(URI(cleanFilePath).path)
if (!fileObject.exists()) {
throw IONFLTRException.FileDoesNotExist()
}
Expand All @@ -50,12 +53,17 @@ internal class IONFLTRFileHelper(val contentResolver: ContentResolver) {
* @return Cleaned file path without URI prefixes
*/
fun normalizeFilePath(filePath: String): String {
return when {
val path = when {
filePath.startsWith("file://") -> filePath.removePrefix("file://")
filePath.startsWith("file:/") -> filePath.removePrefix("file:/")
filePath.startsWith("file:") -> filePath.removePrefix("file:")
else -> filePath
}

return URLEncoder.encode(
URLDecoder.decode(path, Charsets.UTF_8.toString()),
Charsets.UTF_8.toString()
).replace("+", "%20")
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import java.util.regex.Pattern
import java.io.File
import java.net.URI
import java.net.URISyntaxException
import java.net.URLEncoder

internal class IONFLTRInputsValidator {

Expand Down Expand Up @@ -34,17 +35,8 @@ internal class IONFLTRInputsValidator {
}

return try {
val resolvedPath: String
if (path.startsWith("file://")) {
val uri = URI(path)
if (uri.path == null) {
return false
}
resolvedPath = uri.path
} else {
resolvedPath = path
}
File(resolvedPath).isAbsolute
val uri = URI(path).path
File(uri).isAbsolute
} catch (e: URISyntaxException) {
false
}
Expand Down
Loading