From db68b9dee0591e0fee1db34680cd8fa1996b90ed Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 13 Jan 2019 19:45:57 -0500 Subject: [PATCH] Fix crash when decoding status attachments with non-percent encoded spaces in URLs --- Pachyderm/Model/Attachment.swift | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Pachyderm/Model/Attachment.swift b/Pachyderm/Model/Attachment.swift index 063f7065..7543a509 100644 --- a/Pachyderm/Model/Attachment.swift +++ b/Pachyderm/Model/Attachment.swift @@ -25,6 +25,26 @@ public class Attachment: Decodable { ], nil)) } + required public init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + self.id = try container.decode(String.self, forKey: .id) + 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 + if let remote = try? container.decode(String.self, forKey: .remoteURL) { + self.remoteURL = URL(string: remote.replacingOccurrences(of: " ", with: "%20")) + } else { + self.remoteURL = nil + } + self.previewURL = URL(string: try container.decode(String.self, forKey: .previewURL).replacingOccurrences(of: " ", with: "%20"))! + if let text = try? container.decode(String.self, forKey: .textURL) { + self.textURL = URL(string: text.replacingOccurrences(of: " ", with: "%20")) + } else { + self.textURL = nil + } + self.meta = try? container.decode(Metadata.self, forKey: .meta) + self.description = try? container.decode(String.self, forKey: .description) + } + private enum CodingKeys: String, CodingKey { case id case kind = "type"