This commit is contained in:
snowspeeder committed 2026-08-31 19:50:46 -04:00
1 parent c9c25f86eb
commit 40aaf4fea7
56 files changed
+5683 -179

No files matched your search

+20 -16
View File
@@ -182,16 +182,11 @@ final class MediaAPI {
try await post("streams", body: ["room": room, "name": name, "url": url])
}
func streamURL(trackID: String) async throws -> URL {
let data = try await getRaw(url: baseURL.appendingPathComponent("stream").appending(path: trackID))
guard let raw = String(data: data, encoding: .utf8), !raw.isEmpty else {
throw MediaAPIError.decodeFailed
}
if let url = URL(string: raw) { return url }
if raw.hasPrefix("/") {
return serverBase.appendingPathComponent(String(raw.dropFirst()))
}
return serverBase.appendingPathComponent(raw)
/// Returns the progressive-media endpoint URL for a track. The `/stream/{id}`
/// path serves raw media bytes (not a text URL), so this constructs the
/// endpoint without downloading or decoding the body.
func streamURL(trackID: String) -> URL {
baseURL.appendingPathComponent("stream").appending(path: trackID)
}
func healthz() async throws -> Bool {
@@ -218,11 +213,24 @@ final class MediaAPI {
return data
}
/// Fire-and-forget POST that only validates HTTP success. The media server
/// returns `{"ok": true, ...snapshot}` for /play, /control, /report and
/// /streams — callers don't need the body, so decoding it (as Data or any
/// typed model) would be wasted work at best and a spurious failure mode.
private func post(_ path: String, body: [String: Any]) async throws {
_ = try await postDecoded(path, body: body, as: Data.self)
_ = try await postRaw(path, body: body)
}
private func postDecoded<T: Decodable>(_ path: String, body: [String: Any], as type: T.Type) async throws -> T {
let data = try await postRaw(path, body: body)
do {
return try JSONDecoder().decode(T.self, from: data)
} catch {
throw MediaAPIError.decodeFailed
}
}
private func postRaw(_ path: String, body: [String: Any]) async throws -> Data {
var request = URLRequest(url: baseURL.appendingPathComponent(path))
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
@@ -236,10 +244,6 @@ final class MediaAPI {
}
throw MediaAPIError.httpStatus(http.statusCode, detail)
}
do {
return try JSONDecoder().decode(T.self, from: data)
} catch {
throw MediaAPIError.decodeFailed
}
return data
}
}