forked from itpartnersillinois/uofi-itp-directory
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request itpartnersillinois#91 from itpartnersillinois/jonk…
…er/Picture Adding picture controller
- Loading branch information
Showing
2 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using uofi_itp_directory_data.Data; | ||
using uofi_itp_directory_external.ImageManager; | ||
|
||
namespace uofi_itp_directory.Controllers { | ||
|
||
[Route("[controller]")] | ||
[AllowAnonymous] | ||
public class PictureController(DirectoryRepository? directoryRepository) : Controller { | ||
private readonly DirectoryRepository _directoryRepository = directoryRepository ?? throw new ArgumentNullException("directoryRepository"); | ||
|
||
[Route("{netid}")] | ||
[HttpGet] | ||
public async Task<IActionResult> ByUsername(string netid) => await Index(netid); | ||
|
||
[HttpGet] | ||
public async Task<IActionResult> Index(string netid) { | ||
netid = netid.Replace("@illinois.edu", "") + "@illinois.edu"; | ||
var url = (await _directoryRepository.ReadAsync(d => d.Employees.FirstOrDefault(e => e.NetId == netid)))?.PhotoUrl ?? ""; | ||
if (string.IsNullOrWhiteSpace(url)) { | ||
url = DirectoryImage.Blank; | ||
} | ||
var wc = new HttpClient(); | ||
var stream = await wc.GetStreamAsync(url); | ||
return new FileStreamResult(stream, "image/webp"); | ||
} | ||
} | ||
} |