Exemplo n.º 1
0
        public FileStorage(ISettings settings)
        {
            _settings = settings;
            
            _resizeSettings = new ResizeSettings {
                    MaxWidth = settings.ThumbnailSize,
                    MaxHeight = settings.ThumbnailSize, 
                    Format = "jpg"
                };
            _resizeSettings.Add("quality", ImageQuality);

            //create FullsizedImagesFolder & SmallImagesFolder subfolders
            string largeFilesFolder = Path.Combine(settings.ImagesLocalFolder, FullsizedImagesFolder);
            string smallFilesFolder = Path.Combine(settings.ImagesLocalFolder, SmallImagesFolder);

            if (!Directory.Exists(largeFilesFolder))
                Directory.CreateDirectory(largeFilesFolder);
            if (!Directory.Exists(smallFilesFolder))
                Directory.CreateDirectory(smallFilesFolder);
            
            _lastPhoto = Directory
                .GetFiles(largeFilesFolder, "*.jpg")
                .Select(i => int.Parse(Path.GetFileNameWithoutExtension(i).ToLower().Replace(".jpg", "")))
                .OrderByDescending(i => i)
                .FirstOrDefault();

            if (_lastPhoto < 1)
                _lastPhoto = 1;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Resizes and returns the resized content
        /// </summary>
        /// <param name="queryString">The query string.</param>
        /// <param name="fileContent">Content of the file.</param>
        /// <returns></returns>
        private Stream GetResized(NameValueCollection queryString, Stream fileContent)
        {
            try
            {
                ResizeSettings settings = new ResizeSettings(queryString);

                if (settings["mode"] == null || settings["mode"] == "clip")
                {
                    settings.Add("mode", "max");

                    if (!string.IsNullOrEmpty(settings["width"]) && !string.IsNullOrEmpty(settings["height"]))
                    {
                        if (settings["width"].AsInteger() > settings["height"].AsInteger())
                        {
                            settings.Remove("height");
                        }
                        else
                        {
                            settings.Remove("width");
                        }
                    }
                }

                MemoryStream resizedStream = new MemoryStream();

                ImageBuilder.Current.Build(fileContent, resizedStream, settings, false);
                return(resizedStream);
            }
            catch
            {
                // if resize failed, just return original content
                return(fileContent);
            }
        }
Exemplo n.º 3
0
        //
        // GET: /Image/

        public FileResult Generate(string source, int width, int height)
        {
            string url = Uri.UnescapeDataString(source);
            HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse();
            Stream stream = httpWebReponse.GetResponseStream();

            var quality = 80;
            byte[] resized;
            using (var outStream = new MemoryStream())
            {
                var settings = new ResizeSettings
                {
                    Mode = FitMode.Crop,
                    Width = width,
                    Height = height,
                    Format = "jpg"
                };
                settings.Add("quality", quality.ToString());
                ImageBuilder.Current.Build(stream, outStream, settings);
                resized = outStream.ToArray();
            }

            stream.Dispose();

            return new FileStreamResult(new MemoryStream(resized, 0, resized.Length), "image/jpeg");
        }
Exemplo n.º 4
0
        public FileStorage(ISettings settings)
        {
            _settings = settings;

            _resizeSettings = new ResizeSettings {
                MaxWidth  = settings.ThumbnailSize,
                MaxHeight = settings.ThumbnailSize,
                Format    = "jpg"
            };
            _resizeSettings.Add("quality", ImageQuality);

            //create FullsizedImagesFolder & SmallImagesFolder subfolders
            string largeFilesFolder = Path.Combine(settings.ImagesLocalFolder, FullsizedImagesFolder);
            string smallFilesFolder = Path.Combine(settings.ImagesLocalFolder, SmallImagesFolder);

            if (!Directory.Exists(largeFilesFolder))
            {
                Directory.CreateDirectory(largeFilesFolder);
            }
            if (!Directory.Exists(smallFilesFolder))
            {
                Directory.CreateDirectory(smallFilesFolder);
            }

            _lastPhoto = Directory
                         .GetFiles(largeFilesFolder, "*.jpg")
                         .Select(i => int.Parse(Path.GetFileNameWithoutExtension(i).ToLower().Replace(".jpg", "")))
                         .OrderByDescending(i => i)
                         .FirstOrDefault();

            if (_lastPhoto < 1)
            {
                _lastPhoto = 1;
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Pres the save.
        /// </summary>
        /// <param name="dbContext">The database context.</param>
        /// <param name="entry">The entry.</param>
        public override void PreSaveChanges(Data.DbContext dbContext, DbEntityEntry entry)
        {
            if (entry.State == EntityState.Deleted)
            {
                if (StorageProvider != null)
                {
                    this.BinaryFileTypeId = entry.OriginalValues["BinaryFileTypeId"].ToString().AsInteger();

                    try
                    {
                        StorageProvider.DeleteContent(this);
                    }
                    catch (Exception ex)
                    {
                        // If an exception occurred while trying to delete provider's file, log the exception, but continue with the delete.
                        ExceptionLogService.LogException(ex);
                    }

                    this.BinaryFileTypeId = null;
                }
            }
            else
            {
                if (BinaryFileType == null && BinaryFileTypeId.HasValue)
                {
                    BinaryFileType = new BinaryFileTypeService(( RockContext )dbContext).Get(BinaryFileTypeId.Value);
                }

                if (this.MimeType.StartsWith("image/"))
                {
                    try
                    {
                        using (Bitmap bm = new Bitmap(this.ContentStream))
                        {
                            if (bm != null)
                            {
                                this.Width  = bm.Width;
                                this.Height = bm.Height;
                            }
                        }
                        ContentStream.Seek(0, SeekOrigin.Begin);

                        if (!IsTemporary)
                        {
                            if (BinaryFileType.MaxHeight.HasValue &&
                                BinaryFileType.MaxHeight != 0 &&
                                BinaryFileType.MaxWidth.HasValue &&
                                BinaryFileType.MaxWidth != 0)
                            {
                                ResizeSettings settings      = new ResizeSettings();
                                MemoryStream   resizedStream = new MemoryStream();
                                if (BinaryFileType.MaxWidth.Value < Width || BinaryFileType.MaxHeight < Height)
                                {
                                    settings.Add("mode", "max");
                                    if (BinaryFileType.MaxHeight < Height && BinaryFileType.MaxWidth < Width)
                                    {
                                        if (BinaryFileType.MaxHeight >= BinaryFileType.MaxWidth)
                                        {
                                            settings.Add("height", BinaryFileType.MaxHeight.Value.ToString());
                                        }
                                        if (BinaryFileType.MaxHeight <= BinaryFileType.MaxWidth)
                                        {
                                            settings.Add("width", BinaryFileType.MaxWidth.Value.ToString());
                                        }
                                    }
                                    else if (BinaryFileType.MaxHeight < Height)
                                    {
                                        settings.Add("height", BinaryFileType.MaxHeight.Value.ToString());
                                    }
                                    else
                                    {
                                        settings.Add("width", BinaryFileType.MaxWidth.Value.ToString());
                                    }
                                    ImageBuilder.Current.Build(this.ContentStream, resizedStream, settings);
                                    ContentStream = resizedStream;

                                    using (Bitmap bm = new Bitmap(this.ContentStream))
                                    {
                                        if (bm != null)
                                        {
                                            this.Width  = bm.Width;
                                            this.Height = bm.Height;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception) { }   // if the file is an invalid photo keep moving
                }

                if (entry.State == EntityState.Added)
                {
                    // when a file is saved (unless it is getting Deleted/Saved), it should use the StoredEntityType that is associated with the BinaryFileType
                    if (BinaryFileType != null)
                    {
                        // Persist the storage type
                        StorageEntityTypeId = BinaryFileType.StorageEntityTypeId;

                        // Persist the storage type's settings specific to this binary file type
                        var settings = new Dictionary <string, string>();
                        if (BinaryFileType.Attributes == null)
                        {
                            BinaryFileType.LoadAttributes();
                        }
                        foreach (var attributeValue in BinaryFileType.AttributeValues)
                        {
                            settings.Add(attributeValue.Key, attributeValue.Value.Value);
                        }
                        StorageEntitySettings = settings.ToJson();

                        if (StorageProvider != null)
                        {
                            // save the file to the provider's new storage medium, and if the medium returns a filesize, save that value.
                            long?outFileSize = null;
                            StorageProvider.SaveContent(this, out outFileSize);
                            if (outFileSize.HasValue)
                            {
                                FileSize = outFileSize;
                            }

                            Path = StorageProvider.GetPath(this);
                        }
                    }
                }


                else if (entry.State == EntityState.Modified)
                {
                    // when a file is saved (unless it is getting Deleted/Added),
                    // it should use the StorageEntityType that is associated with the BinaryFileType
                    if (BinaryFileType != null)
                    {
                        // if the storage provider changed, or any of its settings specific
                        // to the binary file type changed, delete the original provider's content
                        if (StorageEntityTypeId.HasValue && BinaryFileType.StorageEntityTypeId.HasValue)
                        {
                            var settings = new Dictionary <string, string>();
                            if (BinaryFileType.Attributes == null)
                            {
                                BinaryFileType.LoadAttributes();
                            }
                            foreach (var attributeValue in BinaryFileType.AttributeValues)
                            {
                                settings.Add(attributeValue.Key, attributeValue.Value.Value);
                            }
                            string settingsJson = settings.ToJson();

                            if (StorageProvider != null && (
                                    StorageEntityTypeId.Value != BinaryFileType.StorageEntityTypeId.Value ||
                                    StorageEntitySettings != settingsJson))
                            {
                                var ms = new MemoryStream();
                                ContentStream.Position = 0;
                                ContentStream.CopyTo(ms);
                                ContentStream.Dispose();

                                // Delete the current provider's storage
                                StorageProvider.DeleteContent(this);

                                // Set the new storage provider with its settings
                                StorageEntityTypeId   = BinaryFileType.StorageEntityTypeId;
                                StorageEntitySettings = settingsJson;

                                ContentStream = new MemoryStream();
                                ms.Position   = 0;
                                ms.CopyTo(ContentStream);
                                ContentStream.Position = 0;
                                FileSize = ContentStream.Length;
                            }
                        }
                    }

                    if (_contentIsDirty && StorageProvider != null)
                    {
                        long?fileSize = null;
                        StorageProvider.SaveContent(this, out fileSize);

                        FileSize = fileSize;
                        Path     = StorageProvider.GetPath(this);
                    }
                }
            }

            base.PreSaveChanges(dbContext, entry);
        }
Exemplo n.º 6
0
            protected override void PreSave()
            {
                if (Entry.State == EntityContextState.Deleted)
                {
                    if (Entity.StorageProvider != null)
                    {
                        Entity.BinaryFileTypeId = Entry.OriginalValues[nameof(Entity.BinaryFileTypeId)].ToString().AsInteger();

                        try
                        {
                            Entity.StorageProvider.DeleteContent(Entity);
                        }
                        catch (Exception ex)
                        {
                            // If an exception occurred while trying to delete provider's file, log the exception, but continue with the delete.
                            ExceptionLogService.LogException(ex);
                        }

                        Entity.BinaryFileTypeId = null;
                    }
                }
                else
                {
                    if (Entity.BinaryFileType == null && Entity.BinaryFileTypeId.HasValue)
                    {
                        Entity.BinaryFileType = new BinaryFileTypeService(( RockContext )DbContext).Get(Entity.BinaryFileTypeId.Value);
                    }

                    if (Entity.MimeType.StartsWith("image/"))
                    {
                        try
                        {
                            using (Bitmap bm = new Bitmap(Entity.ContentStream))
                            {
                                if (bm != null)
                                {
                                    Entity.Width  = bm.Width;
                                    Entity.Height = bm.Height;
                                }
                            }
                            Entity.ContentStream.Seek(0, SeekOrigin.Begin);

                            var binaryFileType                   = Entity.BinaryFileType;
                            var binaryFileTypeMaxHeight          = binaryFileType.MaxHeight ?? 0;
                            var binaryFileTypeMaxWidth           = binaryFileType.MaxWidth ?? 0;
                            var binaryFileTypeMaxHeightIsValid   = binaryFileTypeMaxHeight > 0;
                            var binaryFileTypeMaxWidthIsValid    = binaryFileTypeMaxWidth > 0;
                            var binaryFileTypeDimensionsAreValid = binaryFileTypeMaxHeightIsValid && binaryFileTypeMaxWidthIsValid;

                            ResizeSettings settings      = new ResizeSettings();
                            MemoryStream   resizedStream = new MemoryStream();
                            if ((binaryFileTypeMaxWidthIsValid && binaryFileTypeMaxWidth < Entity.Width) ||
                                (binaryFileTypeMaxHeightIsValid && binaryFileTypeMaxHeight < Entity.Height))
                            {
                                /* How to handle aspect-ratio conflicts between the image and width+height.
                                 *   'pad' adds whitespace,
                                 *   'crop' crops minimally,
                                 *   'carve' uses seam carving,
                                 *   'stretch' loses aspect-ratio, stretching the image.
                                 *   'max' behaves like maxwidth/maxheight
                                 */

                                settings.Add("mode", "max");

                                // Height and width are both set.
                                if (binaryFileTypeDimensionsAreValid)
                                {
                                    // A valid max height and width but the max height is greater or equal than the width.
                                    if (binaryFileTypeMaxHeight >= binaryFileTypeMaxWidth)
                                    {
                                        settings.Add("height", binaryFileTypeMaxHeight.ToString());
                                    }

                                    // A valid max height and width but the max height is less or equal the width.
                                    else if (binaryFileTypeMaxHeight <= binaryFileTypeMaxWidth)
                                    {
                                        settings.Add("width", binaryFileTypeMaxWidth.ToString());
                                    }
                                }
                                else
                                {
                                    // A valid max height but less than the binary file height.
                                    if (binaryFileTypeMaxHeightIsValid && binaryFileTypeMaxHeight < Entity.Height)
                                    {
                                        settings.Add("height", binaryFileTypeMaxHeight.ToString());
                                    }
                                    else
                                    {
                                        // A Valid max width.
                                        settings.Add("width", binaryFileTypeMaxWidth.ToString());
                                    }
                                }

                                if (settings.HasKeys())
                                {
                                    ImageBuilder.Current.Build(Entity.ContentStream, resizedStream, settings);
                                    Entity.ContentStream = resizedStream;

                                    using (Bitmap bm = new Bitmap(Entity.ContentStream))
                                    {
                                        if (bm != null)
                                        {
                                            Entity.Width  = bm.Width;
                                            Entity.Height = bm.Height;
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            RockLogger.Log.Error(RockLogDomains.Core, ex, "Error trying to resize the file {0}.", Entity?.FileName);
                        }
                    }

                    if (Entry.State == EntityContextState.Added)
                    {
                        // when a file is saved (unless it is getting Deleted/Saved), it should use the StoredEntityType that is associated with the BinaryFileType
                        if (Entity.BinaryFileType != null)
                        {
                            // Persist the storage type
                            Entity.StorageEntityTypeId = Entity.BinaryFileType.StorageEntityTypeId;

                            // Persist the storage type's settings specific to this binary file type
                            var settings = new Dictionary <string, string>();
                            if (Entity.BinaryFileType.Attributes == null)
                            {
                                Entity.BinaryFileType.LoadAttributes();
                            }
                            foreach (var attributeValue in Entity.BinaryFileType.AttributeValues)
                            {
                                settings.Add(attributeValue.Key, attributeValue.Value.Value);
                            }
                            Entity.StorageEntitySettings = settings.ToJson();

                            if (Entity.StorageProvider != null)
                            {
                                // save the file to the provider's new storage medium, and if the medium returns a filesize, save that value.
                                long?outFileSize = null;
                                Entity.StorageProvider.SaveContent(Entity, out outFileSize);
                                if (outFileSize.HasValue)
                                {
                                    Entity.FileSize = outFileSize;
                                }

                                Entity.Path = Entity.StorageProvider.GetPath(Entity);
                            }
                            else
                            {
                                throw new Rock.Web.FileUploadException("A storage provider has not been registered for this file type or the current storage provider is inactive.", System.Net.HttpStatusCode.BadRequest);
                            }
                        }
                    }


                    else if (Entry.State == EntityContextState.Modified)
                    {
                        // when a file is saved (unless it is getting Deleted/Added),
                        // it should use the StorageEntityType that is associated with the BinaryFileType
                        if (Entity.BinaryFileType != null)
                        {
                            // if the storage provider changed, or any of its settings specific
                            // to the binary file type changed, delete the original provider's content
                            if (Entity.StorageEntityTypeId.HasValue && Entity.BinaryFileType.StorageEntityTypeId.HasValue)
                            {
                                var settings = new Dictionary <string, string>();
                                if (Entity.BinaryFileType.Attributes == null)
                                {
                                    Entity.BinaryFileType.LoadAttributes();
                                }
                                foreach (var attributeValue in Entity.BinaryFileType.AttributeValues)
                                {
                                    settings.Add(attributeValue.Key, attributeValue.Value.Value);
                                }
                                string settingsJson = settings.ToJson();

                                if (Entity.StorageProvider != null && (
                                        Entity.StorageEntityTypeId.Value != Entity.BinaryFileType.StorageEntityTypeId.Value ||
                                        Entity.StorageEntitySettings != settingsJson))
                                {
                                    var ms = new MemoryStream();
                                    Entity.ContentStream.Position = 0;
                                    Entity.ContentStream.CopyTo(ms);
                                    Entity.ContentStream.Dispose();

                                    // Delete the current provider's storage
                                    Entity.StorageProvider.DeleteContent(Entity);

                                    // Set the new storage provider with its settings
                                    Entity.StorageEntityTypeId   = Entity.BinaryFileType.StorageEntityTypeId;
                                    Entity.StorageEntitySettings = settingsJson;

                                    Entity.ContentStream = new MemoryStream();
                                    ms.Position          = 0;
                                    ms.CopyTo(Entity.ContentStream);
                                    Entity.ContentStream.Position = 0;
                                    Entity.FileSize = Entity.ContentStream.Length;
                                }
                            }
                        }

                        if (Entity.ContentIsDirty && Entity.StorageProvider != null)
                        {
                            /*
                             * SK - 12/11/2021
                             * Path should always be reset in case when there is any change in Storage Provider from previous value. Otherwise new storage provider may still be refering the older path.
                             */
                            Entity.Path = null;
                            long?fileSize = null;
                            Entity.StorageProvider.SaveContent(Entity, out fileSize);

                            Entity.FileSize = fileSize;
                            Entity.Path     = Entity.StorageProvider.GetPath(Entity);
                        }
                    }
                }

                base.PreSave();
            }
Exemplo n.º 7
0
        private void ScaleAndSavePhotos(PhotoModel model, HttpRequestBase request)
        {
            var file = request.Files[0];
            string extension = Path.GetExtension(file.FileName);
            string filename = file.FileName;

            //Copied Image and add watermark.
            ResizeSettings settings = new ResizeSettings();
            settings.Add("format", "jpg");
            settings.Add("quality", "90");
            settings.Add("carve", "true");
            settings.Add("scale", "down");
            settings.Add("mode", "carve");
            if (model.Watermark)
            {
                settings.Add("watermark", "wm");
            }

            var path = Path.Combine(HttpContext.Current.Server.MapPath(string.Format("~/Albums/{0}", model.AlbumId)));
            if (Directory.Exists(path))
            {
                //thumb is 75x75
                //small is 240x159
                //medium is 500 x 332
                //large is 1024 x 680

                Image img = Image.FromStream(file.InputStream);

                //PropertyItem[] properties = img.PropertyItems;

                //foreach (PropertyItem prop in properties)
                //{

                //}

                bool vert = img.Height > img.Width;

                settings.Add("maxwidth", vert ? "680" : "1024");
                //settings.Add("maxheight", vert ? "1024" : "680");

                Bitmap copy = ImageResizer.ImageBuilder.Current.Build(img, settings, false);
                copy.Save(Path.Combine(path, "Large\\" + filename));

                settings.Remove("maxwidth");
                //settings.Remove("maxheight");
                settings.Remove("watermark");
                settings.Add("maxwidth", vert ? "332" : "500");
                //settings.Add("maxheight", vert ? "500" : "332");
                copy = ImageResizer.ImageBuilder.Current.Build(img, settings, false);
                copy.Save(Path.Combine(path, "Medium\\" + filename));

                settings.Remove("maxwidth");
                //settings.Remove("maxheight");
                settings.Remove("watermark");
                settings.Add("maxwidth", vert ? "200" : "301");
                //settings.Add("maxheight", vert ? "301" : "200");
                copy = ImageResizer.ImageBuilder.Current.Build(img, settings, false);
                copy.Save(Path.Combine(path, "Small\\" + filename));

                settings.Remove("maxwidth");
                settings.Remove("maxwidth");
                settings.Remove("watermark");
                settings.Add("maxwidth", "75");
                settings.Add("maxheight", "75");
                copy = ImageResizer.ImageBuilder.Current.Build(img, settings, false);
                copy.Save(Path.Combine(path, "Thumb\\" + filename));

                file.SaveAs(Path.Combine(path, "Original\\" + filename));
            }
        }
Exemplo n.º 8
0
            protected override void PreSave()
            {
                if (Entry.State == EntityContextState.Deleted)
                {
                    if (Entity.StorageProvider != null)
                    {
                        Entity.BinaryFileTypeId = Entry.OriginalValues[nameof(Entity.BinaryFileTypeId)].ToString().AsInteger();

                        try
                        {
                            Entity.StorageProvider.DeleteContent(Entity);
                        }
                        catch (Exception ex)
                        {
                            // If an exception occurred while trying to delete provider's file, log the exception, but continue with the delete.
                            ExceptionLogService.LogException(ex);
                        }

                        Entity.BinaryFileTypeId = null;
                    }
                }
                else
                {
                    if (Entity.BinaryFileType == null && Entity.BinaryFileTypeId.HasValue)
                    {
                        Entity.BinaryFileType = new BinaryFileTypeService(( RockContext )DbContext).Get(Entity.BinaryFileTypeId.Value);
                    }

                    if (Entity.MimeType.StartsWith("image/"))
                    {
                        try
                        {
                            using (Bitmap bm = new Bitmap(Entity.ContentStream))
                            {
                                if (bm != null)
                                {
                                    Entity.Width  = bm.Width;
                                    Entity.Height = bm.Height;
                                }
                            }
                            Entity.ContentStream.Seek(0, SeekOrigin.Begin);

                            if (!Entity.IsTemporary)
                            {
                                var BinaryFileType = Entity.BinaryFileType;

                                if (BinaryFileType.MaxHeight.HasValue &&
                                    BinaryFileType.MaxHeight != 0 &&
                                    BinaryFileType.MaxWidth.HasValue &&
                                    BinaryFileType.MaxWidth != 0)
                                {
                                    ResizeSettings settings      = new ResizeSettings();
                                    MemoryStream   resizedStream = new MemoryStream();
                                    if (BinaryFileType.MaxWidth.Value < Entity.Width || BinaryFileType.MaxHeight < Entity.Height)
                                    {
                                        settings.Add("mode", "max");
                                        if (BinaryFileType.MaxHeight < Entity.Height && BinaryFileType.MaxWidth < Entity.Width)
                                        {
                                            if (BinaryFileType.MaxHeight >= BinaryFileType.MaxWidth)
                                            {
                                                settings.Add("height", BinaryFileType.MaxHeight.Value.ToString());
                                            }
                                            if (BinaryFileType.MaxHeight <= BinaryFileType.MaxWidth)
                                            {
                                                settings.Add("width", BinaryFileType.MaxWidth.Value.ToString());
                                            }
                                        }
                                        else if (BinaryFileType.MaxHeight < Entity.Height)
                                        {
                                            settings.Add("height", BinaryFileType.MaxHeight.Value.ToString());
                                        }
                                        else
                                        {
                                            settings.Add("width", BinaryFileType.MaxWidth.Value.ToString());
                                        }
                                        ImageBuilder.Current.Build(Entity.ContentStream, resizedStream, settings);
                                        Entity.ContentStream = resizedStream;

                                        using (Bitmap bm = new Bitmap(Entity.ContentStream))
                                        {
                                            if (bm != null)
                                            {
                                                Entity.Width  = bm.Width;
                                                Entity.Height = bm.Height;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception) { }   // if the file is an invalid photo keep moving
                    }

                    if (Entry.State == EntityContextState.Added)
                    {
                        // when a file is saved (unless it is getting Deleted/Saved), it should use the StoredEntityType that is associated with the BinaryFileType
                        if (Entity.BinaryFileType != null)
                        {
                            // Persist the storage type
                            Entity.StorageEntityTypeId = Entity.BinaryFileType.StorageEntityTypeId;

                            // Persist the storage type's settings specific to this binary file type
                            var settings = new Dictionary <string, string>();
                            if (Entity.BinaryFileType.Attributes == null)
                            {
                                Entity.BinaryFileType.LoadAttributes();
                            }
                            foreach (var attributeValue in Entity.BinaryFileType.AttributeValues)
                            {
                                settings.Add(attributeValue.Key, attributeValue.Value.Value);
                            }
                            Entity.StorageEntitySettings = settings.ToJson();

                            if (Entity.StorageProvider != null)
                            {
                                // save the file to the provider's new storage medium, and if the medium returns a filesize, save that value.
                                long?outFileSize = null;
                                Entity.StorageProvider.SaveContent(Entity, out outFileSize);
                                if (outFileSize.HasValue)
                                {
                                    Entity.FileSize = outFileSize;
                                }

                                Entity.Path = Entity.StorageProvider.GetPath(Entity);
                            }
                        }
                    }


                    else if (Entry.State == EntityContextState.Modified)
                    {
                        // when a file is saved (unless it is getting Deleted/Added),
                        // it should use the StorageEntityType that is associated with the BinaryFileType
                        if (Entity.BinaryFileType != null)
                        {
                            // if the storage provider changed, or any of its settings specific
                            // to the binary file type changed, delete the original provider's content
                            if (Entity.StorageEntityTypeId.HasValue && Entity.BinaryFileType.StorageEntityTypeId.HasValue)
                            {
                                var settings = new Dictionary <string, string>();
                                if (Entity.BinaryFileType.Attributes == null)
                                {
                                    Entity.BinaryFileType.LoadAttributes();
                                }
                                foreach (var attributeValue in Entity.BinaryFileType.AttributeValues)
                                {
                                    settings.Add(attributeValue.Key, attributeValue.Value.Value);
                                }
                                string settingsJson = settings.ToJson();

                                if (Entity.StorageProvider != null && (
                                        Entity.StorageEntityTypeId.Value != Entity.BinaryFileType.StorageEntityTypeId.Value ||
                                        Entity.StorageEntitySettings != settingsJson))
                                {
                                    var ms = new MemoryStream();
                                    Entity.ContentStream.Position = 0;
                                    Entity.ContentStream.CopyTo(ms);
                                    Entity.ContentStream.Dispose();

                                    // Delete the current provider's storage
                                    Entity.StorageProvider.DeleteContent(Entity);

                                    // Set the new storage provider with its settings
                                    Entity.StorageEntityTypeId   = Entity.BinaryFileType.StorageEntityTypeId;
                                    Entity.StorageEntitySettings = settingsJson;

                                    Entity.ContentStream = new MemoryStream();
                                    ms.Position          = 0;
                                    ms.CopyTo(Entity.ContentStream);
                                    Entity.ContentStream.Position = 0;
                                    Entity.FileSize = Entity.ContentStream.Length;
                                }
                            }
                        }

                        if (Entity.ContentIsDirty && Entity.StorageProvider != null)
                        {
                            /*
                             * SK - 12/11/2021
                             * Path should always be reset in case when there is any change in Storage Provider from previous value. Otherwise new storage provider may still be refering the older path.
                             */
                            Entity.Path = null;
                            long?fileSize = null;
                            Entity.StorageProvider.SaveContent(Entity, out fileSize);

                            Entity.FileSize = fileSize;
                            Entity.Path     = Entity.StorageProvider.GetPath(Entity);
                        }
                    }
                }

                base.PreSave();
            }