-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1207 from planetary-social/bugfix/upload
Fix uploading photos and videos to nostr.build
- Loading branch information
Showing
7 changed files
with
156 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import Foundation | ||
|
||
/// The response JSON that's returned from the nostr.build API. | ||
struct NostrBuildResponseJSON: Codable { | ||
let status: NostrBuildResponseStatus? | ||
let message: String? | ||
let data: [NostrBuildResponseDataItem]? | ||
} | ||
|
||
enum NostrBuildResponseStatus: String, Codable { | ||
case success | ||
case unknown | ||
|
||
init(from decoder: Decoder) throws { | ||
let container = try decoder.singleValueContainer() | ||
let statusString = try container.decode(String.self) | ||
switch statusString { | ||
case "success": | ||
self = .success | ||
default: | ||
self = .unknown | ||
} | ||
} | ||
} | ||
|
||
struct NostrBuildResponseDataItem: Codable { | ||
let url: String? | ||
let type: String? | ||
let mime: String? | ||
let thumbnail: String? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"status": "success", | ||
"message": "Upload successful.", | ||
"data": [ | ||
{ | ||
"input_name": "APIv2", | ||
"name": "d9a71e94e59ab36ce13dd6998dcdb34527acdd2781cb6b91858ea0a8ca8557b1.jpeg", | ||
"sha256": "", | ||
"original_sha256": "d9a71e94e59ab36ce13dd6998dcdb34527acdd2781cb6b91858ea0a8ca8557b1", | ||
"type": "picture", | ||
"mime": "image/jpeg", | ||
"size": 526284, | ||
"blurhash": "L9C;-XQS.9roDg^,D$-pHSD$%NnM", | ||
"dimensions": { | ||
"width": 4032, | ||
"height": 3024 | ||
}, | ||
"dimensionsString": "4032x3024", | ||
"url": "https://image.nostr.build/d9a71e94e59ab36ce13dd6998dcdb34527acdd2781cb6b91858ea0a8ca8557b1.jpeg", | ||
"thumbnail": "https://image.nostr.build/thumb/d9a71e94e59ab36ce13dd6998dcdb34527acdd2781cb6b91858ea0a8ca8557b1.jpeg", | ||
"responsive": { | ||
"240p": "https://image.nostr.build/resp/240p/d9a71e94e59ab36ce13dd6998dcdb34527acdd2781cb6b91858ea0a8ca8557b1.jpeg", | ||
"360p": "https://image.nostr.build/resp/360p/d9a71e94e59ab36ce13dd6998dcdb34527acdd2781cb6b91858ea0a8ca8557b1.jpeg", | ||
"480p": "https://image.nostr.build/resp/480p/d9a71e94e59ab36ce13dd6998dcdb34527acdd2781cb6b91858ea0a8ca8557b1.jpeg", | ||
"720p": "https://image.nostr.build/resp/720p/d9a71e94e59ab36ce13dd6998dcdb34527acdd2781cb6b91858ea0a8ca8557b1.jpeg", | ||
"1080p": "https://image.nostr.build/resp/1080p/d9a71e94e59ab36ce13dd6998dcdb34527acdd2781cb6b91858ea0a8ca8557b1.jpeg" | ||
}, | ||
"metadata": [] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import XCTest | ||
|
||
class NostrBuildResponseJSONTests: XCTestCase { | ||
/// Verifies that we can properly decode a response frome the nostr.build API. | ||
func testDecoding() throws { | ||
// Arrange | ||
let jsonData = try jsonData(filename: "nostr_build_response") | ||
let decoder = JSONDecoder() | ||
decoder.keyDecodingStrategy = .convertFromSnakeCase | ||
|
||
// Act | ||
let subject = try decoder.decode(NostrBuildResponseJSON.self, from: jsonData) | ||
|
||
// Assert | ||
XCTAssertEqual(subject.status, .success) | ||
XCTAssertEqual(subject.message, "Upload successful.") | ||
|
||
let photo = try XCTUnwrap(subject.data?.first) | ||
XCTAssertEqual( | ||
photo.url, | ||
"https://image.nostr.build/d9a71e94e59ab36ce13dd6998dcdb34527acdd2781cb6b91858ea0a8ca8557b1.jpeg" | ||
) | ||
XCTAssertEqual( | ||
photo.thumbnail, | ||
"https://image.nostr.build/thumb/d9a71e94e59ab36ce13dd6998dcdb34527acdd2781cb6b91858ea0a8ca8557b1.jpeg" | ||
) | ||
XCTAssertEqual(photo.mime, "image/jpeg") | ||
XCTAssertEqual(photo.type, "picture") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import XCTest | ||
|
||
extension XCTestCase { | ||
func jsonData(filename: String) throws -> Data { | ||
let url = try XCTUnwrap(Bundle.current.url(forResource: filename, withExtension: "json")) | ||
return try Data(contentsOf: url) | ||
} | ||
} |