Files
Saxonite/SaxoniteCoreWebOnline/Online/Controllers/ImageController.cs
T
2022-10-25 11:55:32 +01:00

230 lines
8.9 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Saxonite.Net.Core.Web.Online.Models;
using Saxonite.Net.Core.Web.Base.App;
using Saxonite.Net.Core.Web.Base.Controllers;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Saxonite.Net.Standard.Base.Bus;
using Sarand.Net.Standard.Base.App;
using Sarand.Net.Standard.Base.Bus;
using Sarand.Net.Standard.Base.Utils;
using Saxonite.Net.Standard.Base.App;
using Microsoft.AspNetCore.Http;
using Sarand.Net.Standard.Base.Helpers;
using System.IO;
using System.Drawing;
using Sarand.Net.Standard.Base.Core;
using Microsoft.Net.Http.Headers;
using System.Collections.Specialized;
namespace Saxonite.Net.Core.Web.Online.Controllers
{
//=======================================================================
// Online Image Controller - delivers dynamic images.
//=======================================================================
public partial class ImageController : SaxController_Online
{
//-------------------------------------------------------------------
// 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 - General
//-------------------------------------------------------------------
// 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 = userAccountIdParam.ToSysId();
if (!SysId.IsValid(userAccountId))
userAccountId = SessionUserAccountId;
return GetAccountContactImage(userAccountId, Sax.ImageType.Avatar);
}
//-------------------------------------------------------------------
// Account Contact image - Profile
//-------------------------------------------------------------------
// Account Profile Contact Image 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")]
[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
/// </summary>
/// <param name="imageTypePriority"></param>
/// <returns></returns>
private IActionResult GetAccountContactImage(SysId userAccountId, Sax.ImageType imageTypePriority)
{
String fn = GetType().Name + ".GetAccountContactImage";
// Validate session and referrer.
if (!ValidReferer) return Forbid();
if (!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)
{
String fn = GetType().Name + ".GetOrganisationLogo";
// Validate session and referrer.
if (!ValidReferer || !ValidUserSession) return Forbid();
// Extract encrypted parameters.
NameValueCollection parameters = ExtractOnlineParams(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
//-------------------------------------------------------------------
/// <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)
{
String fn = GetType().Name + ".GetItemImage";
// Validate session and referrer.
if (!ValidReferer) return Forbid();
if (!ValidUserSession) return Forbid();
ItemId userItemId = new ItemId((short)Sax.DataItemType.UserAccount, SessionUserAccountId);
// Extract encrypted parameters.
NameValueCollection parameters = ExtractOnlineParams(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);
}
//-------------------------------------------------------------------
}
//=======================================================================
}