Skip to content

Commit 08002ef

Browse files
committed
fix: fix Downloader to always dispose of temp file stream
1 parent a58864e commit 08002ef

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

Vpn.Service/Downloader.cs

+13-14
Original file line numberDiff line numberDiff line change
@@ -453,27 +453,26 @@ private async Task Start(CancellationToken ct = default)
453453
if (res.Content.Headers.ContentLength >= 0)
454454
TotalBytes = (ulong)res.Content.Headers.ContentLength;
455455

456-
FileStream tempFile;
457-
try
458-
{
459-
tempFile = File.Create(TempDestinationPath, BufferSize,
460-
FileOptions.Asynchronous | FileOptions.SequentialScan);
461-
}
462-
catch (Exception e)
463-
{
464-
_logger.LogError(e, "Failed to create temporary file '{TempDestinationPath}'", TempDestinationPath);
465-
throw;
466-
}
467-
468-
await Download(res, tempFile, ct);
456+
await Download(res, ct);
469457
return;
470458
}
471459

472-
private async Task Download(HttpResponseMessage res, FileStream tempFile, CancellationToken ct)
460+
private async Task Download(HttpResponseMessage res, CancellationToken ct)
473461
{
474462
try
475463
{
476464
var sha1 = res.Headers.Contains("ETag") ? SHA1.Create() : null;
465+
FileStream tempFile;
466+
try
467+
{
468+
tempFile = File.Create(TempDestinationPath, BufferSize,
469+
FileOptions.Asynchronous | FileOptions.SequentialScan);
470+
}
471+
catch (Exception e)
472+
{
473+
_logger.LogError(e, "Failed to create temporary file '{TempDestinationPath}'", TempDestinationPath);
474+
throw;
475+
}
477476
await using (tempFile)
478477
{
479478
var stream = await res.Content.ReadAsStreamAsync(ct);

0 commit comments

Comments
 (0)