Skip to content

Commit ddc448a

Browse files
committed
Refactor
1 parent 0cef1c6 commit ddc448a

3 files changed

Lines changed: 92 additions & 28 deletions

File tree

‎YoutubeDownloader.Core/Downloading/FFmpeg.cs‎

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
using System.IO;
44
using System.IO.Compression;
55
using System.Linq;
6-
using System.Net.Http;
76
using System.Runtime.InteropServices;
87
using System.Threading;
98
using System.Threading.Tasks;
109
using Gress;
1110
using YoutubeDownloader.Core.Utils;
11+
using YoutubeDownloader.Core.Utils.Extensions;
1212

1313
namespace YoutubeDownloader.Core.Downloading;
1414

@@ -120,36 +120,12 @@ public static async Task DownloadAsync(
120120

121121
try
122122
{
123-
using var response = await Http.Client.GetAsync(
123+
await Http.Client.DownloadAsync(
124124
GetDownloadUrl(),
125-
HttpCompletionOption.ResponseHeadersRead,
125+
archiveFilePath,
126+
progress,
126127
cancellationToken
127128
);
128-
response.EnsureSuccessStatusCode();
129-
130-
var totalSize = response.Content.Headers.ContentLength;
131-
var downloadedSize = 0L;
132-
var buffer = new byte[81920];
133-
134-
await using var responseStream = await response.Content.ReadAsStreamAsync(
135-
cancellationToken
136-
);
137-
await using (var archiveFile = File.Create(archiveFilePath))
138-
{
139-
int bytesRead;
140-
while ((bytesRead = await responseStream.ReadAsync(buffer, cancellationToken)) > 0)
141-
{
142-
await archiveFile.WriteAsync(buffer.AsMemory(0, bytesRead), cancellationToken);
143-
downloadedSize += bytesRead;
144-
145-
if (totalSize > 0)
146-
{
147-
progress?.Report(
148-
Percentage.FromFraction((double)downloadedSize / totalSize.Value)
149-
);
150-
}
151-
}
152-
}
153129

154130
using var zip = ZipFile.OpenRead(archiveFilePath);
155131
var entry =
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.IO;
3+
using System.Net.Http;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using Gress;
7+
8+
namespace YoutubeDownloader.Core.Utils.Extensions;
9+
10+
public static class HttpExtensions
11+
{
12+
extension(HttpClient http)
13+
{
14+
public async Task DownloadAsync(
15+
string url,
16+
string filePath,
17+
IProgress<Percentage>? progress = null,
18+
CancellationToken cancellationToken = default
19+
)
20+
{
21+
using var response = await http.GetAsync(
22+
url,
23+
HttpCompletionOption.ResponseHeadersRead,
24+
cancellationToken
25+
);
26+
27+
response.EnsureSuccessStatusCode();
28+
29+
await using var source = await response.Content.ReadAsStreamAsync(cancellationToken);
30+
await using var destination = File.Create(filePath);
31+
32+
var totalBytes = response.Content.Headers.ContentLength ?? -1L;
33+
await source.CopyToAsync(destination, totalBytes, progress, cancellationToken);
34+
}
35+
}
36+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Buffers;
3+
using System.IO;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using Gress;
7+
8+
namespace YoutubeDownloader.Core.Utils.Extensions;
9+
10+
public static class StreamExtensions
11+
{
12+
extension(Stream stream)
13+
{
14+
public async Task<int> CopyBufferedToAsync(
15+
Stream destination,
16+
Memory<byte> buffer,
17+
CancellationToken cancellationToken = default
18+
)
19+
{
20+
var bytesCopied = await stream.ReadAsync(buffer, cancellationToken);
21+
await destination.WriteAsync(buffer[..bytesCopied], cancellationToken);
22+
23+
return bytesCopied;
24+
}
25+
26+
public async Task CopyToAsync(
27+
Stream destination,
28+
long totalBytes,
29+
IProgress<Percentage>? progress = null,
30+
CancellationToken cancellationToken = default
31+
)
32+
{
33+
using var buffer = MemoryPool<byte>.Shared.Rent(81920);
34+
35+
var totalBytesCopied = 0L;
36+
int bytesCopied;
37+
do
38+
{
39+
bytesCopied = await stream.CopyBufferedToAsync(
40+
destination,
41+
buffer.Memory,
42+
cancellationToken
43+
);
44+
45+
totalBytesCopied += bytesCopied;
46+
47+
if (totalBytes > 0)
48+
progress?.Report(Percentage.FromFraction(1.0 * totalBytesCopied / totalBytes));
49+
} while (bytesCopied > 0);
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)