6
6
using System . Text . Json ;
7
7
using System . Security . Principal ;
8
8
using System . Globalization ;
9
+ using System . Security . Cryptography ;
9
10
10
11
namespace Agent
11
12
{
@@ -120,6 +121,7 @@ public async Task HandleNextMessage(ServerResponseResult response)
120
121
121
122
downloadJob . chunk_num ++ ;
122
123
bool completed = ( downloadJob . chunk_num == downloadJob . total_chunks ) ;
124
+
123
125
//Prepare download response
124
126
DownloadResponse dr = new DownloadResponse ( )
125
127
{
@@ -144,18 +146,15 @@ public async Task HandleNextMessage(ServerResponseResult response)
144
146
completed = ( downloadJob . chunk_num == downloadJob . total_chunks ) ,
145
147
} ;
146
148
147
-
148
- var tuple = await this . TryHandleNextChunk ( downloadJob ) ;
149
-
150
- if ( tuple . Item1 )
149
+ if ( this . TryHandleNextChunk ( downloadJob , out var chunk ) )
151
150
{
152
- dr . download . chunk_data = tuple . Item2 ;
151
+ dr . download . chunk_data = chunk ;
153
152
}
154
153
else
155
154
{
156
- dr . download . chunk_data = String . Empty ;
157
- dr . user_output = tuple . Item2 ;
155
+ dr . user_output = chunk ;
158
156
dr . status = "error" ;
157
+ dr . download . chunk_data = String . Empty ;
159
158
this . CompleteDownloadJob ( response . task_id ) ;
160
159
}
161
160
@@ -175,7 +174,6 @@ private async Task<int> GetTotalChunks(ServerDownloadJob job)
175
174
try
176
175
{
177
176
var fi = new FileInfo ( job . path ) ;
178
- //int total_chunks = (int)(fi.Length + job.chunk_size - 1) / job.chunk_size;
179
177
return ( int ) Math . Ceiling ( ( double ) fi . Length / job . chunk_size ) ;
180
178
}
181
179
catch
@@ -196,6 +194,43 @@ public void CompleteDownloadJob(string task_id)
196
194
/// Read the next chunk from the file
197
195
/// </summary>
198
196
/// <param name="job">Download job that's being tracked</param>
197
+ public bool TryHandleNextChunk ( ServerDownloadJob job , out string chunk )
198
+ {
199
+ try
200
+ {
201
+ if ( job . total_chunks == 1 )
202
+ {
203
+ job . complete = true ;
204
+ chunk = Misc . Base64Encode ( File . ReadAllBytes ( job . path ) ) ;
205
+ return true ;
206
+ }
207
+ long totalBytesRead = job . chunk_size * ( job . chunk_num - 1 ) ;
208
+
209
+ using ( var fileStream = new FileStream ( job . path , FileMode . Open , FileAccess . Read ) )
210
+ {
211
+ byte [ ] buffer = new byte [ job . chunk_size ] ;
212
+
213
+ FileInfo fileInfo = new FileInfo ( job . path ) ;
214
+
215
+ if ( fileInfo . Length - totalBytesRead < job . chunk_size )
216
+ {
217
+ job . complete = true ;
218
+ buffer = new byte [ fileInfo . Length - job . bytesRead ] ;
219
+ }
220
+
221
+ fileStream . Seek ( job . bytesRead , SeekOrigin . Begin ) ;
222
+ job . bytesRead += fileStream . Read ( buffer , 0 , buffer . Length ) ;
223
+ chunk = Misc . Base64Encode ( buffer ) ;
224
+ return true ;
225
+ } ;
226
+ }
227
+ catch ( Exception e )
228
+ {
229
+ job . complete = true ;
230
+ chunk = e . ToString ( ) ;
231
+ return false ;
232
+ }
233
+ }
199
234
public async Task < Tuple < bool , string > > TryHandleNextChunk ( ServerDownloadJob job )
200
235
{
201
236
try
@@ -226,6 +261,7 @@ public async Task<Tuple<bool,string>> TryHandleNextChunk(ServerDownloadJob job)
226
261
}
227
262
catch ( Exception e )
228
263
{
264
+ Console . WriteLine ( e . ToString ( ) ) ;
229
265
job . complete = true ;
230
266
return new Tuple < bool , string > ( false , e . ToString ( ) ) ;
231
267
}
0 commit comments