public async Task UploadDirectMultipartTest(string path, int bufferSize = DefaultBufferSize) { var record = new FileRecord(); using (var recordContent = new ObjectContent <FileRecord>(record, new JsonMediaTypeFormatter())) using (var fileStream = File.OpenRead(path)) using (var progressStream = new ProgressStream(fileStream, Progress, bufferSize)) using (var streamContent = new StreamContent(progressStream, bufferSize)) { streamContent.Headers.ContentType = new MediaTypeHeaderValue(MediaTypeNames.Application.Octet); streamContent.Headers.ContentDisposition = new FormDataContentDispositionHeaderValue(); var content = new MultipartContent { recordContent, streamContent }; var response = await Client.PostAsync( $"{Constants.FilesRoute}/{Constants.UploadMultipartDirect}", content); Assert.True(response.IsSuccessStatusCode); } }
public async Task UploadChunkTest(string path, string controller = Constants.UploadChunk, int bufferSize = DefaultBufferSize) { var transactionId = Guid.NewGuid(); var chunkId = 0; using (var stream = File.OpenRead(path)) using (var bufferedStream = new ProgressStream(stream, Progress, bufferSize)) { int length; var buffer = new byte[bufferSize]; while ((length = await bufferedStream.ReadAsync(buffer, 0, bufferSize)) > 0) { var url = $"{Constants.FilesRoute}/{controller}/{transactionId}"; var last = stream.Position >= stream.Length; if (controller == Constants.UploadChunk) { url += $"/{chunkId++}/{last}"; } var result = await Client.PostAsync( url, new ByteArrayContent(buffer, 0, length)); Assert.True(result.IsSuccessStatusCode); if (last && controller == Constants.UploadChunk) { var resultLength = await result.Content.ReadAsAsync <long>(); Assert.Equal(stream.Length, resultLength); } } } }
public async Task UploadFile(string path) { using (var fileStream = File.OpenRead(path)) { var record = new FileRecord { FileId = Guid.NewGuid(), Size = fileStream.Length }; decimal completed = default; var progress = new Progress <decimal>(d => completed = d); var wrapper = new ProgressStream(fileStream, progress); var multipart = new MultipartContent { new ObjectContent <FileRecord>(record, new JsonMediaTypeFormatter()), new StreamContent(wrapper) }; var response = await Client.PostAsync($"{Constants.FilesRoute}/{Constants.LargeFile}/{wrapper.BufferSize}", multipart); Assert.True(completed == 1); Assert.True(response.IsSuccessStatusCode, await response.Content.ReadAsStringAsync()); } }
public async Task UploadDirectStreamTest(string path, string controller = Constants.UploadDirect, int bufferSize = DefaultBufferSize) { using (var file = File.OpenRead(path)) using (var stream = new ProgressStream(file, Progress, bufferSize)) using (var streamContent = new StreamContent(stream, bufferSize)) { streamContent.Headers.ContentType = new MediaTypeHeaderValue(MediaTypeNames.Application.Octet); streamContent.Headers.ContentDisposition = new FormDataContentDispositionHeaderValue(); var response = await Client.PostAsync( $"{Constants.FilesRoute}/{controller}", streamContent); Assert.True(response.IsSuccessStatusCode); } }
public async Task UploadByChunks(string path) { var current = 0; var bufferSize = 1024 * 1000 * 16; //16MB chunks var notifications = 0; var progress = new Progress <decimal>(d => notifications++); using (var stream = File.OpenRead(path)) using (var bufferedStream = new ProgressStream(stream, progress, bufferSize)) { int length; var buffer = new byte[bufferSize]; while ((length = await bufferedStream.ReadAsync(buffer, 0, bufferSize)) > 0) { var result = await Client.PostAsync($"{Constants.FilesRoute}/{Constants.HugeFile}/{current}", new ByteArrayContent(buffer, 0, length)); Assert.True(result.IsSuccessStatusCode); } } }
public async Task UploadMultipartOriginal(string path, int bufferSize = DefaultBufferSize) { var record = new FileRecord(); using (var fileStream = File.OpenRead(path)) using (var bufferedStream = new ProgressStream(fileStream, Progress, bufferSize)) { using (var multipart = new MultipartContent()) using (var fileContent = new StreamContent(bufferedStream, bufferSize)) { multipart.Add(fileContent); var contentDispositionHeader = new ContentDispositionHeaderValue("attachment") { CreationDate = DateTime.Now, FileName = Path.GetFileName(path), Size = fileStream.Length }; fileContent.Headers.ContentDisposition = contentDispositionHeader; var response = await Client.PostAsync($"{Constants.FilesRoute}/{Constants.UploadMultipartOriginal}", multipart); } } }
public async Task UploadPushTest(string path, int bufferSize = DefaultBufferSize) { using (var fileStream = File.OpenRead(path)) using (var bufferedStream = new ProgressStream(fileStream, Progress, bufferSize)) { var psContent = new PushStreamContent(async(stream, content, context) => { try { await bufferedStream.CopyToAsync(stream, bufferSize).ConfigureAwait(false); } finally { stream.Dispose(); } }); var response = await Client.PostAsync( $"{Constants.FilesRoute}/{Constants.UploadPush}/{Guid.NewGuid()}", psContent).ConfigureAwait(false); Assert.True(response.IsSuccessStatusCode); } }