damus

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

commit 2048e68d67d6efa219ece0c20fa6fb37d8ac02f7
parent 624d9662d7f0c91fa8456928ddcafd2ff81f5534
Author: Swift <scoder1747@gmail.com>
Date:   Mon, 24 Apr 2023 11:55:01 -0400

Fix issue where uploaded images were from someone else

Changelog-Fixed: Fix issue where uploaded images were from someone else

Diffstat:
Mdamus/Views/AttachMediaUtility.swift | 30++++++++++++------------------
1 file changed, 12 insertions(+), 18 deletions(-)

diff --git a/damus/Views/AttachMediaUtility.swift b/damus/Views/AttachMediaUtility.swift @@ -62,13 +62,8 @@ func create_upload_request(mediaToUpload: MediaUpload, mediaUploader: MediaUploa do { let (data, _) = try await URLSession.shared.data(for: request, delegate: progress) - - guard let responseString = String(data: data, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) else { - print("Upload failed getting response string") - return .failed(nil) - } - - guard let url = mediaUploader.getMediaURL(from: responseString, mediaIsImage: mediaToUpload.is_image) else { + + guard let url = mediaUploader.getMediaURL(from: data) else { print("Upload failed getting media url") return .failed(nil) } @@ -144,28 +139,27 @@ enum MediaUploader: String, CaseIterable, Identifiable, StringCodable { var postAPI: String { switch self { case .nostrBuild: - return "https://nostr.build/upload.php" + return "https://nostr.build/api/upload/ios.php" case .nostrImg: return "https://nostrimg.com/api/upload" } } - func getMediaURL(from responseString: String, mediaIsImage: Bool) -> String? { + func getMediaURL(from data: Data) -> String? { switch self { case .nostrBuild: - guard let startIndex = responseString.range(of: "nostr.build_")?.lowerBound else { + do { + return try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? String + } catch { + print("Failed JSONSerialization") return nil } - - let stringContainingName = responseString[startIndex..<responseString.endIndex] - guard let endIndex = stringContainingName.range(of: "<")?.lowerBound else { + case .nostrImg: + guard let responseString = String(data: data, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) else { + print("Upload failed getting response string") return nil } - let nostrBuildImageName = responseString[startIndex..<endIndex] - let nostrBuildURL = mediaIsImage ? "https://nostr.build/i/\(nostrBuildImageName)" : "https://nostr.build/av/\(nostrBuildImageName)" - return nostrBuildURL - - case .nostrImg: + guard let startIndex = responseString.range(of: "https://i.nostrimg.com/")?.lowerBound else { return nil }