public async Task Put() { using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context)) { var id = GetQueryStringValueAndAssertIfSingleAndNotEmpty("id"); var name = GetQueryStringValueAndAssertIfSingleAndNotEmpty("name"); var contentType = GetStringQueryString("contentType", false) ?? ""; AttachmentDetails result; using (var streamsTempFile = Database.DocumentsStorage.AttachmentsStorage.GetTempFile("put")) using (var stream = streamsTempFile.StartNewStream()) { Stream requestBodyStream = RequestBodyStream(); string hash; try { hash = await AttachmentsStorageHelper.CopyStreamToFileAndCalculateHash(context, requestBodyStream, stream, Database.DatabaseShutdown); } catch (Exception) { try { // if we failed to read the entire request body stream, we might leave // data in the pipe still, this will cause us to read and discard the // rest of the attachment stream and return the actual error to the caller requestBodyStream.CopyTo(Stream.Null); } catch (Exception) { // we tried, but we can't clean the request, so let's just kill // the connection HttpContext.Abort(); } throw; } var changeVector = context.GetLazyString(GetStringFromHeaders("If-Match")); var cmd = new MergedPutAttachmentCommand { Database = Database, ExpectedChangeVector = changeVector, DocumentId = id, Name = name, Stream = stream, Hash = hash, ContentType = contentType }; stream.Flush(); await Database.TxMerger.Enqueue(cmd); cmd.ExceptionDispatchInfo?.Throw(); result = cmd.Result; } HttpContext.Response.StatusCode = (int)HttpStatusCode.Created; using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream())) { writer.WriteStartObject(); writer.WritePropertyName(nameof(AttachmentDetails.ChangeVector)); writer.WriteString(result.ChangeVector); writer.WriteComma(); writer.WritePropertyName(nameof(AttachmentDetails.Name)); writer.WriteString(result.Name); writer.WriteComma(); writer.WritePropertyName(nameof(AttachmentDetails.DocumentId)); writer.WriteString(result.DocumentId); writer.WriteComma(); writer.WritePropertyName(nameof(AttachmentDetails.ContentType)); writer.WriteString(result.ContentType); writer.WriteComma(); writer.WritePropertyName(nameof(AttachmentDetails.Hash)); writer.WriteString(result.Hash); writer.WriteComma(); writer.WritePropertyName(nameof(AttachmentDetails.Size)); writer.WriteInteger(result.Size); writer.WriteEndObject(); } } }
public async Task Put() { using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context)) { var id = GetQueryStringValueAndAssertIfSingleAndNotEmpty("id"); var name = GetQueryStringValueAndAssertIfSingleAndNotEmpty("name"); var contentType = GetStringQueryString("contentType", false) ?? ""; AttachmentDetails result; using (var streamsTempFile = Database.DocumentsStorage.AttachmentsStorage.GetTempFile("put")) using (var stream = streamsTempFile.StartNewStream()) { var hash = await AttachmentsStorageHelper.CopyStreamToFileAndCalculateHash(context, RequestBodyStream(), stream, Database.DatabaseShutdown); var changeVector = context.GetLazyString(GetStringQueryString("If-Match", false)); var cmd = new MergedPutAttachmentCommand { Database = Database, ExpectedChangeVector = changeVector, DocumentId = id, Name = name, Stream = stream, Hash = hash, ContentType = contentType }; await Database.TxMerger.Enqueue(cmd); cmd.ExceptionDispatchInfo?.Throw(); result = cmd.Result; } HttpContext.Response.StatusCode = (int)HttpStatusCode.Created; using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream())) { writer.WriteStartObject(); writer.WritePropertyName(nameof(AttachmentDetails.ChangeVector)); writer.WriteString(result.ChangeVector); writer.WriteComma(); writer.WritePropertyName(nameof(AttachmentDetails.Name)); writer.WriteString(result.Name); writer.WriteComma(); writer.WritePropertyName(nameof(AttachmentDetails.DocumentId)); writer.WriteString(result.DocumentId); writer.WriteComma(); writer.WritePropertyName(nameof(AttachmentDetails.ContentType)); writer.WriteString(result.ContentType); writer.WriteComma(); writer.WritePropertyName(nameof(AttachmentDetails.Hash)); writer.WriteString(result.Hash); writer.WriteComma(); writer.WritePropertyName(nameof(AttachmentDetails.Size)); writer.WriteInteger(result.Size); writer.WriteEndObject(); } } }