-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathURL+Extensions.swift
More file actions
52 lines (45 loc) · 1.52 KB
/
URL+Extensions.swift
File metadata and controls
52 lines (45 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// AVURLAssetExtensions.swift
// THEOplayer_SDK
//
// Copyright © 2023 THEOplayer. All rights reserved.
//
import Foundation
import AVFoundation
extension URL {
func withScheme(newScheme: URLScheme) -> URL? {
guard let oldScheme = self.scheme else {
return nil
}
guard let range = self.absoluteString.range(of: oldScheme + "://") else {
return nil
}
return URL(string: self.absoluteString.replacingCharacters(in: range, with: newScheme.urlScheme))
}
func withScheme(newScheme: String) -> URL? {
guard let oldScheme = self.scheme else {
return nil
}
guard let range = self.absoluteString.range(of: oldScheme + "://") else {
return nil
}
return URL(string: self.absoluteString.replacingCharacters(in: range, with: newScheme + "://"))
}
var isValid: Bool {
guard self.scheme != nil else {
print("[AVSubtitlesLoader] URL scheme is invalid or missing.")
return false
}
guard let host = self.host,
!host.isEmpty else {
print("[AVSubtitlesLoader] URL host is invalid or missing.")
return false
}
let disallowedCharacterSet = CharacterSet.urlQueryAllowed.inverted
guard self.absoluteString.rangeOfCharacter(from: disallowedCharacterSet) == nil else {
print("[AVSubtitlesLoader] URL contains invalid characters.")
return false
}
return true
}
}