forked from dotnet/crank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebUtils.cs
45 lines (39 loc) · 1.56 KB
/
WebUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
namespace Microsoft.Crank.Controller
{
internal static class WebUtils
{
internal static async Task<string> DownloadFileContentAsync(this HttpClient httpClient, string uri)
{
using (var downloadStream = await httpClient.GetStreamAsync(uri))
{
using (var stringReader = new StreamReader(downloadStream))
{
return await stringReader.ReadToEndAsync();
}
}
}
internal static async Task DownloadFileAsync(this HttpClient httpClient, string uri, string serverJobUri, string destinationFileName)
{
using (var downloadStream = await httpClient.GetStreamAsync(uri))
using (var fileStream = File.Create(destinationFileName))
{
var downloadTask = downloadStream.CopyToAsync(fileStream);
while (!downloadTask.IsCompleted)
{
// Ping server job to keep it alive while downloading the file
Log.Verbose($"GET {serverJobUri}/touch...");
await httpClient.GetAsync(serverJobUri + "/touch");
await Task.Delay(1000);
}
await downloadTask;
}
}
}
}