Skip to content

Commit 24d2c7b

Browse files
committed
DownloadFile should retry as well
1 parent bc94aa2 commit 24d2c7b

1 file changed

Lines changed: 35 additions & 19 deletions

File tree

SabreTools.RedumpLib/Web/RedumpClient.cs

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -246,31 +246,47 @@ public RedumpClient()
246246
/// <returns>The remote filename from the URI, null on error</returns>
247247
public async Task<string?> DownloadFile(string uri, string fileName)
248248
{
249-
if (Debug) Console.WriteLine($"DEBUG: DownloadFile(\"{uri}\", \"{fileName}\")");
249+
// Only retry a positive number of times
250+
if (AttemptCount <= 0)
251+
return null;
252+
253+
for (int i = 0; i < AttemptCount; i++)
254+
{
255+
try
256+
{
257+
if (Debug) Console.WriteLine($"DEBUG: DownloadFile(\"{uri}\", \"{fileName}\"), Attempt {i + 1} of {AttemptCount}");
250258
#if NET40
251-
await Task.Factory.StartNew(() => { _internalClient.DownloadFile(uri, fileName); return true; });
252-
return _internalClient.GetLastFilename();
259+
await Task.Factory.StartNew(() => { _internalClient.DownloadFile(uri, fileName); return true; });
260+
return _internalClient.GetLastFilename();
253261
#elif NETFRAMEWORK || NETSTANDARD2_0_OR_GREATER
254-
await Task.Run(() => _internalClient.DownloadFile(uri, fileName));
255-
return _internalClient.GetLastFilename();
262+
await Task.Run(() => _internalClient.DownloadFile(uri, fileName));
263+
return _internalClient.GetLastFilename();
256264
#else
257-
// Make the call to get the file
258-
var response = await _internalClient.GetAsync(uri);
259-
if (response?.Content?.Headers is null || !response.IsSuccessStatusCode)
260-
{
261-
Console.Error.WriteLine($"Could not download {uri}");
262-
return null;
263-
}
265+
// Make the call to get the file
266+
var response = await _internalClient.GetAsync(uri);
267+
if (response?.Content?.Headers is null || !response.IsSuccessStatusCode)
268+
{
269+
Console.Error.WriteLine($"Could not download {uri}");
270+
return null;
271+
}
264272

265-
// Copy the data to a local temp file
266-
using (var responseStream = await response.Content.ReadAsStreamAsync())
267-
using (var tempFileStream = File.OpenWrite(fileName))
268-
{
269-
responseStream.CopyTo(tempFileStream);
270-
}
273+
// Copy the data to a local temp file
274+
using (var responseStream = await response.Content.ReadAsStreamAsync())
275+
using (var tempFileStream = File.OpenWrite(fileName))
276+
{
277+
responseStream.CopyTo(tempFileStream);
278+
}
271279

272-
return response.Content.Headers.ContentDisposition?.FileName?.Replace("\"", "");
280+
return response.Content.Headers.ContentDisposition?.FileName?.Replace("\"", "");
273281
#endif
282+
}
283+
catch { }
284+
285+
// Sleep for 100ms if the last attempt failed
286+
Thread.Sleep(100);
287+
}
288+
289+
return null;
274290
}
275291

276292
/// <summary>

0 commit comments

Comments
 (0)