Skip to content

Commit

Permalink
# This is a combination of 4 commits.
Browse files Browse the repository at this point in the history
# This is the 1st commit message:

Remove non-functioning servers, add nostrcheck, use NIP96 endpoints for all servers

# This is the commit message damus-io#2:

restore typo

# This is the commit message damus-io#3:

restore typo(2)

# This is the commit message damus-io#4:

Remove non-functioning servers, add nostrcheck, use NIP96 endpoints for all servers

restore typo

restore typo(2)
  • Loading branch information
quentintaranpino committed Aug 27, 2024
1 parent e8fae19 commit a8cd34f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 55 deletions.
86 changes: 32 additions & 54 deletions damus/Models/MediaUploader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
enum MediaUploader: String, CaseIterable, Identifiable, StringCodable {
var id: String { self.rawValue }
case nostrBuild
case nostrImg
case nostrcheck

init?(from string: String) {
guard let mu = MediaUploader(rawValue: string) else {
Expand All @@ -23,95 +23,73 @@ enum MediaUploader: String, CaseIterable, Identifiable, StringCodable {
func to_string() -> String {
return rawValue
}

var nameParam: String {
switch self {
case .nostrBuild:
return "\"fileToUpload\""
case .nostrImg:
return "\"image\""
default:
return "\"file\""
}
}

var supportsVideo: Bool {
switch self {
case .nostrBuild:
return true
case .nostrImg:
return false
case .nostrcheck:
return true
}
}

struct Model: Identifiable, Hashable {
var id: String { self.tag }
var index: Int
var tag: String
var displayName : String
}

var model: Model {
switch self {
case .nostrBuild:
return .init(index: -1, tag: "nostrBuild", displayName: "nostr.build")
case .nostrImg:
return .init(index: 0, tag: "nostrImg", displayName: "nostrimg.com")
case .nostrcheck:
return .init(index: 0, tag: "nostrcheck", displayName: "nostrcheck.me")
}
}



var postAPI: String {
switch self {
case .nostrBuild:
return "https://nostr.build/api/v2/upload/files"
case .nostrImg:
return "https://nostrimg.com/api/upload"
return "https://nostr.build/api/v2/nip96/upload"
case .nostrcheck:
return "https://nostrcheck.me/api/v2/media"
}
}

func getMediaURL(from data: Data) -> String? {
switch self {
case .nostrBuild:
do {
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)
do {
if let jsonObject = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any],
let status = jsonObject["status"] as? String {

if status == "success", let nip94Event = jsonObject["nip94_event"] as? [String: Any] {

if let tags = nip94Event["tags"] as? [[String]] {
for tagArray in tags {
if tagArray.count > 1, tagArray[0] == "url" {
return tagArray[1]
}
}

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")
return nil
}

guard let startIndex = responseString.range(of: "https://i.nostrimg.com/")?.lowerBound else {
} else if status == "error", let message = jsonObject["message"] as? String {
print("Upload Error: \(message)")
return nil
}
let stringContainingName = responseString[startIndex..<responseString.endIndex]
guard let endIndex = stringContainingName.range(of: "\"")?.lowerBound else {
return nil
}
let nostrBuildImageName = responseString[startIndex..<endIndex]
let nostrBuildURL = "\(nostrBuildImageName)"
return nostrBuildURL
} catch {
print("Failed JSONSerialization")
return nil
}
return nil
}
}
2 changes: 1 addition & 1 deletion damus/Views/AttachMediaUtility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func create_upload_request(mediaToUpload: MediaUpload, mediaUploader: MediaUploa
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,
if mediaUploader == .nostrBuild || mediaUploader == .nostrcheck,
let keypair,
let method = request.httpMethod,
let signature = create_nip98_signature(keypair: keypair, method: method, url: url) {
Expand Down

0 comments on commit a8cd34f

Please sign in to comment.