2021-09-20 13:34:25 +02:00
|
|
|
//
|
|
|
|
// ArticleThemeDownloader.swift
|
|
|
|
// ArticleThemeDownloader
|
|
|
|
//
|
|
|
|
// Created by Stuart Breckenridge on 20/09/2021.
|
|
|
|
// Copyright © 2021 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Zip
|
|
|
|
|
2021-09-21 03:10:56 +02:00
|
|
|
public class ArticleThemeDownloader {
|
2021-09-20 13:34:25 +02:00
|
|
|
|
2021-09-21 03:10:56 +02:00
|
|
|
public enum ArticleThemeDownloaderError: LocalizedError {
|
|
|
|
case noThemeFile
|
|
|
|
|
|
|
|
public var errorDescription: String? {
|
|
|
|
switch self {
|
|
|
|
case .noThemeFile:
|
|
|
|
return "There is no NetNewsWire theme available."
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static let shared = ArticleThemeDownloader()
|
|
|
|
private init() {}
|
|
|
|
|
|
|
|
public func handleFile(at location: URL) throws {
|
2021-09-20 13:34:25 +02:00
|
|
|
createDownloadDirectoryIfRequired()
|
|
|
|
let movedFileLocation = try moveTheme(from: location)
|
|
|
|
let unzippedFileLocation = try unzipFile(at: movedFileLocation)
|
2021-09-21 03:10:56 +02:00
|
|
|
NotificationCenter.default.post(name: .didEndDownloadingTheme, object: nil, userInfo: ["url" : unzippedFileLocation])
|
2021-09-20 13:34:25 +02:00
|
|
|
}
|
|
|
|
|
2021-09-21 03:10:56 +02:00
|
|
|
|
|
|
|
/// Creates `Application Support/NetNewsWire/Downloads` if needed.
|
|
|
|
private func createDownloadDirectoryIfRequired() {
|
|
|
|
try? FileManager.default.createDirectory(at: downloadDirectory(), withIntermediateDirectories: true, attributes: nil)
|
2021-09-20 13:34:25 +02:00
|
|
|
}
|
|
|
|
|
2021-09-21 03:10:56 +02:00
|
|
|
/// Moves the downloaded `.tmp` file to the `downloadDirectory` and renames it a `.zip`
|
|
|
|
/// - Parameter location: The temporary file location.
|
|
|
|
/// - Returns: Destination `URL`.
|
|
|
|
private func moveTheme(from location: URL) throws -> URL {
|
|
|
|
var tmpFileName = location.lastPathComponent
|
|
|
|
tmpFileName = tmpFileName.replacingOccurrences(of: ".tmp", with: ".zip")
|
|
|
|
let fileUrl = downloadDirectory().appendingPathComponent("\(tmpFileName)")
|
|
|
|
try FileManager.default.moveItem(at: location, to: fileUrl)
|
|
|
|
return fileUrl
|
2021-09-20 13:34:25 +02:00
|
|
|
}
|
|
|
|
|
2021-09-21 03:10:56 +02:00
|
|
|
/// Unzips the zip file
|
|
|
|
/// - Parameter location: Location of the zip archive.
|
|
|
|
/// - Returns: Enclosed `.nnwtheme` file.
|
|
|
|
private func unzipFile(at location: URL) throws -> URL {
|
2021-09-20 13:34:25 +02:00
|
|
|
do {
|
2021-09-21 03:10:56 +02:00
|
|
|
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
|
2021-09-22 03:17:48 +02:00
|
|
|
let themeFilePath = findThemeFile(in: unzipDirectory.path)
|
2021-09-21 03:10:56 +02:00
|
|
|
if themeFilePath == nil {
|
|
|
|
throw ArticleThemeDownloaderError.noThemeFile
|
|
|
|
}
|
|
|
|
return URL(fileURLWithPath: unzipDirectory.appendingPathComponent(themeFilePath!).path)
|
2021-09-20 13:34:25 +02:00
|
|
|
} catch {
|
|
|
|
try? FileManager.default.removeItem(at: location)
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-22 03:17:48 +02:00
|
|
|
|
2022-01-04 23:25:20 +01:00
|
|
|
/// Performs a deep search of the unzipped directory to find the theme file.
|
2021-09-22 03:17:48 +02:00
|
|
|
/// - 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
|
|
|
|
}
|
|
|
|
|
2022-01-04 23:25:20 +01:00
|
|
|
/// The download directory used by the theme downloader: `Application Support/NetNewsWire/Downloads`
|
2021-09-21 03:10:56 +02:00
|
|
|
/// - Returns: `URL`
|
|
|
|
private func downloadDirectory() -> URL {
|
|
|
|
FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!.appendingPathComponent("NetNewsWire/Downloads", isDirectory: true)
|
2021-09-20 13:34:25 +02:00
|
|
|
}
|
|
|
|
|
2022-01-04 23:25:20 +01:00
|
|
|
/// Removes downloaded themes, where themes == folders, from `Application Support/NetNewsWire/Downloads`.
|
2021-09-21 03:10:56 +02:00
|
|
|
public func cleanUp() {
|
|
|
|
guard let filenames = try? FileManager.default.contentsOfDirectory(atPath: downloadDirectory().path) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for path in filenames {
|
|
|
|
do {
|
|
|
|
if FileManager.default.isFolder(atPath: downloadDirectory().appendingPathComponent(path).path) {
|
|
|
|
try FileManager.default.removeItem(atPath: downloadDirectory().appendingPathComponent(path).path)
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
print(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-20 13:34:25 +02:00
|
|
|
}
|