diff --git a/Passepartout-iOS/Scenes/Organizer/WizardHostViewController.swift b/Passepartout-iOS/Scenes/Organizer/WizardHostViewController.swift index 70be468c..04a5be1e 100644 --- a/Passepartout-iOS/Scenes/Organizer/WizardHostViewController.swift +++ b/Passepartout-iOS/Scenes/Organizer/WizardHostViewController.swift @@ -33,11 +33,6 @@ class WizardHostViewController: UITableViewController, TableModelHost, Wizard { private struct ParsedFile { let url: URL - var filename: String { - let raw = url.deletingPathExtension().lastPathComponent - return raw.components(separatedBy: AppConstants.Store.filenameCharset.inverted).joined(separator: "_") - } - let hostname: String let configuration: TunnelKitProvider.Configuration @@ -118,7 +113,7 @@ class WizardHostViewController: UITableViewController, TableModelHost, Wizard { return } if field.text?.isEmpty ?? true { - field.text = parsedFile?.filename + field.text = parsedFile?.url.normalizedFilename } } @@ -216,7 +211,7 @@ extension WizardHostViewController { let cell = Cells.field.dequeue(from: tableView, for: indexPath) cell.caption = L10n.Wizards.Host.Cells.TitleInput.caption cell.captionWidth = 100.0 - cell.allowedCharset = AppConstants.Store.filenameCharset + cell.allowedCharset = .filename cell.field.placeholder = L10n.Wizards.Host.Cells.TitleInput.placeholder cell.field.clearButtonMode = .always cell.field.returnKeyType = .done diff --git a/Passepartout/Sources/AppConstants.swift b/Passepartout/Sources/AppConstants.swift index ed6f908a..f856014b 100644 --- a/Passepartout/Sources/AppConstants.swift +++ b/Passepartout/Sources/AppConstants.swift @@ -40,16 +40,6 @@ class AppConstants { static let providersDirectory = "Providers" static let hostsDirectory = "Hosts" - - static let filenameCharset: CharacterSet = { - var chars: CharacterSet = .decimalDigits - let english = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - let symbols = "-_" - chars.formUnion(CharacterSet(charactersIn: english)) - chars.formUnion(CharacterSet(charactersIn: english.lowercased())) - chars.formUnion(CharacterSet(charactersIn: symbols)) - return chars - }() } class VPN { diff --git a/Passepartout/Sources/Utils.swift b/Passepartout/Sources/Utils.swift index 0c178870..d90a7e51 100644 --- a/Passepartout/Sources/Utils.swift +++ b/Passepartout/Sources/Utils.swift @@ -201,3 +201,24 @@ extension String { } } } + +extension CharacterSet { + static let filename: CharacterSet = { + var chars: CharacterSet = .decimalDigits + let english = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + let symbols = "-_" + chars.formUnion(CharacterSet(charactersIn: english)) + chars.formUnion(CharacterSet(charactersIn: english.lowercased())) + chars.formUnion(CharacterSet(charactersIn: symbols)) + return chars + }() +} + +extension URL { + private static let illegalCharacterFallback = "_" + + var normalizedFilename: String { + let filename = deletingPathExtension().lastPathComponent + return filename.components(separatedBy: CharacterSet.filename.inverted).joined(separator: URL.illegalCharacterFallback) + } +}