fixed a bug:translate maybe timeout
This commit is contained in:
@@ -15,7 +15,7 @@ enum AppConfig {
|
||||
static let connectTimeout: TimeInterval = 15
|
||||
static let readTimeout: TimeInterval = 30
|
||||
static let assessmentTimeout: TimeInterval = 180
|
||||
static let translationTimeout: TimeInterval = 120
|
||||
static let translationTimeout: TimeInterval = 180
|
||||
|
||||
static let sha256Pattern = #"^[0-9a-fA-F]{64}$"#
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -649,7 +649,14 @@ struct RootView: View {
|
||||
let mediaId = item.id
|
||||
chineseTranslationTask = Task {
|
||||
do {
|
||||
let map = try await SentenceBoundaryAPI.translateSubtitles(videoHash: mediaId)
|
||||
let expectedIndexes = item.sentences.compactMap { sentence -> Int? in
|
||||
let text = (sentence.text ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
return text.isEmpty ? nil : sentence.index
|
||||
}
|
||||
let map = try await SentenceBoundaryAPI.loadSubtitles(
|
||||
videoHash: mediaId,
|
||||
expectedIndexes: expectedIndexes
|
||||
)
|
||||
guard !Task.isCancelled else { return }
|
||||
await MainActor.run {
|
||||
guard controller.currentItem?.id == mediaId else { return }
|
||||
|
||||
Reference in New Issue
Block a user