Exemple #1
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);
        }
Exemple #2
0
        /// <summary>
        /// Pres the save.
        /// </summary>
        /// <param name="dbContext">The database context.</param>
        /// <param name="entry">The entry.</param>
        public override void PreSaveChanges(DbContext dbContext, System.Data.Entity.Infrastructure.DbEntityEntry entry)
        {
            if (entry.State == System.Data.Entity.EntityState.Deleted)
            {
                if (StorageProvider != null)
                {
                    this.BinaryFileTypeId = entry.OriginalValues["BinaryFileTypeId"].ToString().AsInteger();
                    StorageProvider.DeleteContent(this);
                    this.BinaryFileTypeId = null;
                }
            }
            else
            {
                if (BinaryFileType == null && BinaryFileTypeId.HasValue)
                {
                    BinaryFileType = new BinaryFileTypeService((RockContext)dbContext).Get(BinaryFileTypeId.Value);
                }

                if (entry.State == System.Data.Entity.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 == System.Data.Entity.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))
                            {
                                // Save the file contents before deleting
                                var contentStream = ContentStream;

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

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

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

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

            base.PreSaveChanges(dbContext, entry);
        }