public static DataAccessResponseType DeleteGalleryImage(Account account, string imageFormatGroupTypeNameKey, string objectId, string imageGroupNameKey, string imageFormatNameKey, int imageIndex)
        {
            //Get the records (so we can loop through blobs for deletion)
            var imageRecords = ImageRecordsManager.GetImageRecordsForObject(account.AccountID.ToString(), account.StoragePartition, account.AccountNameKey, imageFormatGroupTypeNameKey, objectId);

            ImageRecordModel imageRecord = null;

            #region  Get the record associated with the gallery deletion request
            //Delete all Blobs associated with ALL image records (loop if gallery)
            foreach (ImageRecordGroupModel group in imageRecords)
            {
                if (group.GroupNameKey == imageGroupNameKey)
                {
                    foreach (ImageRecordModel record in group.ImageRecords)
                    {
                        if (record.FormatNameKey == imageFormatNameKey)
                        {
                            if (record.Type != "gallery")
                            {
                                return(new DataAccessResponseType {
                                    isSuccess = false, ErrorMessage = "This image record is not a gallery"
                                });
                            }
                            else
                            {
                                //Assign to the record object
                                imageRecord = record;

                                ImageStorageManager.DeleteImageBlobs(account.StoragePartition, record.ContainerName, record.GalleryImages[imageIndex].BlobPath);
                            }
                        }
                    }
                }
            }

            #endregion

            //if this is the only image in the gallery we delete the entire record
            if (imageRecord.GalleryImages.Count <= 1)
            {
                //This is the last image so we delete the ENTIRE record
                return(Internal.ImageRecordTableStorage.DeleteImageRecord(account.AccountID.ToString(), account.StoragePartition, imageFormatGroupTypeNameKey, objectId, imageGroupNameKey, imageFormatNameKey));
            }
            else
            {
                //Update record to remove the array index on ALL comma deliminated records
                return(Internal.ImageRecordTableStorage.DeleteImageGalleryRecordItem(account.AccountID.ToString(), account.StoragePartition, imageFormatGroupTypeNameKey, objectId, imageGroupNameKey, imageFormatNameKey, imageIndex));
            }
        }
예제 #2
0
            public static ImageRecordModel ImageFormat_To_ImageRecord(ImageFormatModel imageFormat)
            {
                var imageRecord = new ImageRecordModel();

                if (!imageFormat.Gallery)
                {
                    imageRecord.Type = "single";
                }
                else
                {
                    imageRecord.Type = "gallery";
                }

                imageRecord.Height = imageFormat.Height;
                imageRecord.Width  = imageFormat.Width;

                imageRecord.FormatName    = imageFormat.ImageFormatName;
                imageRecord.FormatNameKey = imageFormat.ImageFormatNameKey;

                return(imageRecord);
            }
예제 #3
0
        //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);
        }
예제 #4
0
            public static ImageRecordModel TableEntity_To_ImageRecord(string cdnEndpoint, ImageRecordTableEntity imageRecordTableEntity, ImageFormatModel imageFormat, bool isGallery = false)
            {
                var imageRecord = new ImageRecordModel();


                imageRecord.Height = imageRecordTableEntity.Height;
                imageRecord.Width  = imageRecordTableEntity.Width;

                if (!isGallery)
                {
                    imageRecord.Type = "single";

                    imageRecord.Title       = imageRecordTableEntity.Title;
                    imageRecord.Description = imageRecordTableEntity.Description;

                    imageRecord.BlobPath = imageRecordTableEntity.BlobPath;
                    imageRecord.Url      = cdnEndpoint + imageRecordTableEntity.Url;
                    imageRecord.FileName = imageRecordTableEntity.FileName;
                    imageRecord.FilePath = imageRecordTableEntity.FilePath;

                    imageRecord.BlobPath_sm = imageRecordTableEntity.BlobPath.Replace(".jpg", "_sm.jpg").Replace(".gif", "_sm.gif").Replace(".png", "_sm.png");
                    imageRecord.Url_sm      = cdnEndpoint + imageRecordTableEntity.Url.Replace(".jpg", "_sm.jpg").Replace(".gif", "_sm.gif").Replace(".png", "_sm.png");
                    imageRecord.FileName_sm = imageRecordTableEntity.FileName.Replace(".jpg", "_sm.jpg").Replace(".gif", "_sm.gif").Replace(".png", "_sm.png");
                    imageRecord.FilePath_sm = imageRecordTableEntity.FilePath.Replace(".jpg", "_sm.jpg").Replace(".gif", "_sm.gif").Replace(".png", "_sm.png");

                    imageRecord.BlobPath_xs = imageRecordTableEntity.BlobPath.Replace(".jpg", "_xs.jpg").Replace(".gif", "_xs.gif").Replace(".png", "_xs.png");
                    imageRecord.Url_xs      = cdnEndpoint + imageRecordTableEntity.Url.Replace(".jpg", "_xs.jpg").Replace(".gif", "_xs.gif").Replace(".png", "_xs.png");
                    imageRecord.FileName_xs = imageRecordTableEntity.FileName.Replace(".jpg", "_xs.jpg").Replace(".gif", "_xs.gif").Replace(".png", "_xs.png");
                    imageRecord.FilePath_xs = imageRecordTableEntity.FilePath.Replace(".jpg", "_xs.jpg").Replace(".gif", "_xs.gif").Replace(".png", "_xs.png");
                }
                else
                {
                    imageRecord.Type = "gallery";

                    var blobPaths = imageRecordTableEntity.BlobPath.Split('|').ToList();

                    var titles       = imageRecordTableEntity.Title.Split('|').ToList();
                    var descriptions = imageRecordTableEntity.Description.Split('|').ToList();
                    var urls         = imageRecordTableEntity.Url.Split('|').ToList();
                    var fileNames    = imageRecordTableEntity.FileName.Split('|').ToList();
                    var filePaths    = imageRecordTableEntity.FilePath.Split('|').ToList();

                    if (urls.Count > 0)
                    {
                        imageRecord.GalleryImages = new List <ImageRecordGalleryModel>();

                        for (int i = 0; i < urls.Count; i++)
                        {
                            var imageGalleryRecord = new ImageRecordGalleryModel
                            {
                                Url    = cdnEndpoint + urls[i],
                                Url_sm = cdnEndpoint + urls[i].Replace(".jpg", "_sm.jpg").Replace(".gif", "_sm.gif").Replace(".png", "_sm.png"),
                                Url_xs = cdnEndpoint + urls[i].Replace(".jpg", "_xs.jpg").Replace(".gif", "_xs.gif").Replace(".png", "_xs.png")
                            };

                            try
                            {
                                imageGalleryRecord.Title       = titles[i];
                                imageGalleryRecord.Description = descriptions[i];

                                imageGalleryRecord.BlobPath = blobPaths[i];
                                imageGalleryRecord.FileName = fileNames[i];
                                imageGalleryRecord.FilePath = filePaths[i];

                                imageGalleryRecord.BlobPath_sm = blobPaths[i].Replace(".jpg", "_sm.jpg").Replace(".gif", "_sm.gif").Replace(".png", "_sm.png");
                                imageGalleryRecord.FileName_sm = fileNames[i].Replace(".jpg", "_sm.jpg").Replace(".gif", "_sm.gif").Replace(".png", "_sm.png");
                                imageGalleryRecord.FilePath_sm = filePaths[i].Replace(".jpg", "_sm.jpg").Replace(".gif", "_sm.gif").Replace(".png", "_sm.png");

                                imageGalleryRecord.BlobPath_xs = blobPaths[i].Replace(".jpg", "_xs.jpg").Replace(".gif", "_xs.gif").Replace(".png", "_xs.png");
                                imageGalleryRecord.FileName_xs = fileNames[i].Replace(".jpg", "_xs.jpg").Replace(".gif", "_xs.gif").Replace(".png", "_xs.png");
                                imageGalleryRecord.FilePath_xs = filePaths[i].Replace(".jpg", "_xs.jpg").Replace(".gif", "_xs.gif").Replace(".png", "_xs.png");
                            }
                            catch
                            {
                            }

                            imageRecord.GalleryImages.Add(imageGalleryRecord);
                        }
                    }
                }

                imageRecord.FormatName    = imageFormat.ImageFormatName;
                imageRecord.FormatNameKey = imageFormat.ImageFormatNameKey;

                return(imageRecord);
            }