Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NostrMedia.com as media uploader option #2571

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions damus/Models/MediaUploader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ enum MediaUploader: String, CaseIterable, Identifiable, StringCodable {
var id: String { self.rawValue }
case nostrBuild
case nostrcheck
case nostrMedia // New case for nostrMedia
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code comment can be removed.


init?(from string: String) {
guard let mu = MediaUploader(rawValue: string) else {
Expand All @@ -35,9 +36,7 @@ enum MediaUploader: String, CaseIterable, Identifiable, StringCodable {

var supportsVideo: Bool {
switch self {
case .nostrBuild:
return true
case .nostrcheck:
case .nostrBuild, .nostrcheck, .nostrMedia:
return true
}
}
Expand All @@ -55,6 +54,8 @@ enum MediaUploader: String, CaseIterable, Identifiable, StringCodable {
return .init(index: -1, tag: "nostrBuild", displayName: "nostr.build")
case .nostrcheck:
return .init(index: 0, tag: "nostrcheck", displayName: "nostrcheck.me")
case .nostrMedia:
return .init(index: 1, tag: "nostrMedia", displayName: "NostrMedia.com")
}
}

Expand All @@ -64,6 +65,8 @@ enum MediaUploader: String, CaseIterable, Identifiable, StringCodable {
return "https://nostr.build/api/v2/nip96/upload"
case .nostrcheck:
return "https://nostrcheck.me/api/v2/media"
case .nostrMedia:
return "https://nostrmedia.com/upload"
}
}

Expand Down
46 changes: 23 additions & 23 deletions damus/Views/AttachMediaUtility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,38 @@ enum ImageUploadResult {
}

fileprivate func create_upload_body(mediaData: Data, boundary: String, mediaUploader: MediaUploader, mediaToUpload: MediaUpload) -> Data {
let body = NSMutableData();
let contentType = mediaToUpload.mime_type
body.appendString(string: "Content-Type: multipart/form-data; boundary=\(boundary)\r\n\r\n")
body.appendString(string: "--\(boundary)\r\n")
body.appendString(string: "Content-Disposition: form-data; name=\(mediaUploader.nameParam); filename=\(mediaToUpload.genericFileName)\r\n")
body.appendString(string: "Content-Type: \(contentType)\r\n\r\n")
body.append(mediaData as Data)
body.appendString(string: "\r\n")
body.appendString(string: "--\(boundary)--\r\n")
return body as Data
}
let body = NSMutableData()
let contentType = mediaToUpload.mime_type
body.appendString(string: "Content-Type: multipart/form-data; boundary=\(boundary)\r\n\r\n")
body.appendString(string: "--\(boundary)\r\n")
body.appendString(string: "Content-Disposition: form-data; name=\(mediaUploader.nameParam); filename=\(mediaToUpload.genericFileName)\r\n")
body.appendString(string: "Content-Type: \(contentType)\r\n\r\n")
body.append(mediaData as Data)
body.appendString(string: "\r\n")
body.appendString(string: "--\(boundary)--\r\n")
return body as Data
}

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)
}

var request = URLRequest(url: url)
request.httpMethod = "POST";
request.httpMethod = "POST"
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 || mediaUploader == .nostrcheck,

// If uploading to a media host that supports NIP-98 authorization, add the header
if mediaUploader == .nostrBuild || mediaUploader == .nostrcheck || mediaUploader == .nostrMedia,
let keypair,
let method = request.httpMethod,
let signature = create_nip98_signature(keypair: keypair, method: method, url: url) {
let method = request.httpMethod,
let signature = create_nip98_signature(keypair: keypair, method: method, url: url) {

request.setValue(signature, forHTTPHeaderField: "Authorization")
request.setValue(signature, forHTTPHeaderField: "Authorization")
}

switch mediaToUpload {
case .image(let url):
do {
Expand All @@ -68,17 +68,17 @@ func create_upload_request(mediaToUpload: MediaUpload, mediaUploader: MediaUploa
}

request.httpBody = create_upload_body(mediaData: mediaData, boundary: boundary, mediaUploader: mediaUploader, mediaToUpload: mediaToUpload)

do {
let (data, _) = try await URLSession.shared.data(for: request, delegate: progress)

guard let url = mediaUploader.getMediaURL(from: data) else {
print("Upload failed getting media url")
return .failed(nil)
}

return .success(url)

} catch {
return .failed(error)
}
Expand Down