Merge 'Add nostrcheck'

Quentin (1):
      Remove non-functioning servers,add nostrcheck,NIP96 for all servers
This commit is contained in:
William Casarin
2024-08-27 14:23:46 +03:00
2 changed files with 33 additions and 55 deletions

View File

@@ -10,7 +10,7 @@ import Foundation
enum MediaUploader: String, CaseIterable, Identifiable, StringCodable { enum MediaUploader: String, CaseIterable, Identifiable, StringCodable {
var id: String { self.rawValue } var id: String { self.rawValue }
case nostrBuild case nostrBuild
case nostrImg case nostrcheck
init?(from string: String) { init?(from string: String) {
guard let mu = MediaUploader(rawValue: string) else { guard let mu = MediaUploader(rawValue: string) else {
@@ -23,95 +23,73 @@ enum MediaUploader: String, CaseIterable, Identifiable, StringCodable {
func to_string() -> String { func to_string() -> String {
return rawValue return rawValue
} }
var nameParam: String { var nameParam: String {
switch self { switch self {
case .nostrBuild: case .nostrBuild:
return "\"fileToUpload\"" return "\"fileToUpload\""
case .nostrImg: default:
return "\"image\"" return "\"file\""
} }
} }
var supportsVideo: Bool { var supportsVideo: Bool {
switch self { switch self {
case .nostrBuild: case .nostrBuild:
return true return true
case .nostrImg: case .nostrcheck:
return false return true
} }
} }
struct Model: Identifiable, Hashable { struct Model: Identifiable, Hashable {
var id: String { self.tag } var id: String { self.tag }
var index: Int var index: Int
var tag: String var tag: String
var displayName : String var displayName : String
} }
var model: Model { var model: Model {
switch self { switch self {
case .nostrBuild: case .nostrBuild:
return .init(index: -1, tag: "nostrBuild", displayName: "nostr.build") return .init(index: -1, tag: "nostrBuild", displayName: "nostr.build")
case .nostrImg: case .nostrcheck:
return .init(index: 0, tag: "nostrImg", displayName: "nostrimg.com") return .init(index: 0, tag: "nostrcheck", displayName: "nostrcheck.me")
} }
} }
var postAPI: String { var postAPI: String {
switch self { switch self {
case .nostrBuild: case .nostrBuild:
return "https://nostr.build/api/v2/upload/files" return "https://nostr.build/api/v2/nip96/upload"
case .nostrImg: case .nostrcheck:
return "https://nostrimg.com/api/upload" return "https://nostrcheck.me/api/v2/media"
} }
} }
func getMediaURL(from data: Data) -> String? { func getMediaURL(from data: Data) -> String? {
switch self { do {
case .nostrBuild: if let jsonObject = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any],
do { let status = jsonObject["status"] 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 nip94Event = jsonObject["nip94_event"] as? [String: Any] {
if status == "success", let dataArray = jsonObject["data"] as? [[String: Any]] { if let tags = nip94Event["tags"] as? [[String]] {
for tagArray in tags {
var urls: [String] = [] if tagArray.count > 1, tagArray[0] == "url" {
return tagArray[1]
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
} }
} } else if status == "error", let message = jsonObject["message"] as? String {
} catch { print("Upload Error: \(message)")
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 {
return nil return nil
} }
let stringContainingName = responseString[startIndex..<responseString.endIndex]
guard let endIndex = stringContainingName.range(of: "\"")?.lowerBound else {
return nil
} }
let nostrBuildImageName = responseString[startIndex..<endIndex] } catch {
let nostrBuildURL = "\(nostrBuildImageName)" print("Failed JSONSerialization")
return nostrBuildURL return nil
} }
return nil
} }
} }

View File

@@ -40,7 +40,7 @@ func create_upload_request(mediaToUpload: MediaUpload, mediaUploader: MediaUploa
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type") 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 uploading to a media host that support NIP-98 authorization, add the header
if mediaUploader == .nostrBuild, if mediaUploader == .nostrBuild || mediaUploader == .nostrcheck,
let keypair, let keypair,
let method = request.httpMethod, let method = request.httpMethod,
let signature = create_nip98_signature(keypair: keypair, method: method, url: url) { let signature = create_nip98_signature(keypair: keypair, method: method, url: url) {