Пример #1
0
        public async Task <string> UpsertAsync(string name, Stream stream, int cacheDuration = 0)
        {
            SqlServerBlobClient.ValidateName(ref name);

            long?existingId = await this.GetBlobIdAsync(name);

            using (var connection = new SqlConnection(this.Options.ConnectionString))
            {
                await connection.OpenAsync();

                using (var transaction = connection.BeginTransaction())
                {
                    using (var command = connection.CreateCommand())
                    {
                        string etag = SqlServerBlobClient.CalculateETag(stream);
                        stream.Seek(0, SeekOrigin.Begin);

                        command.Transaction = transaction;

                        command.CommandText = "UpsertBlobItem";
                        command.CommandType = CommandType.StoredProcedure;

                        command.Parameters.AddRange(new[] {
                            new SqlParameter("@id", SqlDbType.BigInt)
                            {
                                Value = existingId ?? 0
                            },
                            new SqlParameter("@containerName", SqlDbType.NVarChar, 1024)
                            {
                                Value = this.ContainerName
                            },
                            new SqlParameter("@name", SqlDbType.NVarChar, 1024)
                            {
                                Value = name
                            },
                            new SqlParameter("@data", SqlDbType.VarBinary, -1)
                            {
                                Value = stream
                            },
                            new SqlParameter("@length", SqlDbType.BigInt)
                            {
                                Value = stream.Length
                            },
                            new SqlParameter("@etag", SqlDbType.VarChar, 100)
                            {
                                Value = etag
                            }
                        });

                        await command.ExecuteNonQueryAsync();
                    }

                    await transaction.CommitAsync();
                }
            }

            return(name);
        }
Пример #2
0
        private async ValueTask <long?> GetBlobIdAsync(string name)
        {
            SqlServerBlobClient.ValidateName(ref name);
            long id = await this.DbContext.Blobs.Where(b => b.ContainerName == this.ContainerName && b.Name == name).Select(b => b.Id).FirstOrDefaultAsync();

            if (0 == id)
            {
                return(null);
            }
            return(id);
        }
Пример #3
0
        public async Task <Stream> OpenReadStreamAsync(string name)
        {
            SqlServerBlobClient.ValidateName(ref name);
            var id = await this.GetBlobIdAsync(name);

            if (!id.HasValue)
            {
                return(SqlServerStream.Empty);
            }

            DbConnection connection = new SqlConnection(this.Options.ConnectionString);
            DbCommand    command    = null;
            DbDataReader reader     = null;

            try
            {
                if (ConnectionState.Closed == connection.State)
                {
                    await connection.OpenAsync();
                }

                command             = connection.CreateCommand();
                command.CommandText = $"SELECT [Content] FROM BlobItems WHERE Id = {id.Value}";

                reader = await command.ExecuteReaderAsync();

                if (!await reader.ReadAsync())
                {
                    return(SqlServerStream.Empty);
                }
                else
                {
                    return(new SqlServerStream(reader.GetStream(0), reader, command, connection));
                }
            }
            catch
            {
                if (null != reader)
                {
                    await reader.DisposeAsync();
                }
                if (null != command)
                {
                    await command.DisposeAsync();
                }
                if (null != connection)
                {
                    await connection.DisposeAsync();
                }
                throw;
            }
        }
Пример #4
0
        public async ValueTask DeleteAsync(string name)
        {
            SqlServerBlobClient.ValidateName(ref name);

            using (var transaction = await this.DbContext.Database.BeginTransactionAsync())
            {
                long?id = await this.GetBlobIdAsync(name);

                if (!id.HasValue)
                {
                    return;
                }

                await this.DbContext.Database.ExecuteSqlInterpolatedAsync($"DELETE BlobItems WHERE Id = {id.Value}");

                await transaction.CommitAsync();
            }
        }
Пример #5
0
        public async Task <ReadOnlyMemory <byte> > ReadAllBytesAsync(string name)
        {
            SqlServerBlobClient.ValidateName(ref name);
            var id = await this.GetBlobIdAsync(name);

            if (!id.HasValue)
            {
                return(new ReadOnlyMemory <byte>(Array.Empty <byte>()));
            }

            var entity = await this.DbContext.Blobs.FindAsync(id);

            if (null == entity)
            {
                return(new ReadOnlyMemory <byte>(Array.Empty <byte>()));
            }
            return(entity.Content);
        }
Пример #6
0
        public async Task <BlobItemProperties> GetPropertiesAsync(string name)
        {
            SqlServerBlobClient.ValidateName(ref name);
            var id = await this.GetBlobIdAsync(name);

            if (!id.HasValue)
            {
                return(BlobItemProperties.NotExists);
            }

            var blobEntity = await this.DbContext.Blobs.FindAsync(id.Value);

            if (null == blobEntity)
            {
                return(BlobItemProperties.NotExists);
            }

            return(new BlobItemProperties(blobEntity.Length, blobEntity.ETag, blobEntity.LastModifiedUtc));
        }
Пример #7
0
 public async ValueTask <bool> ExistsAsync(string name)
 {
     SqlServerBlobClient.ValidateName(ref name);
     return((await this.GetBlobIdAsync(name)).HasValue);
 }