Skip to content

fix: fix Downloader to dispose tempFile and use synchronous IO #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 30, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions Vpn.Service/Downloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,27 +453,26 @@ private async Task Start(CancellationToken ct = default)
if (res.Content.Headers.ContentLength >= 0)
TotalBytes = (ulong)res.Content.Headers.ContentLength;

FileStream tempFile;
try
{
tempFile = File.Create(TempDestinationPath, BufferSize,
FileOptions.Asynchronous | FileOptions.SequentialScan);
}
catch (Exception e)
{
_logger.LogError(e, "Failed to create temporary file '{TempDestinationPath}'", TempDestinationPath);
throw;
}

await Download(res, tempFile, ct);
await Download(res, ct);
return;
}

private async Task Download(HttpResponseMessage res, FileStream tempFile, CancellationToken ct)
private async Task Download(HttpResponseMessage res, CancellationToken ct)
{
try
{
var sha1 = res.Headers.Contains("ETag") ? SHA1.Create() : null;
FileStream tempFile;
try
{
tempFile = File.Create(TempDestinationPath, BufferSize,
FileOptions.Asynchronous | FileOptions.SequentialScan);
}
catch (Exception e)
{
_logger.LogError(e, "Failed to create temporary file '{TempDestinationPath}'", TempDestinationPath);
throw;
}
await using (tempFile)
{
var stream = await res.Content.ReadAsStreamAsync(ct);
Expand Down
Loading