public DataAccessResponseType CreateImageFormat(string accountNameKey, string imageGroupTypeNameKey, string imageGroupNameKey, string imageFormatName, int width, int height, bool listing, bool gallery, string requesterId, RequesterType requesterType, string sharedClientKey) { // Ensure the clients are certified. if (sharedClientKey != Sahara.Core.Platform.Requests.RequestManager.SharedClientKey) { return(null); } //Get ACCOUNT var account = AccountManager.GetAccount(accountNameKey, true, AccountManager.AccountIdentificationType.AccountName); #region Validate Request var requesterName = string.Empty; var requesterEmail = string.Empty; var requestResponseType = RequestManager.ValidateRequest(requesterId, requesterType, out requesterName, out requesterEmail, Sahara.Core.Settings.Platform.Users.Authorization.Roles.Admin, Sahara.Core.Settings.Accounts.Users.Authorization.Roles.Admin); if (!requestResponseType.isApproved) { //Request is not approved, send results: return(new DataAccessResponseType { isSuccess = false, ErrorMessage = requestResponseType.requestMessage }); } #endregion if (imageGroupNameKey == "default") { return(new DataAccessResponseType { isSuccess = false, ErrorMessage = "You cannot append new formats to the default group" }); } if (imageGroupNameKey == "main") { return(new DataAccessResponseType { isSuccess = false, ErrorMessage = "You cannot append new formats to the main group" }); } var result = ImageFormatsManager.CreateImageFormat(account, imageGroupTypeNameKey, imageGroupNameKey, imageFormatName, width, height, listing, gallery); if (result.isSuccess) { #region Invalidate Account Capacity Cache AccountCapacityManager.InvalidateAccountCapacitiesCache(account.AccountID.ToString()); #endregion } return(result); }
public DataAccessResponseType DeleteImageFormat(string accountNameKey, string imageGroupTypeNameKey, string imageGroupNameKey, string imageFormatNameKey, string requesterId, RequesterType requesterType, string sharedClientKey) { // Ensure the clients are certified. if (sharedClientKey != Sahara.Core.Platform.Requests.RequestManager.SharedClientKey) { return(null); } //Get ACCOUNT var account = AccountManager.GetAccount(accountNameKey, true, AccountManager.AccountIdentificationType.AccountName); #region Validate Request var requesterName = string.Empty; var requesterEmail = string.Empty; var requestResponseType = RequestManager.ValidateRequest(requesterId, requesterType, out requesterName, out requesterEmail, Sahara.Core.Settings.Platform.Users.Authorization.Roles.Manager, Sahara.Core.Settings.Accounts.Users.Authorization.Roles.Manager); if (!requestResponseType.isApproved) { //Request is not approved, send results: return(new DataAccessResponseType { isSuccess = false, ErrorMessage = requestResponseType.requestMessage }); } #endregion #region Validate that no image records exist exist that use this format if (ImageRecordsManager.ImageRecordExistsForImageKey(account.AccountID.ToString(), account.StoragePartition, account.AccountNameKey, imageGroupTypeNameKey, imageGroupNameKey + "-" + imageFormatNameKey)) { //Request is not approved, send results: return(new DataAccessResponseType { isSuccess = false, ErrorMessage = "Cannot delete an image format that has any image records associated with it." }); } #endregion var result = ImageFormatsManager.DeleteImageFormat(account, imageGroupTypeNameKey, imageGroupNameKey, imageFormatNameKey); if (result.isSuccess) { #region Invalidate Account Capacity Cache AccountCapacityManager.InvalidateAccountCapacitiesCache(account.AccountID.ToString()); #endregion } return(result); }
public DataAccessResponseType UpdateImageRecordTitle(string accountId, string objectType, string objectId, string groupNameKey, string formatNameKey, string newTitle, string requesterId, RequesterType requesterType, string sharedClientKey) { // Ensure the clients are certified. if (sharedClientKey != Sahara.Core.Platform.Requests.RequestManager.SharedClientKey) { return(null); } var account = AccountManager.GetAccount(accountId); #region Validate Request var requesterName = string.Empty; var requesterEmail = string.Empty; var requestResponseType = RequestManager.ValidateRequest(requesterId, requesterType, out requesterName, out requesterEmail, Sahara.Core.Settings.Platform.Users.Authorization.Roles.Manager, Sahara.Core.Settings.Accounts.Users.Authorization.Roles.Manager); if (!requestResponseType.isApproved) { //Request is not approved, send results: return(new DataAccessResponseType { isSuccess = false, ErrorMessage = requestResponseType.requestMessage }); } #endregion #region Get image format to determine if this is a listing ImageFormatGroupModel imageGroup; var imageFormat = ImageFormatsManager.GetImageFormat(account.AccountNameKey, objectType, groupNameKey, formatNameKey, out imageGroup); if (imageFormat == null) { return(null); } #endregion var result = ImageRecordsManager.UpdateImageRecordTitleForObject(accountId, account.StoragePartition, objectType, objectId, groupNameKey, formatNameKey, newTitle, imageFormat.Listing); #region Invalidate Account API Caching Layer Sahara.Core.Common.Redis.ApiRedisLayer.InvalidateAccountApiCacheLayer(account.AccountNameKey); #endregion return(result); }
public List <ImageFormatGroupModel> GetImageFormats(string accountNameKey, string imageFormatGroupTypeNameKey) { return(ImageFormatsManager.GetImageFormats(accountNameKey, imageFormatGroupTypeNameKey)); }
public List <ImageFormatGroupTypeModel> GetImageFormatGroupTypes() { return(ImageFormatsManager.GetImageFormatGroupTypes()); }
/// <summary> /// Possibly never used by clients (With the exception of Account Admin to manage images for an objet so the associated missing formats can be seen /// In most cases clients should connect to Table storage on their own to perform this merge to avoid a WCF call /// </summary> /// <param name="accountId"></param> /// <param name="accountNameKey"></param> /// <param name="imageFormatGroupTypeNameKey"></param> /// <param name="objectId"></param> /// <param name="listingsOnly"></param> /// <returns></returns> public static List <ImageRecordGroupModel> GetImageRecordsForObject(string accountId, string storagePartition, string accountNameKey, string imageFormatGroupTypeNameKey, string objectId, bool listingsOnly = false) { var imageRecords = new List <ImageRecordGroupModel>(); #region Get Image Formats/FormatGroups for this ImageFormatGroupTypeNameKey var imageFormats = ImageFormatsManager.GetImageFormats(accountNameKey, imageFormatGroupTypeNameKey); //If there are no formats ignore are return null to the user if (imageFormats.Count == 0) { return(null); } #endregion #region Get all image records for this object var imageRecordEntities = Internal.ImageRecordTableStorage.RetrieveImageRecords(accountId, storagePartition, imageFormatGroupTypeNameKey, objectId, listingsOnly); #endregion #region Convert FormatGroups/Formats to RecordGroups/Records And merge them with Records (Where records exist) foreach (ImageFormatGroupModel imageFormatGroup in imageFormats) { var imageRecordGroupModel = new ImageRecordGroupModel { GroupName = imageFormatGroup.ImageFormatGroupName, GroupNameKey = imageFormatGroup.ImageFormatGroupNameKey }; imageRecordGroupModel.ImageRecords = new List <ImageRecordModel>(); foreach (ImageFormatModel imageFormat in imageFormatGroup.ImageFormats) { if (listingsOnly && !imageFormat.Listing) { //ignore } else { #region Get the associated record for each format (if one exists, or create a null object for it if a record does not var recordExists = false; foreach (ImageRecordTableEntity tableEntity in imageRecordEntities) { if (tableEntity.ImageKey == imageFormat.ImageFormatGroupNameKey + "-" + imageFormat.ImageFormatNameKey) { recordExists = true; #region A record exists for this format, convert and remove from list imageRecordGroupModel.ImageRecords.Add(Internal.Transforms.TableEntity_To_ImageRecord(tableEntity, imageFormat, imageFormat.Gallery)); #endregion } } if (!recordExists) { #region A record does not exist, use a null version of the format slug instead imageRecordGroupModel.ImageRecords.Add(Internal.Transforms.ImageFormat_To_ImageRecord(imageFormat)); #endregion } #endregion } } if (imageRecordGroupModel.ImageRecords.Count > 0) { imageRecords.Add(imageRecordGroupModel); //<-- Only append if group has records } } #endregion // Return Image Records for this object return(imageRecords); }
//Creates initial record (as well as replacements for existing records and additions for gallery images) for ALL types (listings, galleries, etc...) public static DataAccessResponseType ProcessAndRecordApplicationImage(Account account, ImageProcessingManifestModel imageManifest, ImageCropCoordinates imageCropCoordinates, ImageEnhancementInstructions imageEnhancementInstructions = null) { var tableStorageResult = new DataAccessResponseType(); #region Get image format & group for this new record ImageFormatGroupModel imageGroup; var imageFormat = ImageFormatsManager.GetImageFormat(account.AccountNameKey, imageManifest.GroupTypeNameKey, imageManifest.GroupNameKey, imageManifest.FormatNameKey, out imageGroup); if (imageFormat == null) { return(new DataAccessResponseType { isSuccess = false, ErrorMessage = "Not a valid image format" }); } #endregion #region Get Image Record for this image slot on this object (If one exists) ImageRecordModel existingImageRecord = null; var imageRecords = ImageRecordsManager.GetImageRecordsForObject(account.AccountID.ToString(), account.StoragePartition, account.AccountNameKey, imageManifest.GroupTypeNameKey, imageManifest.ObjectId); foreach (ImageRecordGroupModel imageRecordGroupModel in imageRecords) { if (imageRecordGroupModel.GroupNameKey == imageManifest.GroupNameKey) { foreach (ImageRecordModel record in imageRecordGroupModel.ImageRecords) { if (record.FormatNameKey == imageManifest.FormatNameKey) { if (record.BlobPath != null || record.GalleryImages.Count > 0) { existingImageRecord = record; //<-- We only assign it if images exist (single or gallery) } } } } } #endregion #region Process image according to ImageFormat specs, save to blob storage for the AccountID //var folderName = account.AccountID.ToString() + "/" + imageManifest.GroupTypeNameKey + "images"; var folderName = imageManifest.GroupTypeNameKey + "Images"; //Override of "Product" to "Item" ----------------- if (folderName == "productImages") { folderName = "itemImages"; } //------------------------------------------------ var applicationImageProcessor = new Imaging.ApplicationImageProcessor(); if (imageManifest.Title.Length > Settings.Objects.Limitations.ImageFormatTitleMaxLength) { return(new DataAccessResponseType { isSuccess = false, ErrorMessage = "Image titles cannot be longer than " + Settings.Objects.Limitations.ImageFormatTitleMaxLength + " characters" }); } if (imageManifest.Description.Length > Settings.Objects.Limitations.ImageFormatDescriptionMaxLength) { return(new DataAccessResponseType { isSuccess = false, ErrorMessage = "Image descriptions cannot be longer than " + Settings.Objects.Limitations.ImageFormatDescriptionMaxLength + " characters" }); } //If this is an addition to an image gallery. Make sure we are within safe limits before creating the image if (existingImageRecord != null && imageFormat.Gallery && existingImageRecord.GalleryImages.Count >= account.PaymentPlan.MaxImagesPerGallery) { return(new DataAccessResponseType { isSuccess = false, ErrorMessage = "Your plan does not allow for more than " + account.PaymentPlan.MaxImagesPerGallery + " images per gallery" }); } //If this is a gallery type we cannot use pipes "|" in titles or descriptions if (imageFormat.Gallery && imageManifest.Title.Contains("|")) { return(new DataAccessResponseType { isSuccess = false, ErrorMessage = "Cannot use the '|' character in gallery titles." }); } if (imageFormat.Gallery && imageManifest.Description.Contains("|")) { return(new DataAccessResponseType { isSuccess = false, ErrorMessage = "Cannot use the '|' character in gallery descriptions." }); } var processingResult = applicationImageProcessor.ProcessApplicationImage(account.AccountID.ToString(), account.StoragePartition, imageManifest.SourceContainerName, imageManifest.FileName, imageFormat.Width, imageFormat.Height, folderName, imageManifest.Type, imageManifest.Quality, imageCropCoordinates, imageEnhancementInstructions); #endregion if (processingResult.isSuccess) { //var cdnUrl = Settings.Azure.Storage.GetStoragePartition(account.StoragePartition).CDN; var newContainerName = account.AccountID.ToString(); var newImageFileName = processingResult.SuccessMessage + "." + imageManifest.Type; //var newUrl = "http://" + cdnUrl + "/" + newContainerName + "/" + folderName + "/" + newImageFileName; //<-- CDN now assigned by consumers in case of future migration var newUrl = newContainerName + "/" + folderName + "/" + newImageFileName; var newBlobPath = folderName + "/" + newImageFileName; var newFilePath = newContainerName + "/" + newBlobPath; #region Record data to table storage if (existingImageRecord == null || !imageFormat.Gallery) { // This is the first image added to this record (gallery or single) #region Store or Replace Image Record for this ObjectType/ObjectID tableStorageResult = ImageRecordsManager.CreateImageRecordForObject( account.AccountID.ToString(), account.StoragePartition, imageManifest.GroupTypeNameKey, imageManifest.ObjectId, imageGroup.ImageFormatGroupName, imageGroup.ImageFormatGroupNameKey, imageFormat.ImageFormatName, imageFormat.ImageFormatNameKey, imageManifest.Title, imageManifest.Description, newUrl, newImageFileName, newFilePath, newContainerName, newBlobPath, imageFormat.Height, imageFormat.Width, imageFormat.Listing ); #endregion } else { //This is an addition to a Galley, Append to the array of gallery image (And do not delete any previous blobs) #region Append to gallery Image Record for this ObjectType/ObjectID tableStorageResult = ImageRecordsManager.AppendToImageGalleryRecordForObject( account, imageManifest.GroupTypeNameKey, imageManifest.ObjectId, imageGroup.ImageFormatGroupName, imageGroup.ImageFormatGroupNameKey, imageFormat.ImageFormatName, imageFormat.ImageFormatNameKey, imageManifest.Title, imageManifest.Description, newUrl, newImageFileName, newFilePath, newBlobPath ); #endregion } #endregion // Add URL for UI to preload: tableStorageResult.SuccessMessage = newUrl; #region Clean up previous blob data if (existingImageRecord == null || imageFormat.Gallery) { //Do nothing, this was the first image created for this slot, or was an image appended to a gallery } else { //This is a replacement on a single image. Delete the previous image from blob storage #region Delete previous blob image ImageStorageManager.DeleteImageBlobs(account.StoragePartition, existingImageRecord.ContainerName, existingImageRecord.BlobPath); #endregion } #endregion if (!tableStorageResult.isSuccess) { #region There was an issue recording data to table storage. Delete the blobs we just created try { ImageStorageManager.DeleteImageBlobs(account.StoragePartition, newContainerName, newBlobPath); } catch { } #endregion } } //Return results return(tableStorageResult); }
public static AccountCapacity GetAccountCapacity(string accountId, bool useCachedVersion = true) { //IDatabase cache = Sahara.Core.Settings.Azure.Redis.RedisMultiplexers.AccountManager_Multiplexer.GetDatabase(); IDatabase cache = Sahara.Core.Settings.Azure.Redis.RedisMultiplexers.RedisMultiplexer.GetDatabase(); string redisHashField = AccountCapacityHash.Fields.AccountCapacity(accountId); AccountCapacity accountCapacityCache = null; AccountCapacity accountCapacity = null; try { #region Attempt to get from Cache if (useCachedVersion) { try { var redisValue = cache.HashGet(AccountCapacityHash.Key(), redisHashField); if (redisValue.HasValue) { accountCapacityCache = JsonConvert.DeserializeObject <AccountCapacity>(redisValue); } } catch { } } #endregion } catch { } if (accountCapacityCache == null) { #region Collect Data and Store into Cache var account = AccountManager.GetAccount(accountId, true, Sahara.Core.Accounts.AccountManager.AccountIdentificationType.AccountID); accountCapacity = new AccountCapacity { //Fill in Payment Plan Limits UsersMax = account.PaymentPlan.MaxUsers, ProductsMax = account.PaymentPlan.MaxProducts, CategorizationsMax = account.PaymentPlan.MaxCategorizations, //CategoriesMax = account.PaymentPlan.MaxCategories, //SubcategoriesMax = account.PaymentPlan.MaxSubcategories, //SubsubcategoriesMax = account.PaymentPlan.MaxSubsubcategories, //SubsubsubcategoriesMax = account.PaymentPlan.MaxSubsubsubcategories, PropertiesMax = account.PaymentPlan.MaxProperties, TagsMax = account.PaymentPlan.MaxTags, CustomImageGroupsMax = account.PaymentPlan.MaxImageGroups, CustomImageFormatsMax = account.PaymentPlan.MaxImageFormats, CustomImageGalleriesMax = account.PaymentPlan.MaxImageGalleries, ImagesPerGalleryMax = account.PaymentPlan.MaxImagesPerGallery, //ProductsPerSetMax = account.PaymentPlan.MaxProductsPerSet, //SubcategoriesPerSetMax = account.PaymentPlan.MaxSubcategoriesPerSet }; //Get Counts accountCapacity.UsersCount = account.Users.Count - 1; //<--Offset by 1 to hide: platformadmin@[Config_PlatformEmail].com //TODO: Switch to Product / change name to "ProductManager" accountCapacity.ProductsCount = ProductManager.GetProductCount(account); accountCapacity.CategorizationsCount = CategorizationManager.GetCategorizationCount(account); accountCapacity.PropertiesCount = PropertiesManager.GetPropertyCount(account); accountCapacity.TagsCount = TagManager.GetTagCount(account.AccountNameKey); accountCapacity.CustomImageGroupsCount = ImageFormatsManager.GetCustomImageGroupCount(account.AccountNameKey); accountCapacity.CustomImageFormatsCount = ImageFormatsManager.GetCustomImageFormatCount(account.AccountNameKey); accountCapacity.CustomImageGalleriesCount = ImageFormatsManager.GetCustomImageGalleryCount(account.AccountNameKey); //Calculate Remaining Amounts Available accountCapacity.UsersRemaining = accountCapacity.UsersMax - accountCapacity.UsersCount; accountCapacity.ProductsRemaining = accountCapacity.ProductsMax - accountCapacity.ProductsCount; accountCapacity.CategorizationsRemaining = accountCapacity.CategorizationsMax - accountCapacity.CategorizationsCount; //accountCapacity.SubcategoriesRemaining = accountCapacity.SubcategoriesMax - accountCapacity.SubcategoriesCount; accountCapacity.PropertiesRemaining = accountCapacity.PropertiesMax - accountCapacity.PropertiesCount; accountCapacity.TagsRemaining = accountCapacity.TagsMax - accountCapacity.TagsCount; accountCapacity.CustomImageGroupsRemaining = accountCapacity.CustomImageGroupsMax - accountCapacity.CustomImageGroupsCount; accountCapacity.CustomImageFormatsRemaining = accountCapacity.CustomImageFormatsMax - accountCapacity.CustomImageFormatsCount; accountCapacity.CustomImageGalleriesRemaining = accountCapacity.CustomImageGalleriesMax - accountCapacity.CustomImageGalleriesCount; //Calculate Percentages Used --------------------------------------------------------- if (accountCapacity.UsersCount != 0 && accountCapacity.UsersMax != 0) { accountCapacity.UsersPercentageUsed = (int)(((decimal)accountCapacity.UsersCount / (decimal)accountCapacity.UsersMax) * 100); } else { accountCapacity.UsersPercentageUsed = 0; } if (accountCapacity.ProductsCount != 0 && accountCapacity.ProductsMax != 0) { accountCapacity.ProductsPercentageUsed = (int)(((decimal)accountCapacity.ProductsCount / (decimal)accountCapacity.ProductsMax) * 100); } else { accountCapacity.ProductsPercentageUsed = 0; } if (accountCapacity.CategorizationsCount != 0 && accountCapacity.CategorizationsMax != 0) { accountCapacity.CategorizationsPercentageUsed = (int)(((decimal)accountCapacity.CategorizationsCount / (decimal)accountCapacity.CategorizationsMax) * 100); } else { accountCapacity.CategorizationsPercentageUsed = 0; } if (accountCapacity.PropertiesCount != 0 && accountCapacity.PropertiesMax != 0) { accountCapacity.PropertiesPercentageUsed = (int)(((decimal)accountCapacity.PropertiesCount / (decimal)accountCapacity.PropertiesMax) * 100); } else { accountCapacity.PropertiesPercentageUsed = 0; } if (accountCapacity.TagsCount != 0 && accountCapacity.TagsMax != 0) { accountCapacity.TagsPercentageUsed = (int)(((decimal)accountCapacity.TagsCount / (decimal)accountCapacity.TagsMax) * 100); } else { accountCapacity.TagsPercentageUsed = 0; } if (accountCapacity.CustomImageGroupsCount != 0 && accountCapacity.CustomImageGroupsMax != 0) { accountCapacity.CustomImageGroupsPercentageUsed = (int)(((decimal)accountCapacity.CustomImageGroupsCount / (decimal)accountCapacity.CustomImageGroupsMax) * 100); } else { accountCapacity.CustomImageGroupsPercentageUsed = 0; } if (accountCapacity.CustomImageFormatsCount != 0 && accountCapacity.CustomImageFormatsMax != 0) { accountCapacity.CustomImageFormatsPercentageUsed = (int)(((decimal)accountCapacity.CustomImageFormatsCount / (decimal)accountCapacity.CustomImageFormatsMax) * 100); } else { accountCapacity.CustomImageFormatsPercentageUsed = 0; } if (accountCapacity.CustomImageGalleriesCount != 0 && accountCapacity.CustomImageGalleriesMax != 0) { accountCapacity.CustomImageGalleriesPercentageUsed = (int)(((decimal)accountCapacity.CustomImageGalleriesCount / (decimal)accountCapacity.CustomImageGalleriesMax) * 100); } else { accountCapacity.CustomImageGalleriesPercentageUsed = 0; } try { //Store into "capacties" Cache cache.HashSet(AccountCapacityHash.Key(), redisHashField, JsonConvert.SerializeObject(accountCapacity), When.Always, CommandFlags.FireAndForget); } catch { } #endregion } else { accountCapacity = accountCapacityCache; } return(accountCapacity); }