-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6404 from grzesiek2010/COLLECT-6364
Implemented #sanitizedCanonicalPath method
- Loading branch information
Showing
16 changed files
with
137 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 0 additions & 23 deletions
23
shared/src/main/java/org/odk/collect/shared/files/DirectoryUtils.kt
This file was deleted.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
shared/src/main/java/org/odk/collect/shared/files/FileExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.odk.collect.shared.files | ||
|
||
import java.io.File | ||
import java.io.IOException | ||
import java.io.InputStream | ||
import kotlin.jvm.Throws | ||
|
||
object FileExt { | ||
/** | ||
* Original File.getCanonicalPath() may return paths with inconsistent letter casing for the | ||
* /Android/data/ part of the path, such as /storage/emulated/0/android/data/... or /storage/emulated/0/Android/Data/... | ||
* instead of the expected /storage/emulated/0/Android/data/... | ||
* Since the Android file system is case-sensitive, this behavior appears to be a bug. | ||
* | ||
* For more details, see the discussion on Stack Overflow: | ||
* https://stackoverflow.com/questions/78965720/file-getcanonicalpath-returns-inconsistent-letter-casing-in-path | ||
*/ | ||
fun File.sanitizedCanonicalPath(): String { | ||
val androidDataSegment = "/Android/data/" | ||
val regex = Regex(androidDataSegment, RegexOption.IGNORE_CASE) | ||
|
||
return canonicalPath.replace(regex, androidDataSegment) | ||
} | ||
|
||
fun File.listFilesRecursively(): List<File> { | ||
val listFiles = listFiles() ?: emptyArray() | ||
return listFiles.flatMap { | ||
if (it.isDirectory) { | ||
it.listFilesRecursively() | ||
} else { | ||
listOf(it) | ||
} | ||
} | ||
} | ||
|
||
@JvmStatic | ||
fun File.deleteDirectory() { | ||
deleteRecursively() | ||
} | ||
|
||
@Throws(IOException::class) | ||
@JvmStatic | ||
fun File.saveToFile(inputStream: InputStream) { | ||
if (exists() && !delete()) { | ||
throw IOException("Cannot overwrite $absolutePath. Perhaps the file is locked?") | ||
} | ||
|
||
inputStream.use { input -> | ||
outputStream().use { output -> | ||
input.copyTo(output) | ||
} | ||
} | ||
} | ||
} |
23 changes: 0 additions & 23 deletions
23
shared/src/main/java/org/odk/collect/shared/files/FileUtils.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.