From 46c82acfe0c052a12ed7c92dfe2165fd1f516337 Mon Sep 17 00:00:00 2001 From: Bryan Jonker Date: Thu, 14 Nov 2024 17:16:51 -0600 Subject: [PATCH] Adding picture controller --- .../ImageManager/DirectoryImage.cs | 2 ++ .../Controllers/PictureController.cs | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 uofi-itp-directory/Controllers/PictureController.cs diff --git a/uofi-itp-directory-external/ImageManager/DirectoryImage.cs b/uofi-itp-directory-external/ImageManager/DirectoryImage.cs index dc9ca59..0cf3499 100644 --- a/uofi-itp-directory-external/ImageManager/DirectoryImage.cs +++ b/uofi-itp-directory-external/ImageManager/DirectoryImage.cs @@ -3,6 +3,8 @@ public static class DirectoryImage { private const string _blank = "https://directory.illinois.edu/webservices/public/ds/profile.png"; + public static string Blank => _blank; + public static string CheckImage(string url) { if (string.IsNullOrWhiteSpace(url)) { return _blank; diff --git a/uofi-itp-directory/Controllers/PictureController.cs b/uofi-itp-directory/Controllers/PictureController.cs new file mode 100644 index 0000000..d6caf6d --- /dev/null +++ b/uofi-itp-directory/Controllers/PictureController.cs @@ -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 ByUsername(string netid) => await Index(netid); + + [HttpGet] + public async Task 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"); + } + } +} \ No newline at end of file