damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 7b6d3ef9dfb471e9030aa9e7792b4636127f8997
parent bc586860163042ed44d6d0874751bdaf0d6dcaca
Author: William Casarin <jb55@jb55.com>
Date:   Wed, 15 Mar 2023 16:43:01 -0600

Refactor image uploader

Diffstat:
Mdamus/Views/AttachMediaUtility.swift | 67+++++++++++++++++++++++++++++++++++++++++--------------------------
1 file changed, 41 insertions(+), 26 deletions(-)

diff --git a/damus/Views/AttachMediaUtility.swift b/damus/Views/AttachMediaUtility.swift @@ -22,15 +22,25 @@ extension PostView { let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in - if error != nil { - print("error=\(error!)") + if let error { + print("error=\(error)") + return + } + + guard let data else { + return + } + + guard let responseString = String(data: data, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) else { + return + } + print("response data = \(responseString)") + + guard let url = imageUploader.getImageURL(from: responseString) else { return } - let responseString = String(data: data!, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) - print("response data = \(responseString!)") - - let uploadedImageURL = NSMutableAttributedString(string: imageUploader.getImageURL(from: responseString), attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18.0), NSAttributedString.Key.foregroundColor: UIColor.label]) + let uploadedImageURL = NSMutableAttributedString(string: url, attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18.0), NSAttributedString.Key.foregroundColor: UIColor.label]) let combinedAttributedString = NSMutableAttributedString() combinedAttributedString.append(post) combinedAttributedString.append(uploadedImageURL) @@ -118,8 +128,10 @@ extension PostView { extension NSMutableData { func appendString(string: String) { - let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true) - append(data!) + guard let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true) else { + return + } + append(data) } } @@ -172,29 +184,32 @@ enum ImageUploader: String, CaseIterable, Identifiable { } } - func getImageURL(from responseString: String?) -> String { + func getImageURL(from responseString: String) -> String? { switch self { case .nostrBuild: - if let startIndex = responseString?.range(of: "nostr.build_")?.lowerBound, - let stringContainingName = responseString?[startIndex..<responseString!.endIndex], - let endIndex = stringContainingName.range(of: "<")?.lowerBound, - let nostrBuildImageName = responseString?[startIndex..<endIndex] { - let nostrBuildURL = "https://nostr.build/i/\(nostrBuildImageName)" - return nostrBuildURL - } else { - return "" + guard let startIndex = responseString.range(of: "nostr.build_")?.lowerBound else { + return nil } + + let stringContainingName = responseString[startIndex..<responseString.endIndex] + guard let endIndex = stringContainingName.range(of: "<")?.lowerBound else { + return nil + } + let nostrBuildImageName = responseString[startIndex..<endIndex] + let nostrBuildURL = "https://nostr.build/i/\(nostrBuildImageName)" + return nostrBuildURL + case .nostrImg: - if let startIndex = responseString?.range(of: "https://i.nostrimg.com/")?.lowerBound, - let stringContainingName = responseString?[startIndex..<responseString!.endIndex], - let endIndex = stringContainingName.range(of: "\"")?.lowerBound, - let nostrBuildImageName = responseString?[startIndex..<endIndex] { - let nostrBuildURL = "\(nostrBuildImageName)" - return nostrBuildURL - } else { - return "" + guard let startIndex = responseString.range(of: "https://i.nostrimg.com/")?.lowerBound else { + return nil + } + let stringContainingName = responseString[startIndex..<responseString.endIndex] + guard let endIndex = stringContainingName.range(of: "\"")?.lowerBound else { + return nil } - + let nostrBuildImageName = responseString[startIndex..<endIndex] + let nostrBuildURL = "\(nostrBuildImageName)" + return nostrBuildURL } } }