Skip to content

Commit

Permalink
Allow user to create file with ending with space or period
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Sep 11, 2024
1 parent ab03d22 commit 6576d2d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,15 @@ class FileNameValidatorTests : AbstractOnServerIT() {
@Test
fun testEndsWithSpaceOrPeriod() {
val result = FileNameValidator.checkFileName("filename ", capability, targetContext)
assertEquals(targetContext.getString(R.string.file_name_validator_error_ends_with_space_period), result)

val result2 = FileNameValidator.checkFileName("filename.", capability, targetContext)
assertEquals(targetContext.getString(R.string.file_name_validator_error_ends_with_space_period), result2)

if (capability.version.isOlderThan(NextcloudVersion.nextcloud_30)) {
assertEquals(null, result)
assertEquals(null, result2)
} else {
assertEquals(targetContext.getString(R.string.file_name_validator_error_ends_with_space_period), result)
assertEquals(targetContext.getString(R.string.file_name_validator_error_ends_with_space_period), result2)
}
}

@Test
Expand Down Expand Up @@ -169,7 +174,7 @@ class FileNameValidatorTests : AbstractOnServerIT() {
val filePaths = listOf("file1.txt", "file2.doc", "file3.jpg")

val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
assertFalse(result)
assertEquals(capability.version.isOlderThan(NextcloudVersion.nextcloud_30), result)
}

@Test
Expand All @@ -178,7 +183,7 @@ class FileNameValidatorTests : AbstractOnServerIT() {
val filePaths = listOf("file1.txt", "file2.doc", "file3.")

val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
assertFalse(result)
assertEquals(capability.version.isOlderThan(NextcloudVersion.nextcloud_30), result)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,69 +49,50 @@ object FileNameValidator {
}
}

return if (capability.version.isNewerOrEqual(NextcloudVersion.nextcloud_30)) {
validateForNCThirtyOrAbove(filename, capability, context)
} else {
validateForBelowNCThirty(filename, context)
}
}

private fun validateForBelowNCThirty(
filename: String,
context: Context,
): String? {
if (filename.endsWith(StringConstants.SPACE) || filename.endsWith(StringConstants.DOT)) {
return context.getString(R.string.file_name_validator_error_ends_with_space_period)
}

return null
}

private fun validateForNCThirtyOrAbove(
filename: String,
capability: OCCapability,
context: Context,
): String? {
checkInvalidCharacters(filename, capability, context)?.let {
return it
}

capability.forbiddenFilenameBaseNames().let {
val forbiddenFilenameBaseNames = capability.forbiddenFilenameBaseNames().map { it.lowercase() }

if (forbiddenFilenameBaseNames.contains(filename.lowercase()) || forbiddenFilenameBaseNames.contains(
filename.removeFileExtension().lowercase()
)
) {
return context.getString(
R.string.file_name_validator_error_reserved_names,
filename.substringBefore(StringConstants.DOT)
)
if (capability.version.isNewerOrEqual(NextcloudVersion.nextcloud_30)) {
checkInvalidCharacters(filename, capability, context)?.let {
return it
}
}

capability.forbiddenFilenamesJson?.let {
val forbiddenFilenames = capability.forbiddenFilenames().map { it.lowercase() }

if (forbiddenFilenames.contains(filename.uppercase()) || forbiddenFilenames.contains(
filename.removeFileExtension().uppercase()
)
) {
return context.getString(
R.string.file_name_validator_error_reserved_names,
filename.substringBefore(StringConstants.DOT)
)
}
}

capability.forbiddenFilenameExtensionJson?.let {
val forbiddenFilenameExtension = capability.forbiddenFilenameExtension()

if (forbiddenFilenameExtension.any { filename.endsWith(it, ignoreCase = true) }) {
return context.getString(
R.string.file_name_validator_error_forbidden_file_extensions,
filename.substringAfter(StringConstants.DOT)
)
capability.run {
forbiddenFilenameBaseNames().let {
val forbiddenFilenameBaseNames = capability.forbiddenFilenameBaseNames().map { it.lowercase() }

if (forbiddenFilenameBaseNames.contains(filename.lowercase()) || forbiddenFilenameBaseNames.contains(
filename.removeFileExtension().lowercase()
)
) {
return context.getString(
R.string.file_name_validator_error_reserved_names,
filename.substringBefore(StringConstants.DOT)
)
}
}

forbiddenFilenamesJson?.let {
val forbiddenFilenames = capability.forbiddenFilenames().map { it.lowercase() }

if (forbiddenFilenames.contains(filename.uppercase()) || forbiddenFilenames.contains(
filename.removeFileExtension().uppercase()
)
) {
return context.getString(
R.string.file_name_validator_error_reserved_names,
filename.substringBefore(StringConstants.DOT)
)
}
}

forbiddenFilenameExtensionJson?.let {
val forbiddenFilenameExtension = capability.forbiddenFilenameExtension()

if (forbiddenFilenameExtension.any { filename.endsWith(it, ignoreCase = true) }) {
return context.getString(
R.string.file_name_validator_error_forbidden_file_extensions,
filename.substringAfter(StringConstants.DOT)
)
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class CreateFolderDialogFragment : DialogFragment(), DialogInterface.OnClickList
private fun getOCCapability(): OCCapability = fileDataStorageManager.getCapability(accountProvider.user.accountName)

private fun checkFileNameAfterEachType(fileNames: MutableSet<String>) {
val newFileName = binding.userInput.text?.toString()?.trim() ?: ""
val newFileName = binding.userInput.text?.toString() ?: ""

val fileNameValidatorResult: String? =
FileNameValidator.checkFileName(newFileName, getOCCapability(), requireContext(), fileNames)
Expand Down Expand Up @@ -172,7 +172,7 @@ class CreateFolderDialogFragment : DialogFragment(), DialogInterface.OnClickList
override fun onClick(dialog: DialogInterface, which: Int) {
if (which == AlertDialog.BUTTON_POSITIVE) {
val newFolderName = (getDialog()?.findViewById<View>(R.id.user_input) as TextView)
.text.toString().trim { it <= ' ' }
.text.toString()

val errorMessage: String? =
FileNameValidator.checkFileName(newFolderName, getOCCapability(), requireContext())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class RenameFileDialogFragment : DialogFragment(), DialogInterface.OnClickListen
var newFileName = ""

if (binding.userInput.text != null) {
newFileName = binding.userInput.text.toString().trim { it <= ' ' }
newFileName = binding.userInput.text.toString()
}

val errorMessage = checkFileName(newFileName, oCCapability, requireContext(), null)
Expand Down Expand Up @@ -166,7 +166,7 @@ class RenameFileDialogFragment : DialogFragment(), DialogInterface.OnClickListen
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
var newFileName = ""
if (binding.userInput.text != null) {
newFileName = binding.userInput.text.toString().trim { it <= ' ' }
newFileName = binding.userInput.text.toString()
}

val errorMessage = checkFileName(newFileName, oCCapability, requireContext(), fileNames)
Expand Down

0 comments on commit 6576d2d

Please sign in to comment.