Skip to content

Commit

Permalink
Merge pull request #173 from /issues/143
Browse files Browse the repository at this point in the history
fix(79875028): get up to date actor image
  • Loading branch information
Maxiboy441 authored Sep 23, 2024
2 parents 9c43fc9 + a557286 commit 3b8278e
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Project/Project/Controllers/ActorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ public async Task<IActionResult> Show(string name)
{
var actor = await _context.Actors.FirstOrDefaultAsync(a => a.Name == name);

if (actor != null)
{
bool accessible = await IsUrlAccessibleAsync(actor.Image);
if (!accessible)
{
var service = new WikipediaMediaAPIService();
var imageUrl = await service.GetFirstImageUrlAsync(actor.Name);
actor.Image = imageUrl;
await _context.SaveChangesAsync();
}
}

if (actor == null)
{
actor = await _actorApiService.GetAndSaveActorAsync(name);
Expand All @@ -38,5 +50,20 @@ public async Task<IActionResult> Show(string name)
return View(actor);
}

public static async Task<bool> IsUrlAccessibleAsync(string url)
{
using (HttpClient client = new HttpClient())
{
try
{
HttpResponseMessage response = await client.GetAsync(url);
return response.StatusCode == System.Net.HttpStatusCode.OK;
}
catch (Exception ex)
{
return false;
}
}
}
}

0 comments on commit 3b8278e

Please sign in to comment.