From 3eb13ea93b831bd853e537934bdc030abd768caf Mon Sep 17 00:00:00 2001 From: Nelson Lopes Silva <5671236+nflsilva@users.noreply.github.com> Date: Tue, 29 Mar 2022 15:25:57 +0100 Subject: [PATCH] fix: fixed bug that prevents loading documents with spaces in name. Added new error for file without extension --- src/ios/FileViewerErrors.swift | 1 + src/ios/FileViewerPlugin.swift | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ios/FileViewerErrors.swift b/src/ios/FileViewerErrors.swift index fbaa3e9..84d3f07 100644 --- a/src/ios/FileViewerErrors.swift +++ b/src/ios/FileViewerErrors.swift @@ -14,6 +14,7 @@ enum FileViewerErrors: String, Error { case invalidURL = "The URL you are trying to open is malformed" case invalidEmptyURL = "Path of the file to open is either null or empty" case downloadFailed = "The download failed" + case missingFileExtension = "The file has no extension" } extension FileViewerErrors : LocalizedError { diff --git a/src/ios/FileViewerPlugin.swift b/src/ios/FileViewerPlugin.swift index 6899f3f..591301d 100644 --- a/src/ios/FileViewerPlugin.swift +++ b/src/ios/FileViewerPlugin.swift @@ -22,6 +22,10 @@ class FileViewerPlugin { if let url = urlString { if let file = URL.init(string: url) { + + let fileName = file.lastPathComponent + guard fileName.contains(".") else { throw FileViewerErrors.missingFileExtension } + guard FileManager.default.fileExists(atPath: file.path) else { throw FileViewerErrors.fileDoesNotExist } if let viewController = rootViewController { @@ -40,7 +44,7 @@ class FileViewerPlugin { return completion({ throw FileViewerErrors.invalidURL }) } - if let fileUrl = URL(string: url), let viewController = rootViewController { + if let fileUrl = URL(string: url.replacingOccurrences(of: " ", with: "%20")), let viewController = rootViewController { let fileViewerOpenDocument = FileViewerOpenDocument(viewController: viewController) fileViewerOpenDocument.openDocumentFromUrl(url:fileUrl, completion: { (inner: ErrorCompletionHandler) -> Void in do {