Skip to content

Commit

Permalink
Fix composition of image URL and Base URL.
Browse files Browse the repository at this point in the history
When an image URL is not an absolute URL (ie. protocol://url), we want
to put the base URL in front of it, if it is known.

BUT, if neither the base URL ends in / or the image URL start with /,
the current formatImageURL mashes them together without a slash, leading
to something like https://base.comimages/image.jpg
  • Loading branch information
lhunath committed Jun 7, 2022
1 parent 415b3a9 commit 5cbefb7
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions Sources/SwiftLinkPreview.swift
Expand Up @@ -157,13 +157,10 @@ open class SwiftLinkPreview: NSObject {
}

private func formatImageURL(_ url: String?, base: String?) -> String? {
guard var url = url else { return nil }
guard let base = base, let url = url, !url.contains( "://" )
else { return url }

if !url.starts(with: "http"), let base = base {
url = "\(base)\(url)"
}

return url
return base + (base.hasSuffix("/") || url.hasPrefix("/") ? "" : "/") + url
}

func formatImageURLs(_ array: [String]?, base: String?) -> [String]? {
Expand Down

0 comments on commit 5cbefb7

Please sign in to comment.