From 9ebc4c8b8bfdd035d612bf44eaa4d53f275fd4b4 Mon Sep 17 00:00:00 2001 From: Matt Hayashida Date: Thu, 12 Sep 2024 15:24:28 -0400 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Update=20svg=20mapping=20t?= =?UTF-8?q?o=20support=20new=20image=20domain?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UI/Components/AppcuesImage.swift | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/Sources/AppcuesKit/Presentation/UI/Components/AppcuesImage.swift b/Sources/AppcuesKit/Presentation/UI/Components/AppcuesImage.swift index e567ce24f..d063db797 100644 --- a/Sources/AppcuesKit/Presentation/UI/Components/AppcuesImage.swift +++ b/Sources/AppcuesKit/Presentation/UI/Components/AppcuesImage.swift @@ -82,8 +82,8 @@ private extension URL { return self } - /// Converts a URL like https://res.cloudinary.com/xyz/image/upload/v123/000/abc.svg to - /// https://res.cloudinary.com/xyz/image/upload/h_600,c_scale/v123/000/abc.png + /// Converts a URL like https://images.appcues.com/image/upload/v123/000/abc.svg to + /// https://images.appcues.com/image/upload/h_600,c_scale/v123/000/abc.png private func transformSVG(components: URLComponents?, width: Double? = nil, height: Double? = nil) -> URL? { guard var components = components else { return self } @@ -95,16 +95,27 @@ private extension URL { // Scale the png to the right size so it appears crisp like a vector would. let scale: CGFloat = UIScreen.main.scale var parts = components.path.split(separator: "/") - if parts.count == 6, parts[2] == "upload" { + + let insertIndex: Int? + if parts.count == 5, parts[1] == "upload" { + insertIndex = 2 + } else if parts.count == 6, parts[2] == "upload" { + // backwards compatibility for res.cloudinary.com urls + insertIndex = 3 + } else { + insertIndex = nil + } + + if let insertIndex = insertIndex { if let width = width { // An integer scale value represents a fixed pixel size. - parts.insert("w_\(Int(width * scale)),c_scale", at: 3) + parts.insert("w_\(Int(width * scale)),c_scale", at: insertIndex) } else if let height = height { - parts.insert("h_\(Int(height * scale)),c_scale", at: 3) + parts.insert("h_\(Int(height * scale)),c_scale", at: insertIndex) } else { // If no fixed size, scale the default SVG size by the screen density. // A decimal scale value represents a percentage. - parts.insert("w_\(scale),c_scale", at: 3) + parts.insert("w_\(scale),c_scale", at: insertIndex) } } components.path = "/" + parts.joined(separator: "/")