Updates from code review:
1) HttpResponseMessage (return from HttpClient.GetAsync) should be disposed so added using statements as appropriate. In some cases the code was slightly rearranged for readability with this update. 2) Since the BaseUrl is never actually used elsewhere, removed updating it in ReleaseNotesViewModel.cs 3) Removed unneeded setting of stream pointers to their head immediately after creation since they will be that way already. 4) In NewsPageViewModel.cs, converted the use of MemoryStream to the return type of ReadAsStreamAsync, Stream, since it allows for the same usage in the following code and avoids a potentially risky cast.
This commit is contained in:
Родитель
f14aed2f6f
Коммит
8352caf1f7
|
@ -214,13 +214,14 @@ namespace Stride.LauncherApp.Services
|
|||
|
||||
|
||||
var strideInstaller = Path.Combine(Path.GetTempPath(), $"StrideSetup-{Guid.NewGuid()}.exe");
|
||||
var response = await httpClient.GetAsync(strideInstallerUrl);
|
||||
response.EnsureSuccessStatusCode();
|
||||
using (var response = await httpClient.GetAsync(strideInstallerUrl))
|
||||
{
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
await using var responseStream = await response.Content.ReadAsStreamAsync();
|
||||
await using var fileStream = File.Create(strideInstaller);
|
||||
responseStream.Seek(0, SeekOrigin.Begin);
|
||||
responseStream.CopyTo(fileStream);
|
||||
await using var responseStream = await response.Content.ReadAsStreamAsync();
|
||||
await using var fileStream = File.Create(strideInstaller);
|
||||
responseStream.CopyTo(fileStream);
|
||||
}
|
||||
|
||||
var startInfo = new ProcessStartInfo(strideInstaller)
|
||||
{
|
||||
|
|
|
@ -75,39 +75,33 @@ namespace Stride.LauncherApp.ViewModels
|
|||
string urlData;
|
||||
try
|
||||
{
|
||||
var response = await httpClient.GetAsync(string.Format(Urls.GettingStarted, version));
|
||||
response.EnsureSuccessStatusCode();
|
||||
urlData = await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// Unable to reach the url, return an empty list.
|
||||
return result;
|
||||
}
|
||||
|
||||
if (urlData == null)
|
||||
return result;
|
||||
|
||||
try
|
||||
{
|
||||
var urls = urlData.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (var url in urls)
|
||||
using (var response = await httpClient.GetAsync(string.Format(Urls.GettingStarted, version)))
|
||||
{
|
||||
var match = ParsingRegex.Match(url);
|
||||
if (match.Success && match.Groups.Count == 4)
|
||||
response.EnsureSuccessStatusCode();
|
||||
urlData = await response.Content.ReadAsStringAsync();
|
||||
|
||||
if (urlData != null)
|
||||
{
|
||||
var link = match.Groups[3].Value;
|
||||
if (link.StartsWith(DocPageScheme))
|
||||
var urls = urlData.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (var url in urls)
|
||||
{
|
||||
link = GetDocumentationPageUrl(version, link.Substring(DocPageScheme.Length));
|
||||
var match = ParsingRegex.Match(url);
|
||||
if (match.Success && match.Groups.Count == 4)
|
||||
{
|
||||
var link = match.Groups[3].Value;
|
||||
if (link.StartsWith(DocPageScheme))
|
||||
{
|
||||
link = GetDocumentationPageUrl(version, link.Substring(DocPageScheme.Length));
|
||||
}
|
||||
var page = new DocumentationPageViewModel(serviceProvider, version)
|
||||
{
|
||||
Title = match.Groups[1].Value.Trim(),
|
||||
Description = match.Groups[2].Value.Trim(),
|
||||
Url = link.Trim()
|
||||
};
|
||||
result.Add(page);
|
||||
}
|
||||
}
|
||||
var page = new DocumentationPageViewModel(serviceProvider, version)
|
||||
{
|
||||
Title = match.Groups[1].Value.Trim(),
|
||||
Description = match.Groups[2].Value.Trim(),
|
||||
Url = link.Trim()
|
||||
};
|
||||
result.Add(page);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -65,51 +65,43 @@ namespace Stride.LauncherApp.ViewModels
|
|||
public static async Task<List<NewsPageViewModel>> FetchNewsPages(IViewModelServiceProvider serviceProvider, int maxCount)
|
||||
{
|
||||
var result = new List<NewsPageViewModel>();
|
||||
MemoryStream rss;
|
||||
try
|
||||
{
|
||||
HttpResponseMessage response = await httpClient.GetAsync(Urls.RssFeed);
|
||||
response.EnsureSuccessStatusCode();
|
||||
rss = (MemoryStream)await response.Content.ReadAsStreamAsync();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// Unable to reach the url, return an empty list.
|
||||
return result;
|
||||
}
|
||||
|
||||
rss.Position = 0;
|
||||
if (rss.Length == 0)
|
||||
return result;
|
||||
|
||||
try
|
||||
{
|
||||
int count = 0;
|
||||
using (XmlReader rssReader = XmlReader.Create(rss))
|
||||
using (var response = await httpClient.GetAsync(Urls.RssFeed))
|
||||
{
|
||||
rssReader.MoveToContent();
|
||||
while (rssReader.ReadToFollowing("item") && count < maxCount)
|
||||
response.EnsureSuccessStatusCode();
|
||||
var rss = await response.Content.ReadAsStreamAsync();
|
||||
|
||||
if (rss.Length == 0)
|
||||
return result;
|
||||
|
||||
int count = 0;
|
||||
using (XmlReader rssReader = XmlReader.Create(rss))
|
||||
{
|
||||
rssReader.ReadToFollowing("title");
|
||||
string title = rssReader.Read() ? rssReader.Value : null;
|
||||
rssReader.ReadToFollowing("description");
|
||||
string description = rssReader.Read() ? rssReader.Value : null;
|
||||
rssReader.ReadToFollowing("pubDate");
|
||||
var date = new DateTime();
|
||||
bool dateValid = rssReader.Read() && DateTime.TryParseExact(rssReader.Value, "ddd, dd MMM yyyy HH:mm:ss zz00", CultureInfo.InvariantCulture, DateTimeStyles.None, out date);
|
||||
rssReader.ReadToFollowing("link");
|
||||
string link = rssReader.Read() ? rssReader.Value : null;
|
||||
if (dateValid && title != null && link != null && description != null)
|
||||
rssReader.MoveToContent();
|
||||
while (rssReader.ReadToFollowing("item") && count < maxCount)
|
||||
{
|
||||
var page = new NewsPageViewModel(serviceProvider)
|
||||
rssReader.ReadToFollowing("title");
|
||||
string title = rssReader.Read() ? rssReader.Value : null;
|
||||
rssReader.ReadToFollowing("description");
|
||||
string description = rssReader.Read() ? rssReader.Value : null;
|
||||
rssReader.ReadToFollowing("pubDate");
|
||||
var date = new DateTime();
|
||||
bool dateValid = rssReader.Read() && DateTime.TryParseExact(rssReader.Value, "ddd, dd MMM yyyy HH:mm:ss zz00", CultureInfo.InvariantCulture, DateTimeStyles.None, out date);
|
||||
rssReader.ReadToFollowing("link");
|
||||
string link = rssReader.Read() ? rssReader.Value : null;
|
||||
if (dateValid && title != null && link != null && description != null)
|
||||
{
|
||||
Title = title,
|
||||
Url = link,
|
||||
Description = description,
|
||||
Date = date
|
||||
};
|
||||
result.Add(page);
|
||||
++count;
|
||||
var page = new NewsPageViewModel(serviceProvider)
|
||||
{
|
||||
Title = title,
|
||||
Url = link,
|
||||
Description = description,
|
||||
Date = date
|
||||
};
|
||||
result.Add(page);
|
||||
++count;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -52,7 +52,7 @@ namespace Stride.LauncherApp.ViewModels
|
|||
ToggleCommand = new AnonymousCommand(ServiceProvider, Toggle);
|
||||
}
|
||||
|
||||
public string BaseUrl { get { return baseUrl; } private set { SetValue(ref baseUrl, value); } }
|
||||
public string BaseUrl { get { return baseUrl; } }
|
||||
|
||||
public string Version { get; }
|
||||
|
||||
|
@ -78,10 +78,6 @@ namespace Stride.LauncherApp.ViewModels
|
|||
{
|
||||
response.EnsureSuccessStatusCode();
|
||||
releaseNotesMarkdown = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// If redirected, the RequestMessage will contain the final full URI for the given response
|
||||
var responseUri = response.RequestMessage.RequestUri.ToString();
|
||||
BaseUrl = responseUri.Remove(responseUri.Length - ReleaseNotesFileName.Length);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
|
|
Загрузка…
Ссылка в новой задаче