public async Task StoreAsync(string id, string name, Stream stream, string contentType = null, CancellationToken token = default) { PutAttachmentCommandHelper.ValidateStream(stream); using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(token, _token); using (_operation.ConcurrencyCheck()) { _operation.EndPreviousCommandIfNeeded(); await _operation.ExecuteBeforeStore().ConfigureAwait(false); try { if (_operation._first == false) { _operation.WriteComma(); } _operation._currentWriter.Write("{\"Id\":\""); _operation.WriteString(id); _operation._currentWriter.Write("\",\"Type\":\"AttachmentPUT\",\"Name\":\""); _operation.WriteString(name); if (contentType != null) { _operation._currentWriter.Write("\",\"ContentType\":\""); _operation.WriteString(contentType); } _operation._currentWriter.Write("\",\"ContentLength\":"); _operation._currentWriter.Write(stream.Length); _operation._currentWriter.Write('}'); await _operation.FlushIfNeeded().ConfigureAwait(false); PutAttachmentCommandHelper.PrepareStream(stream); // pass the default value for bufferSize to make it compile on netstandard2.0 await stream.CopyToAsync(_operation._currentWriter.BaseStream, bufferSize : 16 * 1024, cancellationToken : linkedCts.Token).ConfigureAwait(false); await _operation.FlushIfNeeded().ConfigureAwait(false); } catch (Exception e) { await _operation.HandleErrors(id, e).ConfigureAwait(false); } } }
public override HttpRequestMessage CreateRequest(JsonOperationContext ctx, ServerNode node, out string url) { PutAttachmentCommandHelper.PrepareStream(_stream); url = $"{node.Url}/databases/{node.Database}/attachments?id={Uri.EscapeDataString(_documentId)}&name={Uri.EscapeDataString(_name)}"; if (string.IsNullOrWhiteSpace(_contentType) == false) { url += $"&contentType={Uri.EscapeDataString(_contentType)}"; } var request = new HttpRequestMessage { Method = HttpMethods.Put, Content = new AttachmentStreamContent(_stream, CancellationToken) }; AddChangeVectorIfNotNull(_changeVector, request); return(request); }