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
8 changes: 8 additions & 0 deletions Sources/NextcloudKit/Utils/FileNameValidator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ public final class FileNameValidator: Sendable {
private let forbiddenFileNameCharacters: [String]
private let forbiddenFileNameExtensions: [String]

public func fileEmptyNameError() -> NKError {
NKError(errorCode: NSURLErrorCannotCreateFile, errorDescription: NSLocalizedString("_file_name_empty_", value: "File name cannot be empty.", comment: ""))
}

public func fileWithSpaceError() -> NKError {
NKError(errorCode: NSURLErrorCannotCreateFile, errorDescription: NSLocalizedString("_file_name_validator_error_space_", value: "Name must not contain spaces at the beginning or end.", comment: ""))
}
Expand Down Expand Up @@ -40,6 +44,10 @@ public final class FileNameValidator: Sendable {
}

public func checkFileName(_ filename: String) -> NKError? {
if filename.trimmingCharacters(in: .whitespaces).isEmpty {
return fileEmptyNameError()
}

if let regex = try? NSRegularExpression(pattern: "[\(forbiddenFileNameCharacters.joined())]"), let invalidCharacterError = checkInvalidCharacters(string: filename, regex: regex) {
return invalidCharacterError
}
Expand Down
14 changes: 14 additions & 0 deletions Tests/NextcloudKitUnitTests/FileNameValidatorUnitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,18 @@ class FileNameValidatorUnitTests: XCTestCase {
let result = fileNameValidator.checkFolderPath(folderPath)
XCTAssertFalse(result)
}

func testFileNameEmpty() {
let folderPath = "/A1/Aaaww/W/ "
let filePaths = ["", " ", " ", " "]

let result = fileNameValidator.checkFolderPath(folderPath)
XCTAssertFalse(result)

filePaths.forEach { path in
let result = fileNameValidator.checkFileName(path)

XCTAssertNotNil(result?.errorDescription)
}
}
}
Loading