Skip to content

Commit

Permalink
use nostr.build api v2 with optional nip98 support
Browse files Browse the repository at this point in the history
Closes: damus-io#1471
Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
fishcakeday authored and suhailsaqan committed Sep 19, 2023
1 parent c2aa876 commit fd90f84
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
4 changes: 2 additions & 2 deletions damus/Models/ImageUploadModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
34 changes: 31 additions & 3 deletions damus/Views/AttachMediaUtility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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"
}
Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion damus/Views/PostView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit fd90f84

Please sign in to comment.