Skip to content

Commit 4b52f85

Browse files
authored
Merge pull request #4009 from axmanalad/main
Fix download progress reset conditionally
2 parents 0bcf0c5 + b83ecca commit 4b52f85

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

src/NexusMods.Networking.HttpDownloader/HttpDownloadJob.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,27 +117,28 @@ private async ValueTask<AbsolutePath> StartAsyncImpl(IJobContext<HttpDownloadJob
117117
await context.YieldAsync();
118118
await using var fileStream = Destination.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
119119

120+
fileStream.Position = (long)_state.TotalBytesDownloaded.Value;
121+
120122
await using var outputStream = new StreamProgressWrapper<IJobContext<HttpDownloadJob>>(
121123
fileStream,
122124
context,
123125
(state, tuple) =>
124-
{
125-
var (bytesWritten, speed) = tuple;
126+
{
127+
var (bytesWritten, speed) = tuple;
126128

127-
_state.TotalBytesDownloaded = bytesWritten;
128-
129-
state.SetPercent(bytesWritten, _state.ContentLength.ValueOr(static () => Size.One));
130-
state.SetRateOfProgress(speed);
131-
});
129+
_state.TotalBytesDownloaded = bytesWritten;
130+
131+
state.SetPercent(bytesWritten, _state.ContentLength.ValueOr(static () => Size.One));
132+
state.SetRateOfProgress(speed);
133+
}
134+
);
132135

133136
if (_state.ContentLength.HasValue)
134137
{
135138
var contentLength = (long)_state.ContentLength.Value.Value;
136139
if (outputStream.Length != contentLength) outputStream.SetLength(contentLength);
137140
}
138141

139-
outputStream.Position = (long)_state.TotalBytesDownloaded.Value;
140-
141142
await context.YieldAsync();
142143
using var request = PrepareRequest(out var isRangeRequest);
143144
using var response = await Client.SendAsync(

src/NexusMods.Sdk/Jobs/StreamProgressWrapper.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public StreamProgressWrapper(
3131
timeProvider ??= TimeProvider.System;
3232

3333
_timer = timeProvider.CreateTimer(NotifyLoop, state: this, dueTime: TimeSpan.Zero, period: _period);
34+
35+
var pos = Size.FromLong(_innerStream.Position);
36+
_currentBytesWritten = pos;
37+
_lastBytesWritten = pos;
3438
}
3539

3640
private readonly TimeSpan _period;

0 commit comments

Comments
 (0)