public async Task<UploadResult> UploadAsync(UniqueUploadRequest request) { if (!request.Stream.CanRead) { throw new ArgumentException("The provided stream must be readable."); } using (request.Stream) { // get the current var getLatestRequest = new GetLatestRequest { ConnectionString = request.ConnectionString, Container = request.Container, PathFormat = request.PathFormat, Trace = request.Trace }; request.Trace.Write("Gettings the existing latest..."); string etag = null; using (var currentResult = await _innerClient.GetLatestStreamAsync(getLatestRequest)) { // return nothing if the streams are equivalent if (currentResult != null) { if (!request.Stream.CanSeek) { throw new ArgumentException("The provided stream must be seekable"); } // seek to the beginning for calculating the hash request.Stream.Seek(0, SeekOrigin.Begin); var contentMD5 = await GetStreamContentMD5(request.Stream); if (contentMD5 == currentResult.ContentMD5) { request.Trace.WriteLine(" exactly the same! No upload required."); return null; } if (request.EqualsAsync != null) { // seek to the beginning for comparing the content request.Stream.Seek(0, SeekOrigin.Begin); if (await request.EqualsAsync(currentResult)) { request.Trace.WriteLine(" equivalent! No upload required."); return null; } } etag = currentResult.ETag; request.Trace.WriteLine(" different! The provided content will be uploaded."); } else { request.Trace.WriteLine(" non-existent! The provided content will be uploaded."); } } // Seek to the beginning for uploading the blob. request.Stream.Seek(0, SeekOrigin.Begin); var uploadRequest = new UploadRequest { ConnectionString = request.ConnectionString, ETag = etag, UseETags = request.UseETag, Stream = request.Stream, PathFormat = request.PathFormat, Container = request.Container, Trace = request.Trace, UploadDirect = request.UploadDirect, UploadLatest = true, ContentType = request.ContentType, Type = request.Type }; return await _innerClient.UploadAsync(uploadRequest); } }
private static async Task<int> ExecuteAsync(Options options) { // build the implementation models using (var stdin = Console.OpenStandardInput()) { var client = new Client(new SystemTime(), new PathBuilder()); if (options.OnlyUnique) { var uniqueClient = new UniqueClient(client); // we have to buffer the input for byte-by-byte comparison using (var buffer = new MemoryStream()) { await stdin.CopyToAsync(buffer); buffer.Seek(0, SeekOrigin.Begin); var request = new UniqueUploadRequest { ConnectionString = options.ConnectionString, Container = options.Container, ContentType = options.ContentType, PathFormat = options.PathFormat, Stream = buffer, EqualsAsync = null, UploadDirect = !options.NoDirect, Trace = Console.Out }; // upload await uniqueClient.UploadAsync(request); } } else { var request = new UploadRequest { ConnectionString = options.ConnectionString, Container = options.Container, ContentType = options.ContentType, PathFormat = options.PathFormat, UploadDirect = !options.NoDirect, UploadLatest = !options.NoLatest, Stream = stdin, Trace = Console.Out }; // upload await client.UploadAsync(request); } } return 0; }
public TestContext() { // data UtcNow = new DateTimeOffset(2015, 1, 2, 3, 4, 5, 6, TimeSpan.Zero); Content = "newContent"; Container = TestSupport.GetTestContainer(); EqualsAsyncCalled = false; UniqueUploadRequest = new UniqueUploadRequest { ConnectionString = TestSupport.ConnectionString, Container = Container, ContentType = "text/plain", PathFormat = "testpath/{0}.txt", UploadDirect = true, Stream = new MemoryStream(Encoding.UTF8.GetBytes(Content)), Trace = TextWriter.Null, EqualsAsync = async x => { EqualsAsyncCalled = true; var actualContent = await new StreamReader(x.Stream).ReadLineAsync(); return actualContent == Content; } }; GetLatestRequest = new GetLatestRequest { ConnectionString = UniqueUploadRequest.ConnectionString, Container = UniqueUploadRequest.Container, PathFormat = UniqueUploadRequest.PathFormat, Trace = TextWriter.Null }; // dependencies SystemTime = new Mock<ISystemTime>(); PathBuilder = new PathBuilder(); Client = new Client(SystemTime.Object, PathBuilder); // setup SystemTime.Setup(x => x.UtcNow).Returns(() => UtcNow); // target Target = new UniqueClient(Client); }