internal static ImageRecordTableEntity RetrieveAnyImageRecordForFormatKey(string accountId, string storagePartition, string imageFormatGroupTypeNameKey, string imageRecordKey)
        {
            //CloudTableClient cloudTableClient = Sahara.Core.Settings.Azure.Storage.StorageConnections.AccountsStorage.CreateCloudTableClient();
            CloudTableClient cloudTableClient = Settings.Azure.Storage.GetStoragePartitionAccount(storagePartition).CreateCloudTableClient();

            //Create and set retry policy
            //IRetryPolicy exponentialRetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(1), 4);
            IRetryPolicy linearRetryPolicy = new LinearRetry(TimeSpan.FromSeconds(1), 4);

            cloudTableClient.DefaultRequestOptions.RetryPolicy = linearRetryPolicy;

            CloudTable cloudTable = null;

            ImageRecordTableEntity imageRecordEntity = null;

            //We do not use the listing table as any duplicate records that are deleted in the "Main" table will also not exist there.
            cloudTable = cloudTableClient.GetTableReference(Sahara.Core.Common.Methods.SchemaNames.AccountIdToTableStorageName(accountId) + ImageRecordTableName(imageFormatGroupTypeNameKey));

            //var context = cloudTable.ServiceClient.GetTableServiceContext();
            //context.IgnoreResourceNotFoundException = true; //<--

            //cloudTableClient.DefaultRequestOptions.

            try
            {
                imageRecordEntity = (from record in cloudTable.CreateQuery <ImageRecordTableEntity>().Where(p => p.RowKey == imageRecordKey) select record).FirstOrDefault(); //new TableQuery<ImageRecordTableEntity>().AsQueryable().Where(p => p.PartitionKey == objectId).ToList();
            }
            catch (Exception e)
            {
                if (e.InnerException.Message.Contains("404"))
                {
                    imageRecordEntity = null; //<-- This particular exception ACTUALLY means that we checkd successfully BUT no records exist.
                }
                else
                {
                    imageRecordEntity = new ImageRecordTableEntity();//<-- We new up the object so that we don't unsafely delete a format in case of connection issues with Table Storage
                }
            }

            return(imageRecordEntity);
        }
        internal static DataAccessResponseType StoreImageRecord(string accountId, string storagePartition, string imageGroupTypeNameKey, string objectId, string imageGroupName, string imageGroupNameKey, string imageFormatName, string imageFormatNameKey, string title, string description, string url, string filename, string filepath, string containerName, string blobPath, int height, int width, bool isListing = false)
        {
            var response = new DataAccessResponseType();

            var imageRecordEntity = new ImageRecordTableEntity
            {
                //PartitionKey = objectId,
                //RowKey = imageGroupNameKey + "-" + imageFormatNameKey,

                ObjectId = objectId,                                     //<-- Partitionkey
                ImageKey = imageGroupNameKey + "-" + imageFormatNameKey, //<-- RowKey

                ImageGroup     = imageGroupName,
                ImageGroupKey  = imageGroupNameKey,
                ImageFormat    = imageFormatName,
                ImageFormatKey = imageFormatNameKey,

                Title       = title,
                Description = description,

                Url      = url,
                FileName = filename,
                FilePath = filepath,

                BlobPath      = blobPath,
                ContainerName = containerName,

                Height = height,
                Width  = width
            };


            //CloudTableClient cloudTableClient = Settings.Azure.Storage.StorageConnections.AccountsStorage.CreateCloudTableClient();
            CloudTableClient cloudTableClient = Settings.Azure.Storage.GetStoragePartitionAccount(storagePartition).CreateCloudTableClient();

            //Create and set retry policy--------
            IRetryPolicy linearRetryPolicy = new LinearRetry(TimeSpan.FromSeconds(1), 4);

            cloudTableClient.DefaultRequestOptions.RetryPolicy = linearRetryPolicy;


            TableOperation operation = TableOperation.InsertOrReplace((imageRecordEntity as TableEntity));

            string tableName        = Sahara.Core.Common.Methods.SchemaNames.AccountIdToTableStorageName(accountId) + Internal.ImageRecordTableStorage.ImageRecordTableName(imageGroupTypeNameKey);        //<-- accxxxxxproductimages / accxxxxxcategoryimages  / accxxxxxaccountimages
            string listingTablename = Sahara.Core.Common.Methods.SchemaNames.AccountIdToTableStorageName(accountId) + Internal.ImageRecordTableStorage.ImageRecordListingTableName(imageGroupTypeNameKey); //<-- accxxxxxproductimages / accxxxxxcategoryimages  / accxxxxxaccountimages

            if (tableName.Length > 63 || listingTablename.Length > 63)
            {
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "Storage table names cannot be longer than 63 characters!"
                });
            }


            try
            {
                CloudTable cloudTable = cloudTableClient.GetTableReference(tableName);  //<-- accxxxxxproductimages / accxxxxxcategoryimages  / accxxxxxaccountimages
                cloudTable.CreateIfNotExists();
                var tableResult = cloudTable.Execute(operation);


                if (isListing)
                {
                    //If this is a listing, we also add a copy to the listing variation of the table
                    CloudTable cloudTable2 = cloudTableClient.GetTableReference(listingTablename);  //<-- accxxxxxproductimages / accxxxxxcategoryimages  / accxxxxxaccountimages
                    cloudTable2.CreateIfNotExists();
                    var tableResult2 = cloudTable2.Execute(operation);

                    response.isSuccess = true; //tableResult.;
                }
                else
                {
                    response.isSuccess = true;
                }
            }
            catch
            {
                response.isSuccess = false; //tableResult.;
                //response.ErrorMessage = "image exists";
            }


            return(response);
        }
Пример #3
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);
            }