From 5d300c997537bf6394e3802b34556eca05de2905 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 14 Nov 2024 22:22:52 -0500 Subject: [PATCH] Make download helpers public for ease --- SabreTools.RedumpLib/Web/RedumpClient.cs | 186 ++++++++++++++--------- 1 file changed, 111 insertions(+), 75 deletions(-) diff --git a/SabreTools.RedumpLib/Web/RedumpClient.cs b/SabreTools.RedumpLib/Web/RedumpClient.cs index de62006..52c37d8 100644 --- a/SabreTools.RedumpLib/Web/RedumpClient.cs +++ b/SabreTools.RedumpLib/Web/RedumpClient.cs @@ -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 @@ -186,6 +186,108 @@ public RedumpClient(int retryCount) : this() #endregion + #region Generic Helpers + + /// + /// Download from a URI to a byte array + /// + /// Remote URI to retrieve + /// Byte array from the URI, null on error + public async Task 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; + } + + /// + /// Download from a URI to a local file + /// + /// Remote URI to retrieve + /// Filename to write to + /// The remote filename from the URI, null on error + public async Task 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 + } + + /// + /// Download from a URI to a string + /// + /// Remote URI to retrieve + /// String from the URI, null on error + public async Task 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 /// @@ -198,7 +300,7 @@ public async Task> CheckSingleSitePage(string url) List 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.")) @@ -248,7 +350,7 @@ public async Task> CheckSingleSitePage(string url, string? outDir, boo List 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.")) @@ -306,7 +408,7 @@ public async Task> CheckSingleWIPPage(string url) List 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.")) @@ -346,7 +448,7 @@ public async Task> CheckSingleWIPPage(string url, string? outDir, bool List 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.")) @@ -451,7 +553,7 @@ public async Task 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")) { @@ -489,7 +591,7 @@ public async Task 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")) { @@ -613,7 +715,7 @@ public async Task 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")) { @@ -651,7 +753,7 @@ public async Task 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")) { @@ -792,72 +894,6 @@ public async Task DownloadPacks(string url, RedumpSystem[] systems, string return true; } - /// - /// Download from a URI to a local file - /// - /// Remote URI to retrieve - /// Filename to write to - /// The remote filename from the URI, null on error - private async Task 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 - } - - /// - /// Download from a URI to a string - /// - /// Remote URI to retrieve - /// String from the URI, null on error - private async Task 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; - } - /// /// Move a tempfile to a new name unless it aleady exists, in which case, delete the tempfile ///