Fix deprecation warnings.

This commit is contained in:
Brent Simmons
2024-02-24 20:39:44 -08:00
parent a1a199860e
commit 78c1f8fe59
2 changed files with 28 additions and 34 deletions

View File

@@ -11,6 +11,7 @@ import CoreServices
import Articles
import Account
import RSCore
import UniformTypeIdentifiers
extension Notification.Name {
@@ -61,8 +62,6 @@ final class FaviconDownloader {
loadHomePageToFaviconURLCache()
loadHomePageURLsWithNoFaviconURLCache()
FaviconURLFinder.ignoredTypes = [kUTTypeScalableVectorGraphics as String]
NotificationCenter.default.addObserver(self, selector: #selector(didLoadFavicon(_:)), name: .DidLoadFavicon, object: nil)
}

View File

@@ -9,31 +9,14 @@
import Foundation
import CoreServices
import RSParser
import UniformTypeIdentifiers
// The favicon URLs may be specified in the head section of the home page.
struct FaviconURLFinder {
private static var ignoredMimeTypes = [String]()
private static var ignoredExtensions = [String]()
/// Uniform types to ignore when finding favicon URLs.
static var ignoredTypes: [String]? {
didSet {
guard let ignoredTypes = ignoredTypes else {
return
}
for type in ignoredTypes {
if let mimeTypes = UTTypeCopyAllTagsWithClass(type as CFString, kUTTagClassMIMEType)?.takeRetainedValue() {
ignoredMimeTypes.append(contentsOf: mimeTypes as! [String])
}
if let extensions = UTTypeCopyAllTagsWithClass(type as CFString, kUTTagClassFilenameExtension)?.takeRetainedValue() {
ignoredExtensions.append(contentsOf: extensions as! [String])
}
}
}
}
static var ignoredTypes = [UTType.svg]
/// Finds favicon URLs in a web page.
/// - Parameters:
@@ -48,24 +31,36 @@ struct FaviconURLFinder {
}
// If the favicon has an explicit type, check that for an ignored type; otherwise, check the file extension.
HTMLMetadataDownloader.downloadMetadata(for: homePageURL) { (htmlMetadata) in
let faviconURLs = htmlMetadata?.favicons.compactMap({ (favicon) -> String? in
if let type = favicon.type {
if ignoredMimeTypes.contains(type) {
return nil
}
}
else {
if let urlString = favicon.urlString, let url = URL(string: urlString), ignoredExtensions.contains(url.pathExtension) {
return nil
}
}
HTMLMetadataDownloader.downloadMetadata(for: homePageURL) { htmlMetadata in
let faviconURLs = htmlMetadata?.favicons.compactMap { favicon -> String? in
guard shouldAllowFavicon(favicon) else {
return nil
}
return favicon.urlString
})
}
completion(faviconURLs)
}
}
static func shouldAllowFavicon(_ favicon: RSHTMLMetadataFavicon) -> Bool {
// Check mime type.
if let mimeType = favicon.type, let utType = UTType(mimeType: mimeType) {
if ignoredTypes.contains(utType) {
return false
}
}
// Check file extension.
if let urlString = favicon.urlString, let url = URL(string: urlString), let utType = UTType(filenameExtension: url.pathExtension) {
if ignoredTypes.contains(utType) {
return false
}
}
return true
}
}