diff --git a/ProjectPlugins/CodexPlugin/CodexNode.cs b/ProjectPlugins/CodexPlugin/CodexNode.cs index 8b2ea3a..f316e38 100644 --- a/ProjectPlugins/CodexPlugin/CodexNode.cs +++ b/ProjectPlugins/CodexPlugin/CodexNode.cs @@ -264,10 +264,27 @@ namespace CodexPlugin private void DownloadToFile(string contentId, TrackedFile file, Action onFailure) { using var fileStream = File.OpenWrite(file.Filename); + var timeout = tools.TimeSet.HttpCallTimeout(); try { - using var downloadStream = CodexAccess.DownloadFile(contentId, onFailure); - downloadStream.CopyTo(fileStream); + // Type of stream generated by openAPI client does not support timeouts. + var start = DateTime.UtcNow; + var cts = new CancellationTokenSource(); + var downloadTask = Task.Run(() => + { + using var downloadStream = CodexAccess.DownloadFile(contentId, onFailure); + downloadStream.CopyTo(fileStream); + }, cts.Token); + + while (DateTime.UtcNow - start < timeout) + { + if (downloadTask.IsFaulted) throw downloadTask.Exception; + if (downloadTask.IsCompletedSuccessfully) return; + Thread.Sleep(100); + } + + cts.Cancel(); + throw new TimeoutException($"Download of '{contentId}' timed out after {Time.FormatDuration(timeout)}"); } catch {