Skip to content

Commit

Permalink
Make download helpers public for ease
Browse files Browse the repository at this point in the history
  • Loading branch information
mnadareski committed Nov 15, 2024
1 parent 3042367 commit 5d300c9
Showing 1 changed file with 111 additions and 75 deletions.
186 changes: 111 additions & 75 deletions SabreTools.RedumpLib/Web/RedumpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public RedumpClient(int retryCount) : this()
try
{
// Get the current token from the login page
var loginPage = await DownloadStringWithRetries(Constants.LoginUrl);
var loginPage = await DownloadString(Constants.LoginUrl);
string token = Constants.TokenRegex.Match(loginPage ?? string.Empty).Groups[1].Value;

#if NETFRAMEWORK
Expand Down Expand Up @@ -186,6 +186,108 @@ public RedumpClient(int retryCount) : this()

#endregion

#region Generic Helpers

/// <summary>
/// Download from a URI to a byte array
/// </summary>
/// <param name="uri">Remote URI to retrieve</param>
/// <returns>Byte array from the URI, null on error</returns>
public async Task<byte[]?> DownloadData(string uri)
{
// Only retry a positive number of times
if (RetryCount <= 0)
return null;

for (int i = 0; i < RetryCount; i++)
{
try
{
#if NET40
return await Task.Factory.StartNew(() => _internalClient.DownloadData(uri));
#elif NETFRAMEWORK
return await Task.Run(() => _internalClient.DownloadData(uri));
#else
return await _internalClient.GetByteArrayAsync(uri);
#endif
}
catch { }

// Sleep for 100ms if the last attempt failed
Thread.Sleep(100);
}

return null;
}

/// <summary>
/// Download from a URI to a local file
/// </summary>
/// <param name="uri">Remote URI to retrieve</param>
/// <param name="fileName">Filename to write to</param>
/// <returns>The remote filename from the URI, null on error</returns>
public async Task<string?> DownloadFile(string uri, string fileName)
{
#if NET40
await Task.Factory.StartNew(() => { _internalClient.DownloadFile(uri, fileName); return true; });
return _internalClient.GetLastFilename();
#elif NETFRAMEWORK
await Task.Run(() => _internalClient.DownloadFile(uri, fileName));
return _internalClient.GetLastFilename();
#else
// Make the call to get the file
var response = await _internalClient.GetAsync(uri);
if (response?.Content?.Headers == null || !response.IsSuccessStatusCode)
{
Console.WriteLine($"Could not download {uri}");
return null;
}

// Copy the data to a local temp file
using (var responseStream = await response.Content.ReadAsStreamAsync())
using (var tempFileStream = File.OpenWrite(fileName))
{
responseStream.CopyTo(tempFileStream);
}

return response.Content.Headers.ContentDisposition?.FileName?.Replace("\"", "");
#endif
}

/// <summary>
/// Download from a URI to a string
/// </summary>
/// <param name="uri">Remote URI to retrieve</param>
/// <returns>String from the URI, null on error</returns>
public async Task<string?> DownloadString(string uri)
{
// Only retry a positive number of times
if (RetryCount <= 0)
return null;

for (int i = 0; i < RetryCount; i++)
{
try
{
#if NET40
return await Task.Factory.StartNew(() => _internalClient.DownloadString(uri));
#elif NETFRAMEWORK
return await Task.Run(() => _internalClient.DownloadString(uri));
#else
return await _internalClient.GetStringAsync(uri);
#endif
}
catch { }

// Sleep for 100ms if the last attempt failed
Thread.Sleep(100);
}

return null;
}

#endregion

#region Single Page Helpers

/// <summary>
Expand All @@ -198,7 +300,7 @@ public async Task<List<int>> CheckSingleSitePage(string url)
List<int> ids = [];

// Try to retrieve the data
string? dumpsPage = await DownloadStringWithRetries(url);
string? dumpsPage = await DownloadString(url);

// If we have no dumps left
if (dumpsPage == null || dumpsPage.Contains("No discs found."))
Expand Down Expand Up @@ -248,7 +350,7 @@ public async Task<List<int>> CheckSingleSitePage(string url, string? outDir, boo
List<int> ids = [];

// Try to retrieve the data
string? dumpsPage = await DownloadStringWithRetries(url);
string? dumpsPage = await DownloadString(url);

// If we have no dumps left
if (dumpsPage == null || dumpsPage.Contains("No discs found."))
Expand Down Expand Up @@ -306,7 +408,7 @@ public async Task<List<int>> CheckSingleWIPPage(string url)
List<int> ids = [];

// Try to retrieve the data
string? dumpsPage = await DownloadStringWithRetries(url);
string? dumpsPage = await DownloadString(url);

// If we have no dumps left
if (dumpsPage == null || dumpsPage.Contains("No discs found."))
Expand Down Expand Up @@ -346,7 +448,7 @@ public async Task<List<int>> CheckSingleWIPPage(string url, string? outDir, bool
List<int> ids = [];

// Try to retrieve the data
string? dumpsPage = await DownloadStringWithRetries(url);
string? dumpsPage = await DownloadString(url);

// If we have no dumps left
if (dumpsPage == null || dumpsPage.Contains("No discs found."))
Expand Down Expand Up @@ -451,7 +553,7 @@ public async Task<bool> DownloadSinglePack(string url, RedumpSystem? system, str
{
// Try to retrieve the data
string discPageUri = string.Format(Constants.DiscPageUrl, +id);
string? discPage = await DownloadStringWithRetries(discPageUri);
string? discPage = await DownloadString(discPageUri);

if (discPage == null || discPage.Contains($"Disc with ID \"{id}\" doesn't exist"))
{
Expand Down Expand Up @@ -489,7 +591,7 @@ public async Task<bool> DownloadSingleSiteID(int id, string? outDir, bool rename
{
// Try to retrieve the data
string discPageUri = string.Format(Constants.DiscPageUrl, +id);
string? discPage = await DownloadStringWithRetries(discPageUri);
string? discPage = await DownloadString(discPageUri);

if (discPage == null || discPage.Contains($"Disc with ID \"{id}\" doesn't exist"))
{
Expand Down Expand Up @@ -613,7 +715,7 @@ public async Task<bool> DownloadSingleSiteID(int id, string? outDir, bool rename
{
// Try to retrieve the data
string discPageUri = string.Format(Constants.WipDiscPageUrl, +id);
string? discPage = await DownloadStringWithRetries(discPageUri);
string? discPage = await DownloadString(discPageUri);

if (discPage == null || discPage.Contains($"WIP disc with ID \"{id}\" doesn't exist"))
{
Expand Down Expand Up @@ -651,7 +753,7 @@ public async Task<bool> DownloadSingleWIPID(int id, string? outDir, bool rename)
{
// Try to retrieve the data
string discPageUri = string.Format(Constants.WipDiscPageUrl, +id);
string? discPage = await DownloadStringWithRetries(discPageUri);
string? discPage = await DownloadString(discPageUri);

if (discPage == null || discPage.Contains($"WIP disc with ID \"{id}\" doesn't exist"))
{
Expand Down Expand Up @@ -792,72 +894,6 @@ public async Task<bool> DownloadPacks(string url, RedumpSystem[] systems, string
return true;
}

/// <summary>
/// Download from a URI to a local file
/// </summary>
/// <param name="uri">Remote URI to retrieve</param>
/// <param name="fileName">Filename to write to</param>
/// <returns>The remote filename from the URI, null on error</returns>
private async Task<string?> DownloadFile(string uri, string fileName)
{
#if NET40
await Task.Factory.StartNew(() => { _internalClient.DownloadFile(uri, fileName); return true; });
return _internalClient.GetLastFilename();
#elif NETFRAMEWORK
await Task.Run(() => _internalClient.DownloadFile(uri, fileName));
return _internalClient.GetLastFilename();
#else
// Make the call to get the file
var response = await _internalClient.GetAsync(uri);
if (response?.Content?.Headers == null || !response.IsSuccessStatusCode)
{
Console.WriteLine($"Could not download {uri}");
return null;
}

// Copy the data to a local temp file
using (var responseStream = await response.Content.ReadAsStreamAsync())
using (var tempFileStream = File.OpenWrite(fileName))
{
responseStream.CopyTo(tempFileStream);
}

return response.Content.Headers.ContentDisposition?.FileName?.Replace("\"", "");
#endif
}

/// <summary>
/// Download from a URI to a string
/// </summary>
/// <param name="uri">Remote URI to retrieve</param>
/// <returns>String from the URI, null on error</returns>
private async Task<string?> DownloadStringWithRetries(string uri)
{
// Only retry a positive number of times
if (RetryCount <= 0)
return null;

for (int i = 0; i < RetryCount; i++)
{
try
{
#if NET40
return await Task.Factory.StartNew(() => _internalClient.DownloadString(uri));
#elif NETFRAMEWORK
return await Task.Run(() => _internalClient.DownloadString(uri));
#else
return await _internalClient.GetStringAsync(uri);
#endif
}
catch { }

// Sleep for 100ms if the last attempt failed
Thread.Sleep(100);
}

return null;
}

/// <summary>
/// Move a tempfile to a new name unless it aleady exists, in which case, delete the tempfile
/// </summary>
Expand Down

0 comments on commit 5d300c9

Please sign in to comment.