示例#1
0
        private Stream ReplaceContentStreamWithMemoryStream()
        {
            if (ContentStream is DurableMemoryStream)
            {
                return(ContentStream);
            }

            var buffer = new byte[4096];
            var stream = new MemoryStream();
            var count  = 0;

            do
            {
                if (ContentStream == null)
                {
                    continue;
                }
                count = ContentStream.Read(buffer, 0, buffer.Length);
                stream.Write(buffer, 0, count);
            } while (count != 0);

            if (ContentStream != null)
            {
                ContentStream.Close();
                ContentStream.Dispose();
            }

            if (stream.CanSeek)
            {
                stream.Position = 0;
            }

            return(new DurableMemoryStream(stream));
        }
示例#2
0
 public void Dispose()
 {
     if (ContentStream != null)
     {
         ContentStream.Dispose();
     }
 }
示例#3
0
 /// <summary>
 /// Releases the unmanaged resources used by the Attachment and
 /// optionally releases the managed resources.
 /// </summary>
 /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
 protected virtual void Dispose(bool disposing)
 {
     if (disposing && !_isDisposed)
     {
         _isDisposed = true;
         ContentStream.Dispose();
     }
 }
示例#4
0
 public void Empty()
 {
     if (ContentStream != null)
     {
         ContentStream.Dispose();
         ContentStream = null;
     }
 }
示例#5
0
 /// <summary>
 /// The Dispose.
 /// </summary>
 public void Dispose()
 {
     if (ContentStream != null)
     {
         ContentStream.Close();
         ContentStream.Dispose();
         ContentStream = null;
     }
     GC.SuppressFinalize(this);
 }
示例#6
0
        private void Close()
        {
            ContentStream?.Dispose();
            ContentStream = null;

            // kui path on teada, siis on temp fail ja see tuleb kustutada ..
            if (!string.IsNullOrEmpty(contentPath) && File.Exists(contentPath))
            {
                File.Delete(contentPath);
            }
        }
示例#7
0
 internal void DisposeContentStreamIfNotBuffered()
 {
     // We want to keep the ContentStream readable
     // even after the response is disposed but only if it's a
     // buffered memory stream otherwise we can leave a network
     // connection hanging open
     if (ContentStream is not MemoryStream)
     {
         ContentStream?.Dispose();
     }
 }
示例#8
0
 protected override void Dispose(bool aDisposing)
 {
     if (aDisposing)
     {
         if (ContentStream != null)
         {
             ContentStream.Dispose();
         }
         ContentStream = null;
     }
     base.Dispose(aDisposing);
 }
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    ContentStream.Close();
                    ContentStream.Dispose();
                    ContentStream = null;
                }

                disposedValue = true;
            }
        }
示例#10
0
        /// <summary>
        /// Clean up unmanaged resources allocated by the X-Road message.
        /// </summary>
        public void Dispose()
        {
            if (ContentStream != null)
            {
                ContentStream.Dispose();
                ContentStream = null;
            }

            foreach (var attachment in attachments)
            {
                attachment.Dispose();
            }

            attachments.Clear();
        }
示例#11
0
        /// <summary>
        /// The Content.
        /// </summary>
        /// <param name="buff">The buff<see cref="byte[]"/>.</param>
        /// <param name="contentType">The contentType<see cref="string"/>.</param>
        public void Content(byte[] buff, string contentType = null)
        {
            if (!string.IsNullOrEmpty(contentType))
            {
                MimeType = contentType;
            }

            Headers.Set("Content-Type", MimeType);

            if (ContentStream != null)
            {
                ContentStream.Dispose();
                ContentStream = null;
            }

            ContentStream = new MemoryStream(buff);

            HttpStatus = System.Net.HttpStatusCode.OK;
        }
示例#12
0
 public void Dispose()
 {
     GC.SuppressFinalize(this);
     ContentStream?.Dispose();
 }
示例#13
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);
        }
示例#14
0
 public void Dispose()
 {
     HeaderStream?.Dispose();
     ContentStream?.Dispose();
 }
示例#15
0
 public override void Dispose()
 {
     ContentStream?.Dispose();
 }
示例#16
0
文件: BinaryFile.cs 项目: sjison/Rock
        /// <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))
                            {
                                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);
        }
 public void Dispose()
 {
     Client?.Dispose();
     ContentStream?.Dispose();
 }
示例#18
0
 public void Dispose()
 {
     ContentStream.Dispose();
 }