2017-11-20 22:16:06 +01:00
|
|
|
//
|
|
|
|
// FaviconURLFinder.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
// NetNewsWire
|
2017-11-20 22:16:06 +01:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 11/20/17.
|
|
|
|
// Copyright © 2017 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2020-02-01 08:09:36 +01:00
|
|
|
import CoreServices
|
2024-04-03 06:43:06 +02:00
|
|
|
import Parser
|
2024-02-25 05:39:44 +01:00
|
|
|
import UniformTypeIdentifiers
|
2017-11-20 22:16:06 +01:00
|
|
|
|
2019-11-26 02:54:09 +01:00
|
|
|
// The favicon URLs may be specified in the head section of the home page.
|
2017-11-20 22:16:06 +01:00
|
|
|
|
|
|
|
struct FaviconURLFinder {
|
|
|
|
|
2020-01-31 23:33:35 +01:00
|
|
|
/// Uniform types to ignore when finding favicon URLs.
|
2024-04-03 07:07:19 +02:00
|
|
|
static let ignoredTypes = [UTType.svg]
|
2020-01-31 23:09:01 +01:00
|
|
|
|
|
|
|
/// Finds favicon URLs in a web page.
|
|
|
|
/// - Parameters:
|
|
|
|
/// - homePageURL: The page to search.
|
|
|
|
/// - urls: An array of favicon URLs as strings.
|
2024-04-11 05:49:55 +02:00
|
|
|
@MainActor static func findFaviconURLs(with homePageURL: String) async -> [String]? {
|
2020-01-31 23:09:01 +01:00
|
|
|
|
2021-07-19 00:01:59 +02:00
|
|
|
guard let _ = URL(unicodeString: homePageURL) else {
|
2024-04-11 05:49:55 +02:00
|
|
|
return nil
|
2020-01-31 23:09:01 +01:00
|
|
|
}
|
2020-01-31 22:59:16 +01:00
|
|
|
|
2024-04-11 05:49:55 +02:00
|
|
|
// If the favicon has an explicit type, check that for an ignored type; otherwise, check the file extension.
|
|
|
|
let htmlMetadata = try? await HTMLMetadataDownloader.downloadMetadata(for: homePageURL)
|
2024-04-11 04:03:41 +02:00
|
|
|
|
2024-04-11 05:49:55 +02:00
|
|
|
let faviconURLs = htmlMetadata?.favicons.compactMap { favicon -> String? in
|
|
|
|
shouldAllowFavicon(favicon) ? favicon.urlString : nil
|
2017-12-14 04:46:03 +01:00
|
|
|
}
|
2024-04-11 05:49:55 +02:00
|
|
|
|
|
|
|
return faviconURLs
|
2017-11-20 22:16:06 +01:00
|
|
|
}
|
2024-02-25 05:39:44 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2017-11-20 22:16:06 +01:00
|
|
|
}
|
|
|
|
|