public async Task <BaseResponseObject <ContentMetadataOutput> > CreateContentMetadataAsync(string secretId, ContentMetadataCreationInput input) => await this.ProcessResponseAsync <ContentMetadataOutput>(async() => { return(await client.PostAsJsonAsync($"{ApiVersion}/secret/{secretId}/content", input)); });
public async Task UploadAttachmentsAsync(string secretId, List <AttachmentModel> attachments) { foreach (var attachment in attachments) { try { attachment.Status = UploadStatus.InProgress; attachment.ProgressPercents = 0.0f; var contentInput = new ContentMetadataCreationInput() { ContentType = String.IsNullOrWhiteSpace(attachment.SourceFile.ContentType) ? "application/octet-stream" : attachment.SourceFile.ContentType, FileName = attachment.SourceFile.Name }; var createdContent = await this.CreateContentMetadataAsync(secretId, contentInput); if (!"ok".Equals(createdContent.Status) || createdContent.Result == null) { attachment.Status = UploadStatus.Error; attachment.Error = $"'Attachment {attachment.SourceFile.Name}' creation failed."; continue; // no-op, skip to next attachment } var content = new ContentMetadata(createdContent.Result); var dataStream = attachment.SourceFile.OpenReadStream(Constants.MaxAttachmentDataLength); var chunkLengths = GetChunkLengths(attachment.SourceFile.Size, Constants.MaxChunkDataLength); var accessTicket = string.Empty; for (int chunkIndex = 0; chunkIndex < chunkLengths.Count; chunkIndex++) { var isInterim = chunkIndex < (chunkLengths.Count - 1); var secretData = await this.PutSecretDataStreamAsync( secretId, content.ContentName, dataStream, isInterim, chunkLengths[chunkIndex], accessTicket); if (!"ok".Equals(secretData.Status) || secretData.Result == null) { attachment.Status = UploadStatus.Error; attachment.Error = $"'Attachment {attachment.SourceFile.Name}' upload failed."; break; } accessTicket = secretData.Result.AccessTicket; attachment.ProgressPercents += 100.0f * ((float)chunkLengths[chunkIndex] / attachment.SourceFile.Size); } if (attachment.Status == UploadStatus.Error) { continue; // no-op, skip to next attachment } attachment.Status = UploadStatus.Success; attachment.ProgressPercents = 100.0f; } catch (Exception exception) { attachment.Status = UploadStatus.Error; attachment.Error = $"{exception.GetType()}: {exception.Message}"; } } }