Fix url parsing

This commit is contained in:
Arkadiusz Fal 2022-11-18 22:27:21 +01:00
parent a629bec1ff
commit 2dbc50dc71
2 changed files with 24 additions and 6 deletions

View File

@ -133,7 +133,7 @@ struct VideoDescription: View {
if var components = URLComponents(url: url, resolvingAgainstBaseURL: false) { if var components = URLComponents(url: url, resolvingAgainstBaseURL: false) {
components.scheme = "yattee" components.scheme = "yattee"
if let yatteeURL = components.url { if let yatteeURL = components.url {
let parser = URLParser(url: urlToOpen) let parser = URLParser(url: urlToOpen, allowFileURLs: false)
let destination = parser.destination let destination = parser.destination
if destination == .video, if destination == .video,
parser.videoID == player.currentVideo?.videoID, parser.videoID == player.currentVideo?.videoID,

View File

@ -15,13 +15,17 @@ struct URLParser {
} }
var url: URL var url: URL
var allowFileURLs = true
init(url: URL) { init(url: URL, allowFileURLs: Bool = true) {
self.url = url self.url = url
self.allowFileURLs = allowFileURLs
let urlString = url.absoluteString let urlString = url.absoluteString
let scheme = urlComponents?.scheme let scheme = urlComponents?.scheme
if scheme == nil, if scheme == nil,
urlString.contains("youtube.com"), urlString.contains("youtube.com") ||
urlString.contains("youtu.be") ||
urlString.contains("youtube-nocookie.com"),
let url = URL(string: "https://\(urlString)" let url = URL(string: "https://\(urlString)"
) )
{ {
@ -48,7 +52,7 @@ struct URLParser {
return .channel return .channel
} }
return .fileURL return allowFileURLs ? .fileURL : nil
} }
return .video return .video
@ -56,8 +60,22 @@ struct URLParser {
var isYoutubeHost: Bool { var isYoutubeHost: Bool {
guard let urlComponents else { return false } guard let urlComponents else { return false }
let hostComponents = (urlComponents.host ?? "").components(separatedBy: ".").prefix(2)
return urlComponents.host == "youtube.com" || urlComponents.host == "www.youtube.com" if hostComponents.contains("youtube") || hostComponents.contains("youtube-nocookie") {
return true
}
let host = hostComponents.joined(separator: ".")
.replacingFirstOccurrence(of: "www.", with: "")
return host == "youtu.be"
}
var isYoutube: Bool {
guard let urlComponents else { return false }
return urlComponents.host == "youtube.com" || urlComponents.host == "www.youtube.com" || urlComponents.host == "youtu.be"
} }
var isShortsPath: Bool { var isShortsPath: Bool {
@ -65,7 +83,7 @@ struct URLParser {
} }
var fileURL: URL? { var fileURL: URL? {
guard destination == .fileURL else { return nil } guard allowFileURLs, destination == .fileURL else { return nil }
return url return url
} }