From 94ed186b8cc67f32c0668f3d4e69f79ed86d7bc9 Mon Sep 17 00:00:00 2001 From: Stuart Breckenridge Date: Wed, 22 Sep 2021 09:17:48 +0800 Subject: [PATCH] Perfoms search of directory for theme file. --- .../ArticleStyles/ArticleThemeDownloader.swift | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Shared/ArticleStyles/ArticleThemeDownloader.swift b/Shared/ArticleStyles/ArticleThemeDownloader.swift index 1dd775ee1..0e94437ee 100644 --- a/Shared/ArticleStyles/ArticleThemeDownloader.swift +++ b/Shared/ArticleStyles/ArticleThemeDownloader.swift @@ -58,7 +58,7 @@ public class ArticleThemeDownloader { let unzipDirectory = URL(fileURLWithPath: location.path.replacingOccurrences(of: ".zip", with: "")) try Zip.unzipFile(location, destination: unzipDirectory, overwrite: true, password: nil, progress: nil, fileOutputHandler: nil) // Unzips to folder in Application Support/NetNewsWire/Downloads try FileManager.default.removeItem(at: location) // Delete zip in Cache - let themeFilePath = FileManager.default.filenames(inFolder: unzipDirectory.path)?.first(where: { $0.contains(".nnwtheme") }) + let themeFilePath = findThemeFile(in: unzipDirectory.path) if themeFilePath == nil { throw ArticleThemeDownloaderError.noThemeFile } @@ -69,6 +69,22 @@ public class ArticleThemeDownloader { } } + + /// Performs a deep search of the unzipped direcotry to find the theme file. + /// - Parameter searchPath: directory to search + /// - Returns: optional `String` + private func findThemeFile(in searchPath: String) -> String? { + if let directoryContents = FileManager.default.enumerator(atPath: searchPath) { + while let file = directoryContents.nextObject() as? String { + if file.hasSuffix(".nnwtheme") { + return file + } + } + } + + return nil + } + /// The download directory used by the theme downloader: `Application Suppport/NetNewsWire/Downloads` /// - Returns: `URL` private func downloadDirectory() -> URL {