-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented #sanitizedCanonicalPath method #6404
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
590664e
Implemented #sanitizedCanonicalPath method
grzesiek2010 4307de2
Added tests
grzesiek2010 b4d85f2
Use sanitizedCanonicalPath in getAbsoluteFilePath
grzesiek2010 da478e4
Moved #saveToFile to FileExt class
grzesiek2010 13c499e
Added tests
grzesiek2010 99ed162
Moved #listFilesRecursively to FileExt class
grzesiek2010 4764976
Moved #deleteDirectory to FileExt class
grzesiek2010 768ea97
Updated the comment
grzesiek2010 62e92e2
Simplified #sanitizedCanonicalPath
grzesiek2010 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
60 changes: 60 additions & 0 deletions
60
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,60 @@ | ||
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 canonicalPath = canonicalPath | ||
|
||
if (canonicalPath.contains(androidDataSegment, true)) { | ||
val regex = Regex(androidDataSegment, RegexOption.IGNORE_CASE) | ||
return canonicalPath.replace(regex, androidDataSegment) | ||
} | ||
|
||
return canonicalPath | ||
} | ||
|
||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should be able to do the replace without a contains check -- if there's no match it just won't be replaced.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point.