public async Task Attachment() { using (var client = new CouchClient("http://*****:*****@"{runningPath}\Assets\luke.txt", MediaTypeNames.Text.Plain); luke = await rebels.CreateAsync(luke).ConfigureAwait(false); Assert.Equal("Luke", luke.Name); Assert.NotEmpty(luke.Attachments); CouchAttachment attachment = luke.Attachments.First(); Assert.NotNull(attachment); Assert.NotNull(attachment.Uri); // Download var downloadFilePath = await rebels.DownloadAttachment(attachment, $@"{runningPath}\Assets", "luke-downloaded.txt"); Assert.True(File.Exists(downloadFilePath)); File.Delete(downloadFilePath); // Find luke = await rebels.FindAsync(luke.Id).ConfigureAwait(false); Assert.Equal(19, luke.Age); attachment = luke.Attachments.First(); Assert.NotNull(attachment); Assert.NotNull(attachment.Uri); Assert.NotNull(attachment.Digest); Assert.NotNull(attachment.Length); // Update luke.Surname = "Skywalker"; luke = await rebels.CreateOrUpdateAsync(luke).ConfigureAwait(false); Assert.Equal("Skywalker", luke.Surname); await client.DeleteDatabaseAsync <Rebel>().ConfigureAwait(false); } }
/// <summary> /// Asynchronously downloads a specific attachment. /// </summary> /// <param name="attachment">The attachment to download.</param> /// <param name="localFolderPath">Path of local folder where file is to be downloaded.</param> /// <param name="localFileName">Name of local file. If not specified, the source filename (from Content-Dispostion header, or last segment of the URL) is used.</param> /// <param name="bufferSize">Buffer size in bytes. Default is 4096.</param> /// <returns>The path of the downloaded file.</returns> public async Task <string> DownloadAttachment(CouchAttachment attachment, string localFolderPath, string localFileName = null, int bufferSize = 4096) { if (attachment.Uri == null) { throw new InvalidOperationException("The attachment is not uploaded yet."); } return(await NewRequest() .AppendPathSegment(attachment.DocumentId) .AppendPathSegment(Uri.EscapeUriString(attachment.Name)) .WithHeader("If-Match", attachment.DocumentRev) .DownloadFileAsync(localFolderPath, localFileName, bufferSize) .ConfigureAwait(false)); }
public async Task Attachment() { var luke = new Rebel { Name = "Luke", Age = 19 }; var runningPath = Directory.GetCurrentDirectory(); // Create luke.Attachments.AddOrUpdate($@"{runningPath}\Assets\luke.txt", MediaTypeNames.Text.Plain); luke = await _rebels.AddAsync(luke); Assert.Equal("Luke", luke.Name); Assert.NotEmpty(luke.Attachments); CouchAttachment attachment = luke.Attachments.First(); Assert.NotNull(attachment); Assert.NotNull(attachment.Uri); // Download var downloadFilePath = await _rebels.DownloadAttachmentAsync(attachment, $@"{runningPath}\Assets", "luke-downloaded.txt"); Assert.True(File.Exists(downloadFilePath)); File.Delete(downloadFilePath); // Find luke = await _rebels.FindAsync(luke.Id); Assert.Equal(19, luke.Age); attachment = luke.Attachments.First(); Assert.NotNull(attachment); Assert.NotNull(attachment.Uri); Assert.NotNull(attachment.Digest); Assert.NotNull(attachment.Length); // Update luke.Surname = "Skywalker"; luke = await _rebels.AddOrUpdateAsync(luke); Assert.Equal("Skywalker", luke.Surname); }