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 streamer; private bool disposedValue; internal ObjectWriter(ClientContext ctx, PrmObjectPutBase args, ObjectStreamer streamer) { this.ctx = ctx; this.args = args; this.streamer = streamer; } public async Task WriteAsync(ReadOnlyMemory 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 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); } } }