damus

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

commit c464a26151046e17cd469f390a3212b8af1c9c8e
parent 9104ddb051705adf354f4e4d41ad58c9627ece18
Author: Fishcake <me@fshck.com>
Date:   Sun, 20 Aug 2023 10:10:24 +0900

use nostr.build api v2 with optional nip98 support

Closes: https://github.com/damus-io/damus/pull/1471
Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Mdamus/Models/ImageUploadModel.swift | 4++--
Mdamus/Views/AttachMediaUtility.swift | 34+++++++++++++++++++++++++++++++---
Mdamus/Views/PostView.swift | 2+-
3 files changed, 34 insertions(+), 6 deletions(-)

diff --git a/damus/Models/ImageUploadModel.swift b/damus/Models/ImageUploadModel.swift @@ -47,8 +47,8 @@ enum MediaUpload { class ImageUploadModel: NSObject, URLSessionTaskDelegate, ObservableObject { @Published var progress: Double? = nil - func start(media: MediaUpload, uploader: MediaUploader) async -> ImageUploadResult { - let res = await create_upload_request(mediaToUpload: media, mediaUploader: uploader, progress: self) + func start(media: MediaUpload, uploader: MediaUploader, keypair: Keypair? = nil) async -> ImageUploadResult { + let res = await create_upload_request(mediaToUpload: media, mediaUploader: uploader, progress: self, keypair: keypair) DispatchQueue.main.async { self.progress = nil } diff --git a/damus/Views/AttachMediaUtility.swift b/damus/Views/AttachMediaUtility.swift @@ -28,7 +28,7 @@ fileprivate func create_upload_body(mediaData: Data, boundary: String, mediaUplo return body as Data } -func create_upload_request(mediaToUpload: MediaUpload, mediaUploader: MediaUploader, progress: URLSessionTaskDelegate) async -> ImageUploadResult { +func create_upload_request(mediaToUpload: MediaUpload, mediaUploader: MediaUploader, progress: URLSessionTaskDelegate, keypair: Keypair? = nil) async -> ImageUploadResult { var mediaData: Data? guard let url = URL(string: mediaUploader.postAPI) else { return .failed(nil) @@ -39,6 +39,15 @@ func create_upload_request(mediaToUpload: MediaUpload, mediaUploader: MediaUploa let boundary = "Boundary-\(UUID().description)" request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type") + // If uploading to a media host that support NIP-98 authorization, add the header + if mediaUploader == .nostrBuild, + let keypair, + let method = request.httpMethod, + let signature = create_nip98_signature(keypair: keypair, method: method, url: url) { + + request.setValue(signature, forHTTPHeaderField: "Authorization") + } + switch mediaToUpload { case .image(let url): do { @@ -139,7 +148,7 @@ enum MediaUploader: String, CaseIterable, Identifiable, StringCodable { var postAPI: String { switch self { case .nostrBuild: - return "https://nostr.build/api/upload/ios.php" + return "https://nostr.build/api/v2/upload/files" case .nostrImg: return "https://nostrimg.com/api/upload" } @@ -149,11 +158,30 @@ enum MediaUploader: String, CaseIterable, Identifiable, StringCodable { switch self { case .nostrBuild: do { - return try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? String + if let jsonObject = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any], + let status = jsonObject["status"] as? String { + + if status == "success", let dataArray = jsonObject["data"] as? [[String: Any]] { + + var urls: [String] = [] + + for dataDict in dataArray { + if let mainUrl = dataDict["url"] as? String { + urls.append(mainUrl) + } + } + + return urls.joined(separator: "\n") + } else if status == "error", let message = jsonObject["message"] as? String { + print("Upload Error: \(message)") + return nil + } + } } catch { print("Failed JSONSerialization") return nil } + return nil case .nostrImg: guard let responseString = String(data: data, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) else { print("Upload failed getting response string") diff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift @@ -255,7 +255,7 @@ struct PostView: View { let img = getImage(media: media) print("img size w:\(img.size.width) h:\(img.size.height)") async let blurhash = calculate_blurhash(img: img) - let res = await image_upload.start(media: media, uploader: uploader) + let res = await image_upload.start(media: media, uploader: uploader, keypair: damus_state.keypair) switch res { case .success(let url):