Beispiel #1
0
        /// <summary>
        /// DO NOT USE DIRECTLY THIS METHOD FROM YOUR CODE.
        /// Updates the stream in the appropriate row of the Files table specified by the context.
        /// </summary>
        public void UpdateStream(BlobStorageContext context, Stream stream)
        {
            // We have to work with an integer since SQL does not support
            // binary values bigger than [Int32.MaxValue].
            var bufferSize = Convert.ToInt32(stream.Length);

            var buffer = new byte[bufferSize];

            if (bufferSize > 0)
            {
                // Read bytes from the source
                stream.Seek(0, SeekOrigin.Begin);
                stream.Read(buffer, 0, bufferSize);
            }

            //UNDONE: [DIREF] get connection string through constructor
            using (var ctx = new MsSqlDataContext(ConnectionStrings.ConnectionString, DataOptions, CancellationToken.None))
            {
                ctx.ExecuteNonQueryAsync(WriteStreamScript, cmd =>
                {
                    cmd.Parameters.AddRange(new[]
                    {
                        ctx.CreateParameter("@Id", SqlDbType.Int, context.FileId),
                        ctx.CreateParameter("@Value", SqlDbType.VarBinary, bufferSize, buffer),
                    });
                }).GetAwaiter().GetResult();
            }
        }
 /// <inheritdoc />
 public async Task WriteAsync(BlobStorageContext context, long offset, byte[] buffer, CancellationToken cancellationToken)
 {
     using (var ctx = new MsSqlDataContext(_connectionString, DataOptions, cancellationToken))
     {
         await ctx.ExecuteNonQueryAsync(UpdateStreamWriteChunkScript, cmd =>
         {
             cmd.Parameters.AddRange(new[]
             {
                 ctx.CreateParameter("@FileId", SqlDbType.Int, context.FileId),
                 ctx.CreateParameter("@VersionId", SqlDbType.Int, context.VersionId),
                 ctx.CreateParameter("@PropertyTypeId", SqlDbType.Int, context.PropertyTypeId),
                 ctx.CreateParameter("@Data", SqlDbType.VarBinary, buffer),
                 ctx.CreateParameter("@Offset", SqlDbType.BigInt, offset),
             });
         }).ConfigureAwait(false);
     }
 }
Beispiel #3
0
        private async Task CleanupFilesSetDeleteFlagAsync(string script, CancellationToken cancellationToken)
        {
            using (var ctx = new MsSqlDataContext(ConnectionStrings.ConnectionString, DataOptions, cancellationToken))
            {
                using (var transaction = ctx.BeginTransaction())
                {
                    try
                    {
                        await ctx.ExecuteNonQueryAsync(script).ConfigureAwait(false);

                        transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        throw new DataException("Error during setting deleted flag on files.", e);
                    }
                }
            }
        }
Beispiel #4
0
        public async Task CommitChunkAsync(int versionId, int propertyTypeId, int fileId, long fullSize, BinaryDataValue source,
                                           CancellationToken cancellationToken)
        {
            try
            {
                using (var ctx = new MsSqlDataContext(ConnectionStrings.ConnectionString, DataOptions, cancellationToken))
                {
                    using (var transaction = ctx.BeginTransaction())
                    {
                        await ctx.ExecuteNonQueryAsync(CommitChunkScript, cmd =>
                        {
                            cmd.Parameters.AddRange(new[]
                            {
                                ctx.CreateParameter("@FileId", DbType.Int32, fileId),
                                ctx.CreateParameter("@VersionId", DbType.Int32, versionId),
                                ctx.CreateParameter("@PropertyTypeId", DbType.Int32, propertyTypeId),
                                ctx.CreateParameter("@Size", DbType.Int64, fullSize),
                                ctx.CreateParameter("@Checksum", DbType.AnsiString, 200, DBNull.Value),
                                ctx.CreateParameter("@ContentType", DbType.String, 50, source != null ? source.ContentType : string.Empty),
                                ctx.CreateParameter("@FileNameWithoutExtension", DbType.String, 450, source != null
                                    ? source.FileName.FileNameWithoutExtension == null
                                        ? DBNull.Value
                                        : (object)source.FileName.FileNameWithoutExtension
                                    : DBNull.Value),

                                ctx.CreateParameter("@Extension", DbType.String, 50,
                                                    source != null ? ValidateExtension(source.FileName.Extension) : string.Empty),
                            });
                        }).ConfigureAwait(false);

                        transaction.Commit();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new DataException("Error during committing binary chunk to file stream.", ex);
            }
        }
Beispiel #5
0
        public static async Task UpdateStreamAsync(BlobStorageContext context, Stream stream, MsSqlDataContext dataContext)
        {
            // We have to work with an integer since SQL does not support
            // binary values bigger than [Int32.MaxValue].
            var bufferSize = Convert.ToInt32(stream.Length);

            var buffer = new byte[bufferSize];

            if (bufferSize > 0)
            {
                // Read bytes from the source
                stream.Seek(0, SeekOrigin.Begin);
                stream.Read(buffer, 0, bufferSize);
            }

            await dataContext.ExecuteNonQueryAsync(WriteStreamScript, cmd =>
            {
                cmd.Parameters.AddRange(new[]
                {
                    dataContext.CreateParameter("@Id", SqlDbType.Int, context.FileId),
                    dataContext.CreateParameter("@Value", SqlDbType.VarBinary, bufferSize, buffer),
                });
            }).ConfigureAwait(false);
        }