forked from shadowfacts/Tusker
Better lenient URL parsing
This commit is contained in:
parent
30cd8991e8
commit
882f03a94b
|
@ -29,15 +29,15 @@ public class Attachment: Decodable {
|
||||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||||
self.id = try container.decode(String.self, forKey: .id)
|
self.id = try container.decode(String.self, forKey: .id)
|
||||||
self.kind = try container.decode(Kind.self, forKey: .kind)
|
self.kind = try container.decode(Kind.self, forKey: .kind)
|
||||||
self.url = URL(string: try container.decode(String.self, forKey: .url).replacingOccurrences(of: " ", with: "%20"))! // Pleroma returns spaces in attachment URLs
|
self.url = URL(lenient: try container.decode(String.self, forKey: .url))!
|
||||||
if let remote = try? container.decode(String.self, forKey: .remoteURL) {
|
if let remote = try? container.decode(String.self, forKey: .remoteURL) {
|
||||||
self.remoteURL = URL(string: remote.replacingOccurrences(of: " ", with: "%20"))
|
self.remoteURL = URL(lenient: remote.replacingOccurrences(of: " ", with: "%20"))
|
||||||
} else {
|
} else {
|
||||||
self.remoteURL = nil
|
self.remoteURL = nil
|
||||||
}
|
}
|
||||||
self.previewURL = URL(string: try container.decode(String.self, forKey: .previewURL).replacingOccurrences(of: " ", with: "%20"))!
|
self.previewURL = URL(lenient: try container.decode(String.self, forKey: .previewURL).replacingOccurrences(of: " ", with: "%20"))!
|
||||||
if let text = try? container.decode(String.self, forKey: .textURL) {
|
if let text = try? container.decode(String.self, forKey: .textURL) {
|
||||||
self.textURL = URL(string: text.replacingOccurrences(of: " ", with: "%20"))
|
self.textURL = URL(lenient: text.replacingOccurrences(of: " ", with: "%20"))
|
||||||
} else {
|
} else {
|
||||||
self.textURL = nil
|
self.textURL = nil
|
||||||
}
|
}
|
||||||
|
@ -113,3 +113,14 @@ extension Attachment {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fileprivate extension URL {
|
||||||
|
private static let allowedChars = CharacterSet.urlHostAllowed.union(.urlPathAllowed)
|
||||||
|
|
||||||
|
init?(lenient string: String) {
|
||||||
|
guard let escaped = string.addingPercentEncoding(withAllowedCharacters: URL.allowedChars) else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
self.init(string: escaped)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue