Skip to content

Commit

Permalink
Don't allow invalid characters in filename pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
naveensingh committed Mar 15, 2024
1 parent 60ddae2 commit 27749aa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
11 changes: 6 additions & 5 deletions commons/src/main/kotlin/org/fossify/commons/extensions/String.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ fun String.getFirstParentPath(context: Context, level: Int): String {
}

fun String.isAValidFilename(): Boolean {
val ILLEGAL_CHARACTERS = charArrayOf('/', '\n', '\r', '\t', '\u0000', '`', '?', '*', '\\', '<', '>', '|', '\"', ':')
ILLEGAL_CHARACTERS.forEach {
if (contains(it))
return false
}
charArrayOf('/', '\n', '\r', '\t', '\u0000', '`', '?', '*', '\\', '<', '>', '|', '\"', ':')
.forEach {
if (contains(it))
return false
}

return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class RenamePatternTab(context: Context, attrs: AttributeSet) : RelativeLayout(c
return
}

if (!newNameRaw.isAValidFilename()) {
activity?.toast(R.string.invalid_name)
return
}

val validPaths = paths.filter { activity?.getDoesFilePathExist(it) == true }
val firstPath = validPaths.firstOrNull()
val sdFilePath = validPaths.firstOrNull { activity?.isPathOnSD(it) == true } ?: firstPath
Expand Down Expand Up @@ -180,7 +185,7 @@ class RenamePatternTab(context: Context, attrs: AttributeSet) : RelativeLayout(c
paths: List<String>,
useMediaFileExtension: Boolean,
android30Format: Android30RenameFormat,
callback: (success: Boolean) -> Unit
callback: (success: Boolean) -> Unit,
) {
val fileDirItems = paths.map { File(it).toFileDirItem(context) }
val uriPairs = context.getUrisPathsFromFileDirItems(fileDirItems)
Expand Down

0 comments on commit 27749aa

Please sign in to comment.