Files
Saxonite/Workspace/SaxoniteCoreWebAdmin/Admin/Controllers/ImageController.cs
T
andy 9f22b522df v1.3.0 Restructure Saxonite workspace and upgrade to .NET 10 (#1)
Move Saxonite’s projects into a dedicated `Workspace` folder and align the solution with .NET 10 and the current Common workspace. Update encryption integration, test project structure and coverage, and repository guidance and upgrade notes.

- Relocate application and library projects while retaining Git rename history.
- Split tests into Admin, Online, and Shared projects; add representative behavioral and serialization tests.
- Update Common integration, application version, and repository documentation.

Application version: 1.3.0

Reviewed-on: #1
Co-authored-by: andy <andy@sarand.com>
2026-10-16 18:24:16 +01:00

209 lines
8.4 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
using Sarand.Net.Standard.Base.Bus;
using Sarand.Net.Standard.Base.Utils;
using Saxonite.Net.Core.Web.Base.App;
using Saxonite.Net.Standard.Base.App;
using Saxonite.Net.Standard.Base.Bus;
using System;
using System.Collections.Specialized;
namespace Saxonite.Net.Core.Web.Admin.Controllers
{
//=======================================================================
// Admin Image Controller - delivers dynamic images.
//=======================================================================
public partial class ImageController : SaxController_Admin
{
//-------------------------------------------------------------------
// Data members.
public static String ParamName_OrganisationLogoEncrypted { get; } = "ole";
public static String ParamName_ItemImageEncrypted { get; } = "iie";
public static String ParamName_OrganisationId { get; } = "ori";
public static String ParamName_ItemImageId { get; } = "iii";
//-------------------------------------------------------------------
// Construction.
public ImageController(SaxLocaliser localiser, ILogger<HomeController> logger) :
base(localiser, logger)
{
}
//-------------------------------------------------------------------
// Account Contact image request.
//-------------------------------------------------------------------
/// <summary>
/// Get the Account Contact Image for the specified account.
/// <para>Current User Account is used if no other specified in the encrypted parameter.</para>
/// <para>Encrypted parameter:</para>
/// <para>- uaie: User Account Id encrypted.</para>
/// </summary>
[HttpGet]
[Route("Image/Account/Contact")]
public IActionResult GetAccountContactImage(String uaie)
{
String userAccountIdParam = "";// ToDo Decrypt from uaie value.
SysId userAccountId = SysId.ToSysId(userAccountIdParam);
if (!SysId.IsValid(userAccountId))
userAccountId = SessionUserAccountId;
return GetAccountContactImage(userAccountId, Sax.ImageType.Avatar);
}
//-------------------------------------------------------------------
// Account Contact Image Profile/Upload request.
//-------------------------------------------------------------------
/// <summary>
/// Get the current Account Profile Contact Image for the specified account.
/// <para>Account is always current User Account - this request is only fir use from the user's own profile page.</para>
/// <para>'Uploaded' avatar image will take precedence as this will be the latest if present.</para>
/// <para>NB Avoid caching this image as it will change whenever a new one is uploaded.</para>
/// </summary>
[HttpGet]
[Route("Image/Account/Contact/Profile")]
[Route("Image/Account/Contact/Upload")]
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
public IActionResult GetAccountContactImage()
{
// Response should not be cached.
Response.Headers[HeaderNames.CacheControl] = "no-cache, no-store, must-revalidate, max-age=0";
Response.Headers[HeaderNames.Expires] = "-1";
SysId userAccountId = SessionUserAccountId;
return GetAccountContactImage(userAccountId, Sax.ImageType.Avatar_Upload);
}
//-------------------------------------------------------------------
/// <summary>
/// Get the Avatar image for the specified user account.
/// </summary>
/// <param name="imageTypePriority"></param>
/// <returns></returns>
private IActionResult GetAccountContactImage(SysId userAccountId, Sax.ImageType imageTypePriority)
{
// Validate session and referrer.
if (!ValidReferer || !ValidUserSession) return Forbid();
ItemId userItemId = new ItemId((short)Sax.DataItemType.UserAccount, userAccountId);
// Retrieve account contact images.
SaxItemImage.Params prmItemImage = new SaxItemImage.Params();
prmItemImage.ItemIdsIncluded.Add(userItemId);
prmItemImage.ImageTypesIncluded.Add(Sax.ImageType.Avatar);
prmItemImage.ImageTypesIncluded.Add(Sax.ImageType.Avatar_Upload);
prmItemImage.StatusCat = SaxItemImage.StatusCat.ActiveOnly;
SaxItemImage itemImage = null;
foreach (SaxItemImage itemImageCandidate in ItemDataMgr.GetItemImageList(prmItemImage))
{
// One of the 'Avatar' image types will take precedence.
if (itemImage == null || itemImageCandidate.ImageType == imageTypePriority)
itemImage = itemImageCandidate;
}
if (itemImage == null)
return NotFound();
// Construct response.
return File(itemImage.ImageData, itemImage.ContentType);
}
//-------------------------------------------------------------------
// Organisation logo.
//-------------------------------------------------------------------
/// <summary>
/// Get the logo for the specified Organisation.
/// <para>Method parameter name (ole) must match name of encrypted parameter.</para>
/// <para>Encrypted parameters:</para>
/// <para>- ori: Organisation Id.</para>
/// </summary>
[HttpGet]
[Route("Image/Organisation/Logo")]
public IActionResult GetOrganisationLogo(String ole)
{
return GetOrganisationLogo(ole, false);
}
/// <summary>
/// Get the logo for the specified Organisation.
/// <para>Method parameter name (ole) must match name of encrypted parameter.</para>
/// <para>Encrypted parameters:</para>
/// <para>- ori: Organisation Id.</para>
/// </summary>
[HttpGet]
[Route("Image/Organisation/Logo/Upload")]
public IActionResult GetOrganisationLogoUpload(String ole)
{
return GetOrganisationLogo(ole, true);
}
private IActionResult GetOrganisationLogo(String encryptedParams, bool allowUploadImage)
{
// Validate session and referrer.
if (!ValidReferer || !ValidUserSession) return Forbid();
// Extract encrypted parameters.
NameValueCollection parameters = ExtractSessionParams(encryptedParams);
if (parameters.Count < 1) return Forbid();
// Organisation Id.
SysId organisationId = SysId.ToSysId(parameters.Get(ParamName_OrganisationId));
if (!SysId.IsValid(organisationId)) return Forbid();
ItemId organisationItemId = new ItemId((short)Sax.DataItemType.Organisation, organisationId);
// Query Organisation Logo images.
SaxItemImage.Params prmItemImage = new SaxItemImage.Params();
prmItemImage.ItemIdsIncluded.Add(organisationItemId);
prmItemImage.ImageTypesIncluded.Add(Sax.ImageType.OrganisationLogo);
if (allowUploadImage)
prmItemImage.ImageTypesIncluded.Add(Sax.ImageType.OrganisationLogo_Upload);
prmItemImage.StatusCat = SaxItemImage.StatusCat.ActiveOnly;
SaxItemImage itemImage = null;
foreach (SaxItemImage itemImageCandidate in ItemDataMgr.GetItemImageList(prmItemImage))
{
// The 'Upload' image will take precedence if allowed.
if (itemImage == null || itemImageCandidate.ImageType == Sax.ImageType.OrganisationLogo_Upload)
itemImage = itemImageCandidate;
}
if (itemImage == null)
return NotFound();
// Construct response.
Response.Headers[HeaderNames.CacheControl] = "no-cache, no-store, must-revalidate, max-age=0";
Response.Headers[HeaderNames.Expires] = "-1";
return File(itemImage.ImageData, itemImage.ContentType);
}
//-------------------------------------------------------------------
// Item image.
//-------------------------------------------------------------------
/// <summary>
/// Get the specified Item Image.
/// <para>Method parameter name (iie) must match name of encrypted parameter.</para>
/// <para>Encrypted parameters:</para>
/// <para>- iii: Item Image Id.</para>
/// </summary>
[HttpGet]
[Route("Image/Item")]
public IActionResult GetItemImage(String iie)
{
// Validate session and referrer.
if (!ValidReferer || !ValidUserSession) return Forbid();
ItemId userItemId = new ItemId((short)Sax.DataItemType.UserAccount, SessionUserAccountId);
// Extract encrypted parameters.
NameValueCollection parameters = ExtractSessionParams(iie);
if (parameters.Count < 1) return Forbid();
// Item Image.
SysId itemImageId = SysId.ToSysId(parameters.Get(ParamName_ItemImageId));
if (!SysId.IsValid(itemImageId)) return Forbid();
SaxItemImage itemImage = ItemDataMgr.GetItemImageById(itemImageId);
if (itemImage == null)
return NotFound();
// Construct response.
return File(itemImage.ImageData, itemImage.ContentType);
}
//-------------------------------------------------------------------
}
//=======================================================================
}