70 lines
1.8 KiB
C#
70 lines
1.8 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
|
|
using FrostFS.Object;
|
|
using FrostFS.SDK.Client.Interfaces;
|
|
|
|
using Google.Protobuf;
|
|
|
|
namespace FrostFS.SDK.Client
|
|
{
|
|
internal sealed class ObjectWriter : IObjectWriter
|
|
{
|
|
private readonly ClientContext ctx;
|
|
private readonly PrmObjectPutBase args;
|
|
private readonly ObjectStreamer<PutRequest, PutResponse> streamer;
|
|
private bool disposedValue;
|
|
|
|
internal ObjectWriter(ClientContext ctx, PrmObjectPutBase args, ObjectStreamer<PutRequest, PutResponse> streamer)
|
|
{
|
|
this.ctx = ctx;
|
|
this.args = args;
|
|
this.streamer = streamer;
|
|
}
|
|
|
|
public async Task WriteAsync(ReadOnlyMemory<byte> memory)
|
|
{
|
|
var chunkRequest = new PutRequest
|
|
{
|
|
Body = new PutRequest.Types.Body
|
|
{
|
|
Chunk = UnsafeByteOperations.UnsafeWrap(memory)
|
|
}
|
|
};
|
|
|
|
chunkRequest.AddMetaHeader(args.XHeaders);
|
|
|
|
chunkRequest.Sign(this.ctx.Key);
|
|
|
|
await streamer.Write(chunkRequest).ConfigureAwait(false);
|
|
}
|
|
|
|
public async Task<FrostFsObjectId> CompleteAsync()
|
|
{
|
|
var response = await streamer.Close().ConfigureAwait(false);
|
|
|
|
Verifier.CheckResponse(response);
|
|
|
|
return FrostFsObjectId.FromHash(response.Body.ObjectId.Value.Span);
|
|
}
|
|
|
|
private void Dispose(bool disposing)
|
|
{
|
|
if (!disposedValue)
|
|
{
|
|
if (disposing)
|
|
{
|
|
streamer.Dispose();
|
|
}
|
|
|
|
disposedValue = true;
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Dispose(disposing: true);
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
}
|
|
}
|