LEFT | RIGHT |
1 // | 1 // |
2 // FavIcon | 2 // FavIcon |
3 // Copyright © 2016 Leon Breedt | 3 // Copyright © 2016 Leon Breedt |
4 // | 4 // |
5 // Licensed under the Apache License, Version 2.0 (the "License"); | 5 // Licensed under the Apache License, Version 2.0 (the "License"); |
6 // you may not use this file except in compliance with the License. | 6 // you may not use this file except in compliance with the License. |
7 // You may obtain a copy of the License at | 7 // You may obtain a copy of the License at |
8 // | 8 // |
9 // http://www.apache.org/licenses/LICENSE-2.0 | 9 // http://www.apache.org/licenses/LICENSE-2.0 |
10 // | 10 // |
11 // Unless required by applicable law or agreed to in writing, software | 11 // Unless required by applicable law or agreed to in writing, software |
12 // distributed under the License is distributed on an "AS IS" BASIS, | 12 // distributed under the License is distributed on an "AS IS" BASIS, |
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 // See the License for the specific language governing permissions and | 14 // See the License for the specific language governing permissions and |
15 // limitations under the License. | 15 // limitations under the License. |
16 // | 16 // |
17 | 17 |
18 // swiftlint:disable sorted_imports | |
19 // swiftlint:disable file_length | 18 // swiftlint:disable file_length |
20 | 19 |
21 import Foundation | 20 import Foundation |
| 21 |
22 #if os(iOS) | 22 #if os(iOS) |
23 import UIKit | 23 import UIKit |
24 /// Alias for the iOS image type (`UIImage`). | 24 /// Alias for the iOS image type (`UIImage`). |
25 public typealias ImageType = UIImage | 25 public typealias ImageType = UIImage |
26 #elseif os(OSX) | 26 #elseif os(OSX) |
27 import Cocoa | 27 import Cocoa |
28 /// Alias for the OS X image type (`NSImage`). | 28 /// Alias for the OS X image type (`NSImage`). |
29 public typealias ImageType = NSImage | 29 public typealias ImageType = NSImage |
30 #endif | 30 #endif |
31 | 31 |
(...skipping 30 matching lines...) Expand all Loading... |
62 /// - If _Web Application Manifest JSON_ (`manifest.json`) files are referen
ced, or | 62 /// - If _Web Application Manifest JSON_ (`manifest.json`) files are referen
ced, or |
63 /// _Microsoft browser configuration XML_ (`browserconfig.xml`) files | 63 /// _Microsoft browser configuration XML_ (`browserconfig.xml`) files |
64 /// are referenced, download and parse them to check if they reference ico
ns. | 64 /// are referenced, download and parse them to check if they reference ico
ns. |
65 /// | 65 /// |
66 /// All of this work is performed in a background queue. | 66 /// All of this work is performed in a background queue. |
67 /// | 67 /// |
68 /// - parameter url: The base URL to scan. | 68 /// - parameter url: The base URL to scan. |
69 /// - parameter completion: A closure to call when the scan has completed. T
he closure will be call | 69 /// - parameter completion: A closure to call when the scan has completed. T
he closure will be call |
70 /// on the main queue. | 70 /// on the main queue. |
71 @objc | 71 @objc |
72 public static func scan(_ url: URL, completion: @escaping ([DetectedIcon], [
String:String]) -> Void) { | 72 public static func scan(_ url: URL, completion: @escaping ([DetectedIcon], [
String: String]) -> Void) { |
73 let queue = DispatchQueue(label: "org.bitserf.FavIcon", attributes: []) | 73 let queue = DispatchQueue(label: "org.bitserf.FavIcon", attributes: []) |
74 var icons: [DetectedIcon] = [] | 74 var icons: [DetectedIcon] = [] |
75 var additionalDownloads: [URLRequestWithCallback] = [] | 75 var additionalDownloads: [URLRequestWithCallback] = [] |
76 let urlSession = urlSessionProvider() | 76 let urlSession = urlSessionProvider() |
77 var meta: [String:String] = [:] | 77 var meta: [String: String] = [:] |
78 | 78 |
79 let downloadHTMLOperation = DownloadTextOperation(url: url, session: url
Session) | 79 let downloadHTMLOperation = DownloadTextOperation(url: url, session: url
Session) |
80 let downloadHTML = urlRequestOperation(downloadHTMLOperation) { result i
n | 80 let downloadHTML = urlRequestOperation(downloadHTMLOperation) { result i
n |
81 if case let .textDownloaded(actualURL, text, contentType) = result { | 81 if case let .textDownloaded(actualURL, text, contentType) = result { |
82 if contentType == "text/html" { | 82 if contentType == "text/html" { |
83 let document = HTMLDocument(string: text) | 83 let document = HTMLDocument(string: text) |
84 | 84 |
85 let htmlIcons = extractHTMLHeadIcons(document, baseURL: actu
alURL) | 85 let htmlIcons = extractHTMLHeadIcons(document, baseURL: actu
alURL) |
86 let htmlMeta = examineHTMLMeta(document, baseURL: actualURL) | 86 let htmlMeta = examineHTMLMeta(document, baseURL: actualURL) |
87 queue.sync { | 87 queue.sync { |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 | 355 |
356 extension FavIcon { | 356 extension FavIcon { |
357 /// Convenience overload for `scan(url:completion:)` that takes a `String` | 357 /// Convenience overload for `scan(url:completion:)` that takes a `String` |
358 /// instead of a `URL` as the URL parameter. Throws an error if the URL is n
ot a valid URL. | 358 /// instead of a `URL` as the URL parameter. Throws an error if the URL is n
ot a valid URL. |
359 /// | 359 /// |
360 /// - parameter url: The base URL to scan. | 360 /// - parameter url: The base URL to scan. |
361 /// - parameter completion: A closure to call when the scan has completed. T
he closure will be called | 361 /// - parameter completion: A closure to call when the scan has completed. T
he closure will be called |
362 /// on the main queue. | 362 /// on the main queue. |
363 /// - throws: An `IconError` if the scan failed for some reason. | 363 /// - throws: An `IconError` if the scan failed for some reason. |
364 @objc | 364 @objc |
365 public static func scan(_ url: String, completion: @escaping ([DetectedIcon]
, [String:String]) -> Void) throws { | 365 public static func scan(_ url: String, completion: @escaping ([DetectedIcon]
, [String: String]) -> Void) throws { |
366 guard let url = URL(string: url) else { throw IconError.invalidBaseURL } | 366 guard let url = URL(string: url) else { throw IconError.invalidBaseURL } |
367 scan(url, completion: completion) | 367 scan(url, completion: completion) |
368 } | 368 } |
369 | 369 |
370 /// Convenience overload for `downloadAll(url:completion:)` that takes a `St
ring` | 370 /// Convenience overload for `downloadAll(url:completion:)` that takes a `St
ring` |
371 /// instead of a `URL` as the URL parameter. Throws an error if the URL is n
ot a valid URL. | 371 /// instead of a `URL` as the URL parameter. Throws an error if the URL is n
ot a valid URL. |
372 /// | 372 /// |
373 /// - parameter url: The URL to scan for icons. | 373 /// - parameter url: The URL to scan for icons. |
374 /// - parameter completion: A closure to call when all download tasks have r
esults available | 374 /// - parameter completion: A closure to call when all download tasks have r
esults available |
375 /// (successful or otherwise). The closure will be c
alled on the main queue. | 375 /// (successful or otherwise). The closure will be c
alled on the main queue. |
(...skipping 25 matching lines...) Expand all Loading... |
401 | 401 |
402 extension DetectedIcon { | 402 extension DetectedIcon { |
403 /// The area of a detected icon, if known. | 403 /// The area of a detected icon, if known. |
404 var area: Int? { | 404 var area: Int? { |
405 if let width = width, let height = height { | 405 if let width = width, let height = height { |
406 return width * height | 406 return width * height |
407 } | 407 } |
408 return nil | 408 return nil |
409 } | 409 } |
410 } | 410 } |
LEFT | RIGHT |