Example #1
0
        public DbPartialWriter(IDataContext dataContext, FileContent content, int partSize = 1024)
        {
            _dataContext = dataContext;
            _content     = content;
            _partSize    = partSize;

            _currentPart = _dataContext.Set <FileContentPart>()
                           .AsNoTracking()
                           .Where(x => x.ContentId == _content.Id)
                           .OrderByDescending(x => x.Order)
                           .FirstOrDefault();

            if (_currentPart == null)
            {
                _currentPart = new FileContentPart()
                {
                    Content = _content,
                    Data    = new FileContentPartData()
                    {
                        Bytes = new byte[_partSize]
                    },
                    PositionInFile = 0,
                    Order          = 0,
                    Length         = 0,
                };

                _dataContext.Set <FileContentPart>().Add(_currentPart);
            }

            _currentPartPosition = (int)(_content.CurrentSize - _currentPart.PositionInFile);
        }
Example #2
0
        /// <summary>
        /// Получает <see cref="DbDataReader"/> для указанной части
        /// </summary>
        protected virtual DbDataReader GetReaderForPart(FileContentPart part)
        {
            var connection = Context.Database.GetDbConnection();

            var command = connection.CreateCommand();

            command.CommandText = @"SELECT Data FROM ""FileContentParts"" WHERE ""Id""=@id";

            var id = command.CreateParameter();

            id.ParameterName = "@id";
            id.DbType        = DbType.Guid;
            id.Value         = part.Id;

            command.Parameters.Add(id);

            var closedConnection = !connection.State.HasFlag(ConnectionState.Open);

            if (closedConnection)
            {
                connection.Open();
            }

            try
            {
                var parameters =
                    CommandBehavior.SequentialAccess |
                    CommandBehavior.SingleResult |
                    CommandBehavior.SingleRow;

                if (closedConnection)
                {
                    parameters |= CommandBehavior.CloseConnection;
                }

                var reader = command.ExecuteReader(parameters);

                if (!reader.Read())
                {
                    return(null);
                }

                closedConnection = false;

                return(reader);
            }
            finally
            {
                if (closedConnection)
                {
                    connection.Close();
                }
            }
        }
Example #3
0
        public void Seek(long streamOffset)
        {
            _currentPart = _dataContext.Set <FileContentPart>()
                           .AsNoTracking()
                           .FirstOrDefault(
                x => x.ContentId == _content.Id &&
                x.PositionInFile <= streamOffset &&
                streamOffset < x.PositionInFile + x.Length);

            if (_currentPart == null)
            {
                throw new InvalidOperationException();
            }

            _currentPartPosition = (int)(streamOffset - _currentPart.PositionInFile);
        }
Example #4
0
        public bool Next()
        {
            if (_content.CurrentSize == _content.MaxSize)
            {
                return(false);
            }

            var newPart = new FileContentPart()
            {
                Content = _content,
                Data    = new FileContentPartData()
                {
                    Bytes = new byte[_partSize]
                },
                PositionInFile = _currentPart.PositionInFile + _currentPart.Length + 1,
                Order          = _currentPart.Order + 1,
                Length         = 0
            };

            _dataContext.Set <FileContentPart>().Add(newPart);

            return(true);
        }
Example #5
0
 /// <summary>
 /// Делает текущей частью указанный <see cref="FileContentPart"/>
 /// </summary>
 private void SetCurrentPart(FileContentPart part)
 {
     mCurrentPart = part;
     CreateReaderForCurrentPart();
 }
Example #6
0
 public void Dispose()
 {
     _dataContext = null;
     _content     = null;
     _currentPart = null;
 }