diff --git a/MoppApp/MoppApp/Extensions/URL+Additions.swift b/MoppApp/MoppApp/Extensions/URL+Additions.swift index 030c5c36..4a2604de 100644 --- a/MoppApp/MoppApp/Extensions/URL+Additions.swift +++ b/MoppApp/MoppApp/Extensions/URL+Additions.swift @@ -28,24 +28,22 @@ extension URL { func isValidPath() -> Bool { return !self.pathComponents.contains("..") } -} -class ValidPath { - static func getValidPath(url: URL) -> URL { + func getValidPath() -> URL? { guard let appDirectoryURL = FileManager.default.urls(for: .applicationDirectory, in: .userDomainMask).first else { - return URL(string: "")! + return nil } guard let subdirectoryURLs = try? FileManager.default.contentsOfDirectory(at: appDirectoryURL, includingPropertiesForKeys: nil, options: .skipsHiddenFiles) else { - return URL(string: "")! + return nil } for subdirectoryURL in subdirectoryURLs { let subdirectoryFilePath = FilePath(stringLiteral: subdirectoryURL.path) - if (FilePath(stringLiteral: url.path).starts(with: subdirectoryFilePath.lexicallyNormalized())) { + if (FilePath(stringLiteral: self.path).starts(with: subdirectoryFilePath.lexicallyNormalized())) { return URL(fileURLWithPath: subdirectoryFilePath.description) } } - return URL(string: "")! + return nil } } diff --git a/MoppApp/MoppApp/MoppApp.swift b/MoppApp/MoppApp/MoppApp.swift index b14b2455..c4a14960 100644 --- a/MoppApp/MoppApp/MoppApp.swift +++ b/MoppApp/MoppApp/MoppApp.swift @@ -323,11 +323,11 @@ class MoppApp: UIApplication, URLSessionDelegate, URLSessionDownloadDelegate { pathExtension = MimeTypeExtractor.determineContainer(mimetype: MimeTypeExtractor.getMimeTypeFromContainer(filePath: newUrl), fileExtension: newUrl.pathExtension) do { - guard newUrl.isValidPath() else { + guard let validUrl = newUrl.getValidPath(), validUrl.isValidPath() else { printLog("Unable to open file. Invalid path: \(newUrl.path)") return false } - let newData: Data? = try Data(contentsOf: ValidPath.getValidPath(url: newUrl)) + let newData: Data? = try Data(contentsOf: validUrl) let fileName: String = newUrl.deletingPathExtension().lastPathComponent.sanitize() let tempDirectoryPath: URL? = MoppFileManager.shared.tempCacheDirectoryPath() guard let tempDirectory = tempDirectoryPath else { @@ -343,11 +343,11 @@ class MoppApp: UIApplication, URLSessionDelegate, URLSessionDownloadDelegate { return false } do { - guard filePath.isValidPath() else { + guard let validUrl = filePath.getValidPath(), validUrl.isValidPath() else { printLog("Unable to write to file. Invalid path: \(filePath.path)") return false } - try newUrlData.write(to: ValidPath.getValidPath(url: filePath), options: .atomic) + try newUrlData.write(to: validUrl, options: .atomic) newUrl = filePath if !isFileEmpty { fileUrls.append(newUrl) @@ -375,7 +375,7 @@ class MoppApp: UIApplication, URLSessionDelegate, URLSessionDownloadDelegate { newUrl.deletePathExtension() newUrl.appendPathExtension(ContainerFormatCdoc) } - guard newUrl.isValidPath() else { + guard let validUrl = newUrl.getValidPath(), validUrl.isValidPath() else { printLog("Unable to move file. Invalid path: \(newUrl.path)") return false }