4
4
using System . Net . Http . Headers ;
5
5
using System . Text ;
6
6
using System . Threading ;
7
+ using Common . Extensions ;
8
+ using Humanizer ;
7
9
using JetBrains . Annotations ;
8
10
using Microsoft . Extensions . Logging ;
9
11
using Newtonsoft . Json ;
@@ -19,12 +21,14 @@ public sealed class ServerCementUpdater : ICementUpdater
19
21
private readonly ILogger log ;
20
22
private readonly string server ;
21
23
private readonly string branch ;
24
+ private readonly ConsoleWriter consoleWriter ;
22
25
23
- public ServerCementUpdater ( ILogger log , string server , string branch )
26
+ public ServerCementUpdater ( ILogger log , ConsoleWriter consoleWriter , string server , string branch )
24
27
{
25
28
this . server = server ;
26
29
this . branch = branch ;
27
30
this . log = log ;
31
+ this . consoleWriter = consoleWriter ;
28
32
}
29
33
30
34
public string GetNewCommitHash ( )
@@ -63,7 +67,7 @@ public string GetNewCommitHash()
63
67
catch ( Exception ex )
64
68
{
65
69
log . LogError ( ex , "Failed to look for updates on server" ) ;
66
- ConsoleWriter . Shared . WriteWarning ( "Failed to look for updates on server: " + ex . Message ) ;
70
+ consoleWriter . WriteWarning ( "Failed to look for updates on server: " + ex . Message ) ;
67
71
68
72
return null ;
69
73
}
@@ -91,29 +95,28 @@ public byte[] GetNewCementZip()
91
95
}
92
96
} ;
93
97
94
- using var httpResponseMessage = httpClient . Send ( httpRequestMessage , cts . Token ) ;
98
+ using var httpResponseMessage = httpClient . Send ( httpRequestMessage , HttpCompletionOption . ResponseHeadersRead , cts . Token ) ;
95
99
httpResponseMessage . EnsureSuccessStatusCode ( ) ;
96
100
97
101
using var stream = httpResponseMessage . Content . ReadAsStream ( cts . Token ) ;
98
- return ReadAllBytes ( stream ) ;
102
+
103
+ return stream . ReadAllBytesWithProgress ( ReportProgress ) ;
104
+
105
+ void ReportProgress ( long totalBytes )
106
+ {
107
+ var readableBytes = totalBytes . Bytes ( ) . ToString ( ) ;
108
+ consoleWriter . WriteProgress ( $ "Downloading: { readableBytes } ") ;
109
+ }
99
110
}
100
111
catch ( Exception ex )
101
112
{
102
113
log . LogError ( ex , "Failed to look for updates on server, channel='{CementServerReleaseChannel}'" , branch ) ;
103
- ConsoleWriter . Shared . WriteWarning ( "Failed to look for updates on server: " + ex . Message ) ;
114
+ consoleWriter . WriteWarning ( "Failed to look for updates on server: " + ex . Message ) ;
104
115
105
116
return null ;
106
117
}
107
118
}
108
119
109
- private static byte [ ] ReadAllBytes ( Stream stream )
110
- {
111
- using var buffer = new MemoryStream ( ) ;
112
- stream . CopyTo ( buffer ) ;
113
-
114
- return buffer . ToArray ( ) ;
115
- }
116
-
117
120
public string Name => server ;
118
121
}
119
122
}
0 commit comments