Refactor image uploader

This commit is contained in:
William Casarin
2023-03-15 16:43:01 -06:00
parent bc58686016
commit 7b6d3ef9df

View File

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