Ejemplo n.º 1
0
        public bool SaveResourceToDisk(HttpPostedFileBase mainFile, Image img, string svrImagePath)
        {
            try
            {
                bool saved = false;

                string imageExt  = string.Empty;
                string imageName = string.Empty;

                bool hasResourceFile = mainFile != null && mainFile.ContentLength > 0;

                if (hasResourceFile)
                {
                    string id = Guid.NewGuid().ToString();
                    imageExt  = System.IO.Path.GetExtension(mainFile.FileName);
                    imageName = String.Concat(id, imageExt);
                    string path = System.IO.Path.Combine(svrImagePath, imageName);

                    bool isValidItem = Helpers.ConfigurationHelper.AuthorizedImagesExt().Contains(imageExt.ToLower());
                    int  sizeKb      = Convert.ToInt32(Math.Round(Convert.ToDecimal(mainFile.ContentLength / 1024), 0));

                    int maxAllowedSize = 0;

                    switch (img.Type)
                    {
                    case "CAROUSEL":
                        maxAllowedSize = Helpers.ConfigurationHelper.MaxUploadSizeCarousel();
                        break;

                    case "GALLERY1":
                    case "GALLERY2":
                        maxAllowedSize = Helpers.ConfigurationHelper.MaxUploadSizeGallery();
                        break;

                    case "THUMB1":
                    case "THUMB2":
                        maxAllowedSize = Helpers.ConfigurationHelper.MaxUploadSizeThumb();
                        break;

                    default:
                        break;
                    }



                    if (isValidItem && (sizeKb <= maxAllowedSize))
                    {
                        mainFile.SaveAs(path);

                        IContentRepository repos = new ContentRepository();
                        img.ImageName = imageName;

                        saved = repos.CreateImage(img);

                        if (saved)
                        {
                            Helpers.CacheHelper.RemoveObjectFromCache(Helpers.CacheHelperKeys.CK_ALL_PAGES);
                        }
                    }
                }
                return(saved);
            }
            catch (Exception)
            {
                throw;
            }
        }