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 logger) : base(localiser, logger) { } //------------------------------------------------------------------- // Account Contact image request. //------------------------------------------------------------------- /// /// Get the Account Contact Image for the specified account. /// Current User Account is used if no other specified in the encrypted parameter. /// Encrypted parameter: /// - uaie: User Account Id encrypted. /// [HttpGet] [Route("Image/Account/Contact")] public IActionResult GetAccountContactImage(String uaie) { String userAccountIdParam = "";// ToDo Decrypt from uaie value. SysId userAccountId = userAccountIdParam.ToSysId(); if (!SysId.IsValid(userAccountId)) userAccountId = SessionUserAccountId; return GetAccountContactImage(userAccountId, Sax.ImageType.Avatar); } //------------------------------------------------------------------- // Account Contact Image Profile/Upload request. //------------------------------------------------------------------- /// /// Get the current Account Profile Contact Image for the specified account. /// Account is always current User Account - this request is only fir use from the user's own profile page. /// 'Uploaded' avatar image will take precedence as this will be the latest if present. /// NB Avoid caching this image as it will change whenever a new one is uploaded. /// [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); } //------------------------------------------------------------------- /// /// Get the Avatar image for the specified user account. /// /// /// private IActionResult GetAccountContactImage(SysId userAccountId, Sax.ImageType imageTypePriority) { String fn = GetType().Name + ".GetAccountContactImage"; // 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. //------------------------------------------------------------------- /// /// Get the logo for the specified Organisation. /// Method parameter name (ole) must match name of encrypted parameter. /// Encrypted parameters: /// - ori: Organisation Id. /// [HttpGet] [Route("Image/Organisation/Logo")] public IActionResult GetOrganisationLogo(String ole) { return GetOrganisationLogo(ole, false); } /// /// Get the logo for the specified Organisation. /// Method parameter name (ole) must match name of encrypted parameter. /// Encrypted parameters: /// - ori: Organisation Id. /// [HttpGet] [Route("Image/Organisation/Logo/Upload")] public IActionResult GetOrganisationLogoUpload(String ole) { return GetOrganisationLogo(ole, true); } private IActionResult GetOrganisationLogo(String encryptedParams, bool allowUploadImage) { String fn = GetType().Name + ".GetOrganisationLogo"; // 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 = parameters.Get(ParamName_OrganisationId).ToSysId(); 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. //------------------------------------------------------------------- /// /// Get the specified Item Image. /// Method parameter name (iie) must match name of encrypted parameter. /// Encrypted parameters: /// - iii: Item Image Id. /// [HttpGet] [Route("Image/Item")] public IActionResult GetItemImage(String iie) { String fn = GetType().Name + ".GetItemImage"; // 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 = parameters.Get(ParamName_ItemImageId).ToSysId(); if (!SysId.IsValid(itemImageId)) return Forbid(); SaxItemImage itemImage = ItemDataMgr.GetItemImageById(itemImageId); if (itemImage == null) return NotFound(); // Construct response. return File(itemImage.ImageData, itemImage.ContentType); } //------------------------------------------------------------------- } //======================================================================= }