fixed a bug:translate maybe timeout

This commit is contained in:
2026-10-03 19:49:21 +08:00
parent 6bd64ee4a9
commit 9daea7b807
10 changed files with 554 additions and 99 deletions

View File

@@ -100,7 +100,29 @@ extension SentenceBoundaryAPI {
.explanation
}
static func fetchStoredSubtitles(videoHash: String) async throws -> [Int: String] {
try await requestSubtitles(videoHash: videoHash, method: "GET", timeout: AppConfig.readTimeout)
}
static func translateSubtitles(videoHash: String) async throws -> [Int: String] {
try await requestSubtitles(videoHash: videoHash, method: "POST", timeout: AppConfig.translationTimeout)
}
static func loadSubtitles(videoHash: String, expectedIndexes: [Int]) async throws -> [Int: String] {
if let stored = try? await fetchStoredSubtitles(videoHash: videoHash) {
let expected = Set(expectedIndexes)
if expected.isEmpty || expected.isSubset(of: Set(stored.keys)) {
return stored
}
}
return try await translateSubtitles(videoHash: videoHash)
}
private static func requestSubtitles(
videoHash: String,
method: String,
timeout: TimeInterval
) async throws -> [Int: String] {
let hash = videoHash.lowercased()
guard hash.range(of: AppConfig.sha256Pattern, options: .regularExpression) != nil else {
throw SentenceBoundaryAPIError.invalidHash
@@ -112,8 +134,8 @@ extension SentenceBoundaryAPI {
let result = try await HTTPClient.execute(
baseURL: AppConfig.serverBaseURL,
path: "api/v1/videos/\(hash)/subtitle-translation",
method: "POST",
timeout: AppConfig.translationTimeout,
method: method,
timeout: timeout,
headers: headers
)
guard (200...299).contains(result.statusCode) else {