v1.0.7.0
This commit is contained in:
@@ -12,6 +12,12 @@ 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;
|
||||
|
||||
namespace Saxonite.Net.Core.Web.Online.Controllers
|
||||
{
|
||||
@@ -21,6 +27,7 @@ namespace Saxonite.Net.Core.Web.Online.Controllers
|
||||
partial class AccountController
|
||||
{
|
||||
//-------------------------------------------------------------------
|
||||
// Common response data.
|
||||
public class ContactResponseData
|
||||
{
|
||||
public String EmailAddress { get; set; }
|
||||
@@ -54,6 +61,13 @@ namespace Saxonite.Net.Core.Web.Online.Controllers
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Utilites.
|
||||
private ItemId GetUserItemId(SysId userAccountId)
|
||||
{
|
||||
return new ItemId((short)Sax.DataItemType.UserAccount, SessionData.User.UserAccountId);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Account - Get Contact Details.
|
||||
//-------------------------------------------------------------------
|
||||
@@ -84,6 +98,17 @@ namespace Saxonite.Net.Core.Web.Online.Controllers
|
||||
return Json(responseData.SetGoHome(messageTitle, @Localiser.GetJavascriptText("msg.something.gone.wrong")));
|
||||
}
|
||||
|
||||
// Logically delete any previous 'Uploaded' Avatar image.
|
||||
ItemId userItemId = GetUserItemId(SessionData.User.UserAccountId);
|
||||
SaxItemImage itemImageAvatarUploaded = ItemDataMgr.GetItemImage(userItemId, Sax.ImageType.AvatarUpload, SaxItemImage.StatusCat.ActiveOnly);
|
||||
if (itemImageAvatarUploaded != null)
|
||||
{
|
||||
itemImageAvatarUploaded.Status = SaStatus.Deleted;
|
||||
if (!ItemDataMgr.UpdateItemImage(itemImageAvatarUploaded, DbUpdateInfo))
|
||||
SaLogger.Warn(fn + " Failed to logically delete uploaded avatar image - " + ItemDataMgr.ErrorStateMessage);
|
||||
}
|
||||
|
||||
|
||||
// Populate data to response.
|
||||
responseData.Contact = new ContactResponseData();
|
||||
PopulateContactResponseData(responseData.Contact, userAccount);
|
||||
@@ -91,6 +116,93 @@ namespace Saxonite.Net.Core.Web.Online.Controllers
|
||||
return Json(responseData);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Account - Upload Contact Image.
|
||||
// Ref. http://www.binaryintellect.net/articles/474dc031-087f-4b81-b994-9ca2adb949d6.aspx
|
||||
//-------------------------------------------------------------------
|
||||
public class AjaxResponse_UploadContactImage : AjaxResponse_Data
|
||||
{
|
||||
public String UploadMsg { get; set; }
|
||||
}
|
||||
// Ajax request.
|
||||
[HttpPost]
|
||||
[Route("Account/Ajax/UploadContactImage")]
|
||||
public IActionResult UploadContactImage(Model_Account model)
|
||||
{
|
||||
String fn = GetType().Name + ".UploadContactImage";
|
||||
AjaxResponse_UploadContactImage responseData = new AjaxResponse_UploadContactImage();
|
||||
String messageTitle = Localiser["account.caption.profile.contact"];
|
||||
|
||||
// Validate session and referrer.
|
||||
if (!ValidReferer)
|
||||
return Forbid();
|
||||
if (!ValidUserSession)
|
||||
return GetAjaxResult_InvalidSession(responseData);
|
||||
|
||||
// Validate number of images.
|
||||
if (model.AccountContactImage.IsEmpty())
|
||||
{
|
||||
SaLogger.Warn(fn + " No image file supplied for upload");
|
||||
return Json(responseData);
|
||||
}
|
||||
if (model.AccountContactImage.Count != 1)
|
||||
{
|
||||
SaLogger.Warn(fn + " Invalid number of image files uploaded: " + model.AccountContactImage.Count);
|
||||
return Json(responseData.SetControlError("AccountContactImage", "Only one image can be uploaded."));
|
||||
}
|
||||
IFormFile file = model.AccountContactImage[0];
|
||||
|
||||
// Image properties.
|
||||
byte[] imageData = null;
|
||||
using (MemoryStream fileStream = new MemoryStream())
|
||||
{
|
||||
file.CopyTo(fileStream);
|
||||
imageData = fileStream.ToArray();
|
||||
}
|
||||
|
||||
// Validate the image.
|
||||
ImageUtils.ImageConfig imageConfig = ImageUtils.VerifyImageData(imageData);
|
||||
if (!imageConfig.ValidImage)
|
||||
return Json(responseData.SetControlError("AccountContactImage", "Invalid image file uploaded."));
|
||||
|
||||
// Image belongs to the user's account.
|
||||
ItemId userItemId = GetUserItemId(SessionData.User.UserAccountId);
|
||||
|
||||
// Store uploaded image as 'Upload' Avatar - awaiting confirmation that the contact details are to be saved.
|
||||
SaxItemImage itemImage = ItemDataMgr.GetItemImage(userItemId, Sax.ImageType.AvatarUpload, SaxItemImage.StatusCat.Any);
|
||||
if (itemImage == null)
|
||||
itemImage = ItemDataMgr.CreateItemImage(userItemId, Sax.ImageType.AvatarUpload);
|
||||
itemImage.Filename = file.FileName;
|
||||
itemImage.Mime = SacsContentTypeHelper.GetMimeTypeFromContentType(file.ContentType).ToString().ToLower();
|
||||
itemImage.ContentType = file.ContentType;
|
||||
itemImage.Width = imageConfig.Width;
|
||||
itemImage.Height = imageConfig.Height;
|
||||
itemImage.ImageData = imageData;
|
||||
itemImage.Status = SaxItemImage.DefaultStatus;
|
||||
|
||||
if (!SysId.IsValid(itemImage.Id))
|
||||
{
|
||||
if (!ItemDataMgr.InsertItemImage(itemImage, DbUpdateInfo))
|
||||
{
|
||||
SaLogger.Warn(fn + " Failed to insert uploaded image - " + ItemDataMgr.ErrorStateMessage);
|
||||
return Json(responseData.SetControlError("AccountContactImage", Localiser["msg.error.item.insert.failed"]));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!ItemDataMgr.UpdateItemImage(itemImage, DbUpdateInfo))
|
||||
{
|
||||
SaLogger.Warn(fn + " Failed to update uploaded image - " + ItemDataMgr.ErrorStateMessage);
|
||||
return Json(responseData.SetControlError("AccountContactImage", Localiser["msg.error.item.insert.failed"]));
|
||||
}
|
||||
}
|
||||
|
||||
// Complete response.
|
||||
//responseData.SetNotification(messageTitle, "Image uploaded.");
|
||||
responseData.Success = true;
|
||||
return Json(responseData);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Account - Store Contact Details.
|
||||
//-------------------------------------------------------------------
|
||||
@@ -160,6 +272,45 @@ namespace Saxonite.Net.Core.Web.Online.Controllers
|
||||
// Refresh profile contact details to session.
|
||||
RefreshSessionProfileContact(userAccount);
|
||||
|
||||
// Check for uploaded profile image.
|
||||
ItemId userItemId = GetUserItemId(SessionData.User.UserAccountId);
|
||||
SaxItemImage itemImageAvatarUploaded = ItemDataMgr.GetItemImage(userItemId, Sax.ImageType.AvatarUpload, SaxItemImage.StatusCat.ActiveOnly);
|
||||
if (itemImageAvatarUploaded != null)
|
||||
{
|
||||
// Retrieve current Avatar image.
|
||||
SaxItemImage itemImageAvatar = ItemDataMgr.GetItemImage(userItemId, Sax.ImageType.Avatar, SaxItemImage.StatusCat.Any);
|
||||
if (itemImageAvatar == null)
|
||||
itemImageAvatar = ItemDataMgr.CreateItemImage(userItemId, Sax.ImageType.Avatar);
|
||||
// Copy properties from uploaded image.
|
||||
itemImageAvatar.Filename = itemImageAvatarUploaded.Filename;
|
||||
itemImageAvatar.Mime = itemImageAvatarUploaded.Mime;
|
||||
itemImageAvatar.ContentType = itemImageAvatarUploaded.ContentType;
|
||||
itemImageAvatar.Width = itemImageAvatarUploaded.Width;
|
||||
itemImageAvatar.Height = itemImageAvatarUploaded.Height;
|
||||
itemImageAvatar.ImageData = itemImageAvatarUploaded.ImageData;
|
||||
itemImageAvatar.Status = SaxItemImage.DefaultStatus;
|
||||
if (!SysId.IsValid(itemImageAvatar.Id))
|
||||
{
|
||||
if (!ItemDataMgr.InsertItemImage(itemImageAvatar, DbUpdateInfo))
|
||||
{
|
||||
SaLogger.Warn(fn + " Failed to insert avatar image - " + ItemDataMgr.ErrorStateMessage);
|
||||
return Json(responseData.SetControlError("AccountContactImage", Localiser["msg.error.item.insert.failed"]));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!ItemDataMgr.UpdateItemImage(itemImageAvatar, DbUpdateInfo))
|
||||
{
|
||||
SaLogger.Warn(fn + " Failed to update avatar image - " + ItemDataMgr.ErrorStateMessage);
|
||||
return Json(responseData.SetControlError("AccountContactImage", Localiser["msg.error.item.update.failed"]));
|
||||
}
|
||||
}
|
||||
// Logically delete the uplaoded image.
|
||||
itemImageAvatarUploaded.Status = SaStatus.Deleted;
|
||||
if (!ItemDataMgr.UpdateItemImage(itemImageAvatarUploaded, DbUpdateInfo))
|
||||
SaLogger.Warn(fn + " Failed to logically delete uploaded avatar image - " + ItemDataMgr.ErrorStateMessage);
|
||||
}
|
||||
|
||||
// Construct response.
|
||||
responseData.Contact = new ContactResponseData();
|
||||
PopulateContactResponseData(responseData.Contact, userAccount);
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
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;
|
||||
|
||||
namespace Saxonite.Net.Core.Web.Online.Controllers
|
||||
{
|
||||
//=======================================================================
|
||||
// Online Image Controller - delivers dynamic images.
|
||||
//=======================================================================
|
||||
public partial class ImageController : SaxController_Online
|
||||
{
|
||||
//-------------------------------------------------------------------
|
||||
// Data members.
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// 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 = SessionData.User.UserAccountId;
|
||||
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 = SessionData.User.UserAccountId;
|
||||
return GetAccountContactImage(userAccountId, Sax.ImageType.AvatarUpload);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
/// <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.AvatarUpload);
|
||||
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);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
}
|
||||
//=======================================================================
|
||||
}
|
||||
@@ -33,6 +33,7 @@ namespace Saxonite.Net.Core.Web.Online.Controllers
|
||||
public class ActivityResponseData
|
||||
{
|
||||
public short ItemType { get; set; }
|
||||
public String ItemTypeClass { get; set; }
|
||||
public long ItemIdx { get; set; }
|
||||
public String Caption { get; set; }
|
||||
public String Blurb { get; set; }
|
||||
@@ -84,6 +85,10 @@ namespace Saxonite.Net.Core.Web.Online.Controllers
|
||||
{
|
||||
ActivityResponseData activityData = new ActivityResponseData();
|
||||
activityData.ItemType = itemData.ItemId.Type;
|
||||
if (itemData.ItemId.Type == (int)Sax.DataItemType.JournalistArticle)
|
||||
activityData.ItemTypeClass = "Article";
|
||||
else if (itemData.ItemId.Type == (int)Sax.DataItemType.OrganisationOpportunity)
|
||||
activityData.ItemTypeClass = "Opportunity";
|
||||
// ToDo Mask activity index Id with cross-reference stored in session data.
|
||||
activityData.ItemIdx = SysId.ToLong(itemData.ItemId.Id);
|
||||
activityData.Caption = itemData.Caption;
|
||||
|
||||
Reference in New Issue
Block a user